From de2b675a090cf0426b6693fc9bf717eefbec2281 Mon Sep 17 00:00:00 2001 From: Vikram Reddy Date: Sun, 23 Jul 2023 17:45:59 +0530 Subject: [PATCH] Charts - code cleanup --- .../Components/Charts/BarChart.razor.cs | 4 +- .../Components/Charts/BaseChart.cs | 37 +++++++------------ .../Components/Charts/DoughnutChart.razor.cs | 4 +- .../Components/Charts/LineChart.razor.cs | 4 +- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/blazorbootstrap/Components/Charts/BarChart.razor.cs b/blazorbootstrap/Components/Charts/BarChart.razor.cs index 78c96d649..3a9f1917d 100644 --- a/blazorbootstrap/Components/Charts/BarChart.razor.cs +++ b/blazorbootstrap/Components/Charts/BarChart.razor.cs @@ -22,7 +22,7 @@ public override async Task InitializeAsync(ChartData chartData, IChartOptions ch if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.bar.initialize", ElementId, GetChartType(), data, (BarChartOptions)chartOptions); } } @@ -32,7 +32,7 @@ public override async Task UpdateAsync(ChartData chartData, IChartOptions chartO if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.bar.update", ElementId, GetChartType(), data, (BarChartOptions)chartOptions); } } diff --git a/blazorbootstrap/Components/Charts/BaseChart.cs b/blazorbootstrap/Components/Charts/BaseChart.cs index 1c61831f8..6c4fb4fb7 100644 --- a/blazorbootstrap/Components/Charts/BaseChart.cs +++ b/blazorbootstrap/Components/Charts/BaseChart.cs @@ -12,11 +12,6 @@ public class BaseChart : BaseComponent #region Methods - protected override async Task OnInitializedAsync() - { - await base.OnInitializedAsync(); - } - //public async Task Clear() { } /// @@ -48,10 +43,7 @@ public virtual async Task InitializeAsync(ChartData chartData, IChartOptions cha /// /// /// - public async Task ResizeAsync(int width, int height) - { - await JS.InvokeVoidAsync("window.blazorChart.resize", ElementId, width, height); - } + public async Task ResizeAsync(int width, int height) => await JS.InvokeVoidAsync("window.blazorChart.resize", ElementId, width, height); //public async Task Stop() { } @@ -101,28 +93,25 @@ private object GetChartDataObject(ChartData chartData) var data = new { - Labels = chartData.Labels, + chartData.Labels, Datasets = datasets }; return data; } - protected string GetChartType() + protected string GetChartType() => chartType switch { - return chartType switch - { - ChartType.Bar => "bar", - ChartType.Bubble => "bubble", - ChartType.Doughnut => "doughnut", - ChartType.Line => "line", - ChartType.Pie => "pie", - ChartType.PolarArea => "polarArea", - ChartType.Radar => "radar", - ChartType.Scatter => "scatter", - _ => "line" // default - }; - } + ChartType.Bar => "bar", + ChartType.Bubble => "bubble", + ChartType.Doughnut => "doughnut", + ChartType.Line => "line", + ChartType.Pie => "pie", + ChartType.PolarArea => "polarArea", + ChartType.Radar => "radar", + ChartType.Scatter => "scatter", + _ => "line" // default + }; private string GetChartContainerSizeAsStyle() { diff --git a/blazorbootstrap/Components/Charts/DoughnutChart.razor.cs b/blazorbootstrap/Components/Charts/DoughnutChart.razor.cs index 6d2e4c1cd..7e7384bac 100644 --- a/blazorbootstrap/Components/Charts/DoughnutChart.razor.cs +++ b/blazorbootstrap/Components/Charts/DoughnutChart.razor.cs @@ -22,7 +22,7 @@ public override async Task InitializeAsync(ChartData chartData, IChartOptions ch if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.doughnut.initialize", ElementId, GetChartType(), data, (DoughnutChartOptions)chartOptions); } } @@ -32,7 +32,7 @@ public override async Task UpdateAsync(ChartData chartData, IChartOptions chartO if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.doughnut.update", ElementId, GetChartType(), data, (DoughnutChartOptions)chartOptions); } } diff --git a/blazorbootstrap/Components/Charts/LineChart.razor.cs b/blazorbootstrap/Components/Charts/LineChart.razor.cs index 6fb58fa1a..79e9ae798 100644 --- a/blazorbootstrap/Components/Charts/LineChart.razor.cs +++ b/blazorbootstrap/Components/Charts/LineChart.razor.cs @@ -22,7 +22,7 @@ public override async Task InitializeAsync(ChartData chartData, IChartOptions ch if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.line.initialize", ElementId, GetChartType(), data, (LineChartOptions)chartOptions); } } @@ -32,7 +32,7 @@ public override async Task UpdateAsync(ChartData chartData, IChartOptions chartO if (chartData is not null && chartData.Datasets is not null) { var datasets = chartData.Datasets.OfType(); - var data = new { Labels = chartData.Labels, Datasets = datasets }; + var data = new { chartData.Labels, Datasets = datasets }; await JS.InvokeVoidAsync("window.blazorChart.line.update", ElementId, GetChartType(), data, (LineChartOptions)chartOptions); } }