Skip to content

Commit

Permalink
Charts - code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gvreddy04 committed Jul 23, 2023
1 parent c9700e4 commit de2b675
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
4 changes: 2 additions & 2 deletions blazorbootstrap/Components/Charts/BarChart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarChartDataset>();
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);
}
}
Expand All @@ -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<BarChartDataset>();
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);
}
}
Expand Down
37 changes: 13 additions & 24 deletions blazorbootstrap/Components/Charts/BaseChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public class BaseChart : BaseComponent

#region Methods

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
}

//public async Task Clear() { }

/// <summary>
Expand Down Expand Up @@ -48,10 +43,7 @@ public virtual async Task InitializeAsync(ChartData chartData, IChartOptions cha
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
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() { }

Expand Down Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions blazorbootstrap/Components/Charts/DoughnutChart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DoughnutChartDataset>();
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);
}
}
Expand All @@ -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<DoughnutChartDataset>();
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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions blazorbootstrap/Components/Charts/LineChart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LineChartDataset>();
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);
}
}
Expand All @@ -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<LineChartDataset>();
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);
}
}
Expand Down

0 comments on commit de2b675

Please sign in to comment.