Skip to content

Commit c1997e5

Browse files
authored
Merge pull request #205 from apexcharts/add-xAxisLabelClick-event
Added XAxisLabelClick
2 parents 123aec3 + 1fbe8d0 commit c1997e5

File tree

9 files changed

+243
-3
lines changed

9 files changed

+243
-3
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<DemoContainer>
2+
<ApexChart TItem="Order"
3+
Title="Order Net Value"
4+
OnXAxisLabelClick=XAxisLabelClick
5+
@ref=chart>
6+
7+
<ApexPointSeries TItem="Order"
8+
Items="Orders"
9+
Name="Gross Value"
10+
SeriesType="SeriesType.Line"
11+
XValue="@(e => e.Country)"
12+
YAggregate="@(e => e.Sum(e => e.GrossValue))"
13+
OrderByDescending="e=>e.X" />
14+
15+
<ApexPointSeries TItem="Order"
16+
Items="Orders"
17+
Name="Net Value"
18+
SeriesType="SeriesType.Line"
19+
XValue="@(e => e.Country)"
20+
YAggregate="@(e => e.Sum(e => e.NetValue))"
21+
OrderByDescending="e=>e.X" />
22+
</ApexChart>
23+
</DemoContainer>
24+
25+
@if (selectedData != null)
26+
{
27+
<Alert BackgroundColor="TablerColor.Primary">
28+
<h3>You clicked: @selectedData.Caption [@selectedData.LabelIndex] </h3>
29+
30+
@foreach (var seriesPoint in selectedData.SeriesPoints)
31+
{
32+
var dataPoint = (DataPoint<Order>)seriesPoint.DataPoint;
33+
<div>@seriesPoint.Series.Name: @dataPoint.Y</div>
34+
}
35+
36+
</Alert>
37+
}
38+
39+
@code {
40+
private List<Order> Orders { get; set; } = SampleData.GetOrders();
41+
private XAxisLabelClicked<Order> selectedData;
42+
private ApexChart<Order> chart;
43+
44+
private void XAxisLabelClick(XAxisLabelClicked<Order> data)
45+
{
46+
selectedData = data;
47+
48+
}
49+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<DemoContainer>
2+
<ApexChart TItem="Order" Title="Orders Value"
3+
XAxisType="XAxisType.Datetime"
4+
Options="options"
5+
OnXAxisLabelClick=XAxisLabelClick
6+
Debug>
7+
8+
<ApexPointSeries TItem="Order"
9+
Items="SampleData.GetOrders()"
10+
Name="Net Value"
11+
SeriesType="SeriesType.Line"
12+
XValue="@(e => e.OrderDate.FirstDayOfMonth())"
13+
YAggregate="@(e => e.Sum(e => e.NetValue))"
14+
OrderBy="e=>e.X" />
15+
16+
<ApexPointSeries TItem="Order"
17+
Items="SampleData.GetOrders()"
18+
Name="Gross Value"
19+
SeriesType="SeriesType.Line"
20+
XValue="@(e => e.OrderDate.FirstDayOfMonth())"
21+
YAggregate="@(e => e.Sum(e => e.GrossValue))"
22+
OrderBy="e=>e.X" />
23+
</ApexChart>
24+
25+
@if (selectedData != null)
26+
{
27+
<Alert BackgroundColor="TablerColor.Primary">
28+
<h3>You clicked: @selectedData.Caption [@selectedData.LabelIndex] </h3>
29+
30+
@foreach (var seriesPoint in selectedData.SeriesPoints)
31+
{
32+
var dataPoint = (DataPoint<Order>)seriesPoint.DataPoint;
33+
<div>@seriesPoint.Series.Name - @dataPoint.Y</div>
34+
}
35+
36+
</Alert>
37+
}
38+
39+
</DemoContainer>
40+
41+
@code {
42+
private ApexChartOptions<Order> options = new ApexCharts.ApexChartOptions<Order>();
43+
private XAxisLabelClicked<Order> selectedData;
44+
private void XAxisLabelClick(XAxisLabelClicked<Order> data)
45+
{
46+
47+
selectedData = data;
48+
49+
}
50+
51+
protected override void OnInitialized()
52+
{
53+
54+
options.Debug = true;
55+
options.ForecastDataPoints = new ForecastDataPoints
56+
{
57+
Count = 3,
58+
DashArray = 4,
59+
FillOpacity = 0.5,
60+
};
61+
options.Tooltip = new ApexCharts.Tooltip { X = new TooltipX { Format = @"MMMM \ yyyy" } };
62+
options.Subtitle = new Subtitle { OffsetY = 15, Text = "DateTime sample with options" };
63+
options.Tooltip = new ApexCharts.Tooltip
64+
{
65+
Y = new TooltipY
66+
{
67+
Title = new TooltipYTitle { Formatter = @"function(name) { return name + ':' }" },
68+
Formatter = @"function(value, { series, seriesIndex, dataPointIndex, w }) { return '$' + value }"
69+
},
70+
};
71+
options.Xaxis = new XAxis
72+
{
73+
Title = new AxisTitle
74+
{
75+
OffsetY = 5,
76+
Text = "Month",
77+
Style = new AxisTitleStyle { FontSize = "14px", Color = "lightgrey" }
78+
},
79+
AxisBorder = new AxisBorder
80+
{
81+
Height = 2
82+
}
83+
};
84+
options.Yaxis = new List<YAxis>();
85+
options.Yaxis.Add(new YAxis
86+
{
87+
DecimalsInFloat = 0,
88+
Labels = new YAxisLabels { Rotate = -45, Style = new AxisLabelStyle { FontSize = "10px" } },
89+
Title = new AxisTitle { Text = "Value", Style = new AxisTitleStyle { FontSize = "14px", Color = "lightgrey" } }
90+
});
91+
92+
options.Annotations = new Annotations { Yaxis = new List<AnnotationsYAxis>() };
93+
options.Annotations.Yaxis.Add(new AnnotationsYAxis
94+
{
95+
Y = 50000,
96+
BorderWidth = 2,
97+
StrokeDashArray = 5,
98+
BorderColor = "red",
99+
Label = new Label { Text = "Monthly Target" }
100+
});
101+
}
102+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page "/events/xaxis-label-click"
2+
3+
<DocExamples Title="X Axis Label Click">
4+
5+
<Description>
6+
Click on a XAxis label to trigger an event
7+
</Description>
8+
9+
<ChildContent>
10+
11+
<CodeSnippet Title="Basic" ClassName=@typeof(Basic).ToString()>
12+
<Snippet>
13+
<Basic />
14+
</Snippet>
15+
</CodeSnippet>
16+
17+
<CodeSnippet Title="DateTime" ClassName=@typeof(DateTime).ToString()>
18+
<Snippet>
19+
<DateTime />
20+
</Snippet>
21+
</CodeSnippet>
22+
23+
</ChildContent>
24+
25+
26+
</DocExamples>

docs/BlazorApexCharts.Docs/Shared/MainNavigation.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<NavbarMenuItem Text="Legend Click" Href="events/legend-click" />
112112
<NavbarMenuItem Text="Marker Click" Href="events/marker-click" />
113113
<NavbarMenuItem Text="Selection" Href="events/selection" />
114+
<NavbarMenuItem Text="XAxisLabelClick" Href="events/xaxis-label-click" />
114115
<NavbarMenuItem Text="Zoomed" Href="events/zoomed" />
115116
</SubMenu>
116117
</NavbarMenuItem>

src/Blazor-ApexCharts/ApexChart.razor.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public partial class ApexChart<TItem> : IDisposable where TItem : class
2323
[Parameter] public object Width { get; set; }
2424
[Parameter] public object Height { get; set; }
2525

26+
27+
[Parameter] public EventCallback<XAxisLabelClicked<TItem>> OnXAxisLabelClick { get; set; }
2628
[Parameter] public EventCallback<SelectedData<TItem>> OnMarkerClick { get; set; }
2729
[Parameter] public EventCallback<SelectedData<TItem>> OnDataPointSelection { get; set; }
2830
[Parameter] public EventCallback<HoverData<TItem>> OnDataPointEnter { get; set; }
@@ -349,7 +351,7 @@ public virtual async Task AppendDataAsync(IEnumerable<TItem> items)
349351

350352
public virtual async Task ZoomXAsync(ZoomOptions zoomOptions)
351353
{
352-
if(zoomOptions == null) { throw new ArgumentNullException(nameof(zoomOptions)); }
354+
if (zoomOptions == null) { throw new ArgumentNullException(nameof(zoomOptions)); }
353355
await JSRuntime.InvokeVoidAsync("blazor_apexchart.zoomX", Options.Chart.Id, zoomOptions.Start, zoomOptions.End);
354356
}
355357

@@ -472,6 +474,7 @@ private void SetEvents()
472474
Options.HasDataPointLeave = OnDataPointLeave.HasDelegate;
473475
Options.HasLegendClick = OnLegendClicked.HasDelegate;
474476
Options.HasMarkerClick = OnMarkerClick.HasDelegate;
477+
Options.HasXAxisLabelClick = OnXAxisLabelClick.HasDelegate;
475478
Options.HasSelection = OnSelection.HasDelegate;
476479
Options.HasBrushScrolled = OnBrushScrolled.HasDelegate;
477480
Options.HasZoomed = OnZoomed.HasDelegate;
@@ -652,6 +655,29 @@ public void JSLegendClicked(JSLegendClicked jsLegendClicked)
652655
OnLegendClicked.InvokeAsync(legendClicked);
653656
}
654657

658+
[JSInvokable("JSXAxisLabelClick")]
659+
public void JSXAxisLabelClick(JSXAxisLabelClick jsXAxisLabelClick)
660+
{
661+
if (OnXAxisLabelClick.HasDelegate)
662+
{
663+
var data = new XAxisLabelClicked<TItem>();
664+
data.LabelIndex = jsXAxisLabelClick.LabelIndex;
665+
data.Caption = jsXAxisLabelClick.Caption;
666+
data.SeriesPoints = new List<SeriesDataPoint<TItem>>();
667+
668+
foreach (var series in Options.Series)
669+
{
670+
data.SeriesPoints.Add(new SeriesDataPoint<TItem>
671+
{
672+
Series = series,
673+
DataPoint = series.Data.ElementAt(jsXAxisLabelClick.LabelIndex)
674+
});
675+
}
676+
677+
OnXAxisLabelClick.InvokeAsync(data);
678+
}
679+
}
680+
655681
[JSInvokable("JSMarkerClick")]
656682
public void JSMarkerClick(JSDataPointSelection selectedDataPoints)
657683
{

src/Blazor-ApexCharts/Models/ApexChartOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class ApexChartOptions<TItem> where TItem : class
1313
public bool HasDataPointLeave { get; internal set; }
1414
public bool HasLegendClick { get; internal set; }
1515
public bool HasMarkerClick { get; internal set; }
16+
public bool HasXAxisLabelClick { get; internal set; }
17+
1618
public bool HasSelection { get; internal set; }
1719
public bool HasBrushScrolled { get; internal set; }
1820
public bool HasZoomed { get; internal set; }

src/Blazor-ApexCharts/Models/DataPoints/JSDataPointSelection.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ public class JSDataPointSelection
77
public List<List<int?>> SelectedDataPoints { get; set; }
88
public int DataPointIndex { get; set; }
99
public int SeriesIndex { get; set; }
10-
1110
}
1211

12+
public class JSXAxisLabelClick
13+
{
14+
public int LabelIndex { get; set; }
15+
public string Caption { get; set; }
16+
}
1317

14-
public class SelectedData<TItem> where TItem:class
18+
public class SelectedData<TItem> where TItem : class
1519
{
1620
public ApexChart<TItem> Chart { get; set; }
1721
public Series<TItem> Series { get; set; }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace ApexCharts
4+
{
5+
public class XAxisLabelClicked<TItem> where TItem : class
6+
{
7+
public int LabelIndex { get; set; }
8+
public string Caption { get; set; }
9+
public List<SeriesDataPoint<TItem>> SeriesPoints { get; set; }
10+
11+
}
12+
13+
public class SeriesDataPoint<TItem> where TItem : class
14+
{
15+
public Series<TItem> Series { get; set; }
16+
public IDataPoint<TItem> DataPoint { get; set; }
17+
18+
}
19+
20+
}

src/Blazor-ApexCharts/wwwroot/js/blazor-apex-charts.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,17 @@
249249
}
250250
};
251251

252+
if (options.hasXAxisLabelClick === true) {
253+
options.chart.events.xAxisLabelClick = function (event, chartContext, config) {
254+
var data = {
255+
labelIndex: config.labelIndex,
256+
caption: event.target.innerHTML
257+
};
258+
dotNetObject.invokeMethodAsync('JSXAxisLabelClick', data);
259+
}
260+
};
252261

262+
253263
if (options.hasLegendClick === true) {
254264
options.chart.events.legendClick = function (chartContext, seriesIndex, config) {
255265
var legendClick = {

0 commit comments

Comments
 (0)