-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
248 additions
and
35 deletions.
There are no files selected for viewing
129 changes: 99 additions & 30 deletions
129
BlazorBootstrap.Demo/Pages/Charts/BarCharts/Charts_Demo_01_BarChart_Examples.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,150 @@ | ||
@using BlazorBootstrap.Extensions | ||
@using Color = System.Drawing.Color | ||
|
||
<BarChart @ref="barChart" Width="700" Class="mb-4" /> | ||
<BarChart @ref="barChart" Width="800" Class="mb-4" /> | ||
|
||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await AddDataAsync()">Add Random Data</Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await RandomizeAsync()"> Randomize </Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await AddDatasetAsync()"> Add Dataset </Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await AddDataAsync()">Add Data</Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await ShowHorizontalBarChartAsync()">Horizontal Bar Chart</Button> | ||
<Button Type="ButtonType.Button" Color="ButtonColor.Primary" Size="Size.Small" @onclick="async () => await ShowVerticalBarChartAsync()">Vertical Bar Chart</Button> | ||
|
||
@code { | ||
private BarChart barChart; | ||
|
||
private ChartData chartData; | ||
private BarChartOptions chartOptions; | ||
private BarChart barChart = default!; | ||
private BarChartOptions barChartOptions = default!; | ||
private ChartData chartData = default!; | ||
|
||
int datasetsCount = 0; | ||
int labelsCount = 0; | ||
string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; | ||
Random random = new Random(); | ||
|
||
protected override void OnInitialized() | ||
{ | ||
chartData = new ChartData | ||
{ | ||
Labels = new List<string> { "January", "February", "March", "April", "May", "June", "July" }, | ||
Datasets = new List<IChartDataset>() | ||
}; | ||
|
||
chartData.Datasets.Add(GetRandomBarChartDataset()); | ||
chartData.Datasets.Add(GetRandomBarChartDataset()); | ||
chartData.Datasets.Add(GetRandomBarChartDataset()); | ||
|
||
chartOptions = new BarChartOptions | ||
{ | ||
Responsive = true, | ||
Interaction = new Interaction { Mode = InteractionMode.Index } | ||
}; | ||
chartData = new ChartData { Labels = GetDefaultDataLabels(6), Datasets = GetDefaultDataSets(3) }; | ||
barChartOptions = new BarChartOptions { Responsive = true, Interaction = new Interaction { Mode = InteractionMode.Index } }; | ||
} | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
await barChart.InitializeAsync(chartData, chartOptions); | ||
await barChart.InitializeAsync(chartData, barChartOptions); | ||
} | ||
await base.OnAfterRenderAsync(firstRender); | ||
} | ||
|
||
private async Task AddDataAsync() | ||
private async Task RandomizeAsync() | ||
{ | ||
if (chartData is null || chartData.Datasets is null || !chartData.Datasets.Any()) return; | ||
|
||
var newDatasets = new List<IChartDataset>(); | ||
|
||
foreach (var dataset in chartData.Datasets) | ||
{ | ||
if (dataset is BarChartDataset barChartDataset | ||
&& barChartDataset is not null | ||
&& barChartDataset.Data is not null) | ||
{ | ||
var count = barChartDataset.Data.Count; | ||
|
||
var newData = new List<double>(); | ||
for (var i = 0; i < count; i++) | ||
{ | ||
newData.Add(random.Next(200)); | ||
} | ||
|
||
barChartDataset.Data = newData; | ||
newDatasets.Add(barChartDataset); | ||
} | ||
} | ||
|
||
chartData.Datasets = newDatasets; | ||
|
||
await barChart.UpdateAsync(chartData, barChartOptions); | ||
} | ||
|
||
private async Task AddDatasetAsync() | ||
{ | ||
if (chartData is null || chartData.Datasets is null) return; | ||
} | ||
|
||
private async Task AddDataAsync() | ||
{ | ||
if (chartData is null || chartData.Datasets is null) | ||
return; | ||
|
||
chartData.Datasets.Add(GetRandomBarChartDataset()); | ||
await barChart.UpdateAsync(chartData, chartOptions); | ||
await barChart.UpdateAsync(chartData, barChartOptions); | ||
} | ||
|
||
private async Task ShowHorizontalBarChartAsync() | ||
{ | ||
chartOptions.IndexAxis = "y"; | ||
await barChart.UpdateAsync(chartData, chartOptions); | ||
barChartOptions.IndexAxis = "y"; | ||
await barChart.UpdateAsync(chartData, barChartOptions); | ||
} | ||
|
||
private async Task ShowVerticalBarChartAsync() | ||
{ | ||
chartOptions.IndexAxis = "x"; | ||
await barChart.UpdateAsync(chartData, chartOptions); | ||
barChartOptions.IndexAxis = "x"; | ||
await barChart.UpdateAsync(chartData, barChartOptions); | ||
} | ||
|
||
private List<IChartDataset> GetDefaultDataSets(int numberOfDatasets) | ||
{ | ||
var datasets = new List<IChartDataset>(); | ||
|
||
for (var index = 0; index < numberOfDatasets; index++) | ||
{ | ||
datasets.Add(GetRandomBarChartDataset()); | ||
} | ||
|
||
return datasets; | ||
} | ||
|
||
private BarChartDataset GetRandomBarChartDataset() | ||
{ | ||
datasetsCount += 1; | ||
|
||
// random color | ||
var c = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)); | ||
Console.WriteLine($"Bar Chart: Color Name: {c.Name}, HEX: {c.ToHexString()}, RGB: {c.ToRgbString()}, IsNamedColor: {c.IsNamedColor}"); | ||
|
||
return new BarChartDataset() | ||
{ | ||
Label = $"Bar chart dataset {chartData.Datasets.Count + 1}", | ||
Data = new List<double> { random.Next(120), random.Next(120), random.Next(120), random.Next(120), random.Next(120), random.Next(120), random.Next(120) }, | ||
Label = $"Product {datasetsCount}", | ||
Data = GetRandomData(), | ||
BackgroundColor = new List<string> { c.ToRgbString() }, | ||
BorderColor = new List<string> { c.ToRgbString() }, | ||
BorderWidth = new List<double> { 0 }, | ||
}; | ||
} | ||
|
||
private List<double> GetRandomData() | ||
{ | ||
var data = new List<double>(); | ||
for (var index = 0; index < labelsCount; index++) | ||
{ | ||
data.Add(random.Next(200)); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
private List<string> GetDefaultDataLabels(int numberOfLabels) | ||
{ | ||
var labels = new List<string>(); | ||
for (var index = 0; index < numberOfLabels; index++) | ||
{ | ||
labels.Add(GetNextDataLabel()); | ||
} | ||
|
||
return labels; | ||
} | ||
|
||
private string GetNextDataLabel() | ||
{ | ||
labelsCount += 1; | ||
return months[labelsCount]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters