From ff0656dd67bc10c85c313e31297c97b7b7f96eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Dang=C3=A5rden?= Date: Fri, 20 Jan 2023 18:07:10 +0100 Subject: [PATCH] Added MarkerClick --- .../Components/Events/MarkerClick/Basic.razor | 42 +++++++++++++++++++ .../Events/MarkerClick/MarkerClick.razor | 18 ++++++++ .../Shared/MainNavigation.razor | 1 + src/Blazor-ApexCharts/ApexChart.razor.cs | 26 ++++++++++++ .../Models/ApexChartOptions.cs | 1 + .../wwwroot/js/blazor-apex-charts.js | 12 ++++++ 6 files changed, 100 insertions(+) create mode 100644 docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/Basic.razor create mode 100644 docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/MarkerClick.razor diff --git a/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/Basic.razor b/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/Basic.razor new file mode 100644 index 00000000..b10f2096 --- /dev/null +++ b/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/Basic.razor @@ -0,0 +1,42 @@ + + + + + + + + + +@if (selectedData != null) +{ + +

You clicked @selectedData.DataPoint.X @selectedData.DataPoint.Items.Sum(e=> e.NetValue) (@selectedData.DataPoint.Items.Sum(e=> e.GrossValue))

+
+} + +@code { + private List Orders { get; set; } = SampleData.GetOrders(); + private SelectedData selectedData; + private ApexChart chart; + + private void MarkerClick(SelectedData data) + { + selectedData = data; + + } +} \ No newline at end of file diff --git a/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/MarkerClick.razor b/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/MarkerClick.razor new file mode 100644 index 00000000..e1b30911 --- /dev/null +++ b/docs/BlazorApexCharts.Docs/Components/Events/MarkerClick/MarkerClick.razor @@ -0,0 +1,18 @@ +@page "/events/marker-click" + + + + + Click on a marker to trigger an event + + + + + + + + + + + + diff --git a/docs/BlazorApexCharts.Docs/Shared/MainNavigation.razor b/docs/BlazorApexCharts.Docs/Shared/MainNavigation.razor index a4448871..2064fbc0 100644 --- a/docs/BlazorApexCharts.Docs/Shared/MainNavigation.razor +++ b/docs/BlazorApexCharts.Docs/Shared/MainNavigation.razor @@ -109,6 +109,7 @@ + diff --git a/src/Blazor-ApexCharts/ApexChart.razor.cs b/src/Blazor-ApexCharts/ApexChart.razor.cs index 21dc6a02..2516b7cf 100644 --- a/src/Blazor-ApexCharts/ApexChart.razor.cs +++ b/src/Blazor-ApexCharts/ApexChart.razor.cs @@ -22,6 +22,8 @@ public partial class ApexChart : IDisposable where TItem : class [Parameter] public bool Debug { get; set; } [Parameter] public object Width { get; set; } [Parameter] public object Height { get; set; } + + [Parameter] public EventCallback> OnMarkerClick { get; set; } [Parameter] public EventCallback> OnDataPointSelection { get; set; } [Parameter] public EventCallback> OnDataPointEnter { get; set; } [Parameter] public EventCallback> OnDataPointLeave { get; set; } @@ -469,6 +471,7 @@ private void SetEvents() Options.HasDataPointEnter = OnDataPointEnter.HasDelegate || ApexPointTooltip != null; Options.HasDataPointLeave = OnDataPointLeave.HasDelegate; Options.HasLegendClick = OnLegendClicked.HasDelegate; + Options.HasMarkerClick = OnMarkerClick.HasDelegate; Options.HasSelection = OnSelection.HasDelegate; Options.HasBrushScrolled = OnBrushScrolled.HasDelegate; Options.HasZoomed = OnZoomed.HasDelegate; @@ -649,6 +652,29 @@ public void JSLegendClicked(JSLegendClicked jsLegendClicked) OnLegendClicked.InvokeAsync(legendClicked); } + [JSInvokable("JSMarkerClick")] + public void JSMarkerClick(JSDataPointSelection selectedDataPoints) + { + if (OnMarkerClick.HasDelegate) + { + var series = Options.Series.ElementAt(selectedDataPoints.SeriesIndex); + var dataPoint = series.Data.ElementAt(selectedDataPoints.DataPointIndex); + + var selection = new SelectedData + { + Chart = this, + Series = series, + DataPoint = dataPoint, + IsSelected = selectedDataPoints.SelectedDataPoints?.Any(e => e != null && e.Any(e => e != null && e.HasValue)) ?? false, + DataPointIndex = selectedDataPoints.DataPointIndex, + SeriesIndex = selectedDataPoints.SeriesIndex + }; + + OnMarkerClick.InvokeAsync(selection); + } + } + + [JSInvokable("JSDataPointSelected")] public void JSDataPointSelected(JSDataPointSelection selectedDataPoints) { diff --git a/src/Blazor-ApexCharts/Models/ApexChartOptions.cs b/src/Blazor-ApexCharts/Models/ApexChartOptions.cs index 0dcae37b..1d458217 100644 --- a/src/Blazor-ApexCharts/Models/ApexChartOptions.cs +++ b/src/Blazor-ApexCharts/Models/ApexChartOptions.cs @@ -12,6 +12,7 @@ public class ApexChartOptions where TItem : class public bool HasDataPointEnter { get; internal set; } public bool HasDataPointLeave { get; internal set; } public bool HasLegendClick { get; internal set; } + public bool HasMarkerClick { get; internal set; } public bool HasSelection { get; internal set; } public bool HasBrushScrolled { get; internal set; } public bool HasZoomed { get; internal set; } diff --git a/src/Blazor-ApexCharts/wwwroot/js/blazor-apex-charts.js b/src/Blazor-ApexCharts/wwwroot/js/blazor-apex-charts.js index eecf26da..58bd8abe 100644 --- a/src/Blazor-ApexCharts/wwwroot/js/blazor-apex-charts.js +++ b/src/Blazor-ApexCharts/wwwroot/js/blazor-apex-charts.js @@ -238,6 +238,18 @@ } }; + if (options.hasMarkerClick === true) { + options.chart.events.markerClick = function (event, chartContext, config) { + var selection = { + dataPointIndex: config.dataPointIndex, + seriesIndex: config.seriesIndex, + selectedDataPoints: config.selectedDataPoints + } + dotNetObject.invokeMethodAsync('JSMarkerClick', selection); + } + }; + + if (options.hasLegendClick === true) { options.chart.events.legendClick = function (chartContext, seriesIndex, config) { var legendClick = {