Bar Chart
The Blazor Bar Chart component displays data values as vertical bars, making it easy to compare multiple data sets or visualize trends over time.
Chart setup #
How it works #
The Bar Chart component visualizes data as vertical bars, making it easy to compare values across categories or track changes over time.
How to use:
This demo showcases the essential setup for a bar chart and demonstrates how to bind your data for quick visualization.
How to use:
- Add the
BarChartcomponent to your page. - Provide
Labelsfor the X-axis categories and one or moreDatasetsfor the Y-axis values. - Customize appearance and behavior using the
Optionsproperty (e.g., colors, tooltips, axis settings). - Refer to the demo code for examples of basic and advanced usage, including multiple datasets and custom styling.
<BarChart @ref="barChart" Width="600" />
<div class="mt-5">
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="RandomizeAsync"> Randomize </Button>
<Button Color="ButtonColor.Link" Size="ButtonSize.Small" @onclick="AddDatasetAsync"> Add Dataset </Button>
<Button Color="ButtonColor.Info" Size="ButtonSize.Small" @onclick="AddDataAsync">Add Data</Button>
<Button Color="ButtonColor.Success" Size="ButtonSize.Small" @onclick="ShowHorizontalBarChartAsync">Horizontal Bar Chart</Button>
<Button Color="ButtonColor.Warning" Size="ButtonSize.Small" @onclick="ShowVerticalBarChartAsync">Vertical Bar Chart</Button>
</div>
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
private int datasetsCount = 0;
private int labelsCount = 0;
private string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
private Random random = new();
protected override void OnInitialized()
{
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, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
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;
if (datasetsCount >= 12)
return;
var chartDataset = GetRandomBarChartDataset();
chartData = await barChart.AddDatasetAsync(chartData, chartDataset, barChartOptions);
}
private async Task AddDataAsync()
{
if (chartData is null || chartData.Datasets is null)
return;
if (labelsCount >= 12)
return;
var data = new List<IChartDatasetData>();
foreach (var dataset in chartData.Datasets)
{
if (dataset is BarChartDataset barChartDataset)
data.Add(new BarChartDatasetData(barChartDataset.Label, random.Next(200)));
}
chartData = await barChart.AddDataAsync(chartData, GetNextDataLabel(), data);
}
private async Task ShowHorizontalBarChartAsync()
{
barChartOptions.IndexAxis = "y";
await barChart.UpdateAsync(chartData, barChartOptions);
}
private async Task ShowVerticalBarChartAsync()
{
barChartOptions.IndexAxis = "x";
await barChart.UpdateAsync(chartData, barChartOptions);
}
#region Data Preparation
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()
{
var c = ChartJsColorExtensions.ToColor(ChartJsColorUtility.CategoricalTwelveColors[datasetsCount]);
datasetsCount += 1;
return new BarChartDataset()
{
Label = $"Product {datasetsCount}",
Data = GetRandomData(),
BackgroundColor = new List<string> { ChartJsColorExtensions.ToRgbString(c) },
BorderColor = new List<string> { ChartJsColorExtensions.ToRgbString(c) },
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 - 1];
}
#endregion Data Preparation
}
Combo bar/line #
The Combo bar/line demo mixes
How to use:
BarChartDataset and LineChartDataset in the same BarChart.
How to use:
- Use the
BarChartcomponent as the root chart. - Add both
BarChartDatasetandLineChartDatasetinstances to the sameChartData.Datasetscollection. - Configure interaction options such as
Mode = InteractionMode.IndexandIntersect = falsefor a combined tooltip experience. - Refer to the demo code below for a working example with bar columns and a line overlay.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var revenueColor = ChartJsColorExtensions.ToColor(ChartJsColorUtility.CategoricalTwelveColors[0]);
var trendColor = ChartJsColorExtensions.ToColor(ChartJsColorUtility.CategoricalTwelveColors[1]);
chartData = new ChartData
{
Labels = new List<string> { "January", "February", "March", "April", "May", "June" },
Datasets = new List<IChartDataset>
{
new BarChartDataset
{
Label = "Revenue",
Data = new List<double?> { 65, 59, 80, 81, 56, 55 },
BackgroundColor = new List<string> { ChartJsColorExtensions.ToRgbaString(revenueColor) },
BorderColor = new List<string> { ChartJsColorExtensions.ToRgbString(revenueColor) },
BorderWidth = new List<double> { 0 },
},
new LineChartDataset
{
Label = "Target",
Data = new List<double?> { 50, 55, 60, 70, 72, 78 },
BackgroundColor = ChartJsColorExtensions.ToRgbaString(trendColor),
BorderColor = ChartJsColorExtensions.ToRgbString(trendColor),
PointRadius = new List<double> { 4 },
PointHoverRadius = new List<double> { 6 },
},
},
};
barChartOptions = new BarChartOptions
{
Responsive = true,
Interaction = new Interaction { Mode = InteractionMode.Index, Intersect = false },
};
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
await barChart.InitializeAsync(chartData, barChartOptions);
await base.OnAfterRenderAsync(firstRender);
}
}Horizontal bar chart #
The Horizontal Bar Chart displays data values as horizontal bars, making it ideal for comparing categories with long labels or when you want to emphasize comparison between values.
How to use:
Refer to the demo code below for a working example and configuration options.
How to use:
- Use the
BarChartcomponent and set theIndexAxisoption to'y'to render bars horizontally. - Provide your data and labels as you would for a standard bar chart.
- Customize colors and appearance using the available palette utilities or your own color set.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Data = new List<double?> { 55000, 15000, 18000, 21000 },
BackgroundColor = new List<string> { ChartJsColorUtility.CategoricalTwelveColors[0] },
BorderColor = new List<string> { ChartJsColorUtility.CategoricalTwelveColors[0] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset1);
chartData = new ChartData { Labels = labels, Datasets = datasets };
barChartOptions = new BarChartOptions();
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.IndexAxis = "y";
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Plugins.Legend.Display = false;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await barChart.InitializeAsync(chartData, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
}Stacked bar chart #
The Stacked Bar Chart allows you to display multiple data series stacked on top of each other, making it easy to compare the total and individual contributions of each series for every category.
How to use:
How to use:
- Use the
BarChartcomponent and configure theOptions.Scales.X.StackedandOptions.Scales.Y.Stackedproperties totrueto enable stacking. - Provide multiple datasets in your chart data, each representing a different series to be stacked.
- Customize colors for each dataset using palette utilities or your own color set for better distinction.
- Refer to the demo code below for a working example and further configuration options.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var colors = ChartJsColorUtility.CategoricalTwelveColors;
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Label = "Windows",
Data = new List<double?> { 28000, 8000, 2000, 17000 },
BackgroundColor = new List<string> { colors[0] },
BorderColor = new List<string> { colors[0] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset1);
var dataset2 = new BarChartDataset()
{
Label = "macOS",
Data = new List<double?> { 8000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[1] },
BorderColor = new List<string> { colors[1] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset2);
var dataset3 = new BarChartDataset()
{
Label = "Other",
Data = new List<double?> { 28000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[2] },
BorderColor = new List<string> { colors[2] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset3);
chartData = new ChartData { Labels = labels, Datasets = datasets };
barChartOptions = new();
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.IndexAxis = "y";
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Scales.X.Stacked = true;
barChartOptions.Scales.Y.Stacked = true;
barChartOptions.Plugins.Title!.Text = "Operating system";
barChartOptions.Plugins.Title.Display = true;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await barChart.InitializeAsync(chartData, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
}Stacked bar chart with data labels #
The Stacked Bar Chart with Data Labels enhances the standard stacked bar chart by displaying value labels directly on each bar segment. This makes it easier to read and compare the values of each dataset within a category.
How to use:
How to use:
- Use the
BarChartcomponent and enable stacking by settingOptions.Scales.X.StackedandOptions.Scales.Y.Stackedtotrue. - Add multiple datasets to your chart data to represent different series.
- Enable data labels by configuring the chart's plugins, such as
Options.Plugins.Datalabels.Display = true. - Customize the appearance and formatting of data labels as needed for your scenario.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var colors = ChartJsColorUtility.CategoricalTwelveColors;
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Label = "Windows",
Data = new List<double?> { 28000, 8000, 2000, 17000 },
BackgroundColor = new List<string> { colors[0] },
BorderColor = new List<string> { colors[0] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset1);
var dataset2 = new BarChartDataset()
{
Label = "macOS",
Data = new List<double?> { 8000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[1] },
BorderColor = new List<string> { colors[1] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset2);
var dataset3 = new BarChartDataset()
{
Label = "Other",
Data = new List<double?> { 28000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[2] },
BorderColor = new List<string> { colors[2] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset3);
chartData = new ChartData
{
Labels = labels,
Datasets = datasets
};
barChartOptions = new();
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.IndexAxis = "y";
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Scales.X.Stacked = true;
barChartOptions.Scales.Y.Stacked = true;
barChartOptions.Plugins.Title!.Text = "Operating system";
barChartOptions.Plugins.Title.Display = true;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
// pass the plugin name to enable the data labels
await barChart.InitializeAsync(chartData: chartData, chartOptions: barChartOptions, plugins: new string[] { "ChartDataLabels" });
}
await base.OnAfterRenderAsync(firstRender);
}
}Locale #
The Locale demo shows how to localize number and date formatting in your bar chart.
How to use:
Refer to the demo code below for a working example and configuration options.
How to use:
- Set the
Localeproperty on theBarChartcomponent to your desired culture code (e.g.,"fr-FR"for French). - Number and date labels in the chart will automatically format according to the specified locale.
- This is useful for displaying charts to users in different regions with appropriate formatting.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var colors = ChartJsColorUtility.CategoricalTwelveColors;
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Label = "Windows",
Data = new List<double?> { 28000, 8000, 2000, 17000 },
BackgroundColor = new List<string> { colors[0] },
BorderColor = new List<string> { colors[0] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset1);
var dataset2 = new BarChartDataset()
{
Label = "macOS",
Data = new List<double?> { 8000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[1] },
BorderColor = new List<string> { colors[1] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset2);
var dataset3 = new BarChartDataset()
{
Label = "Other",
Data = new List<double?> { 28000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[2] },
BorderColor = new List<string> { colors[2] },
BorderWidth = new List<double> { 0 },
};
datasets.Add(dataset3);
chartData = new ChartData { Labels = labels, Datasets = datasets };
barChartOptions = new();
barChartOptions.Locale = "de-DE";
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.IndexAxis = "y";
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Scales.X.Stacked = true;
barChartOptions.Scales.Y.Stacked = true;
barChartOptions.Plugins.Title!.Text = "Operating system";
barChartOptions.Plugins.Title.Display = true;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await barChart.InitializeAsync(chartData, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
}Border radius #
The Border Radius feature allows you to create bar charts with rounded corners, giving your charts a modern and visually appealing look. This is especially useful for dashboards and applications where aesthetics are important.
How to use:
Using the
How to use:
- Add the
BarChartcomponent to your page. - When defining each
BarChartDataset, set theBorderRadiusproperty to a list of pixel values (e.g.,new List<double> { 10 }) to control the roundness of the bar corners. - Optionally, set
BorderSkipped = falseto apply the border radius to all corners of each bar. - Customize other appearance options such as
BackgroundColorandBorderColoras needed. - Refer to the demo code below for a working example and further customization options.
BorderRadius property, you can easily enhance the style of your bar charts to better match your application's design.
<BarChart @ref="barChart" Width="600" />
<div class="mt-5">
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="RandomizeAsync"> Randomize </Button>
</div>
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
private int datasetsCount = 0;
private int labelsCount = 0;
private string[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
private Random random = new();
protected override void OnInitialized()
{
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, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
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(NewRandom());
}
barChartDataset.Data = newData;
newDatasets.Add(barChartDataset);
}
}
chartData.Datasets = newDatasets;
await barChart.UpdateAsync(chartData, barChartOptions);
}
#region Data Preparation
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()
{
var c = ChartJsColorExtensions.ToColor(ChartJsColorUtility.CategoricalTwelveColors[datasetsCount]);
datasetsCount += 1;
return new BarChartDataset()
{
Label = $"Product {datasetsCount}",
Data = GetRandomData(),
BorderColor = new List<string> { ChartJsColorExtensions.ToRgbString(c) },
BackgroundColor = new List<string> { ChartJsColorExtensions.ToRgbaString(c, 0.5) },
BorderWidth = new List<double> { 2 },
BorderRadius = new List<double> { 10 },
BorderSkipped = false
};
}
private List<double?> GetRandomData()
{
var data = new List<double?>();
for (var index = 0; index < labelsCount; index++)
{
data.Add(NewRandom());
}
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 - 1];
}
private double NewRandom() => random.Next(-100, 100);
#endregion Data Preparation
}
Animations - Delay #
The Animations - Delay demo illustrates how to add a delay to the animation of your Bar Chart, making the chart elements appear with a timed entrance effect. This is useful for drawing attention to the chart as it loads or for creating a more engaging user experience.
How to use:
Animation delays help you create visually appealing transitions and can be tailored to fit your application's style.
How to use:
- Add the
BarChartcomponent to your page and define yourLabelsandDatasetsas usual. - Configure the
Options.Animationproperty by setting theDuration(in milliseconds) andDelay(in milliseconds) to control the timing of the animation. - Bind your
chartDataandbarChartOptionsto theBarChartcomponent. - Refer to the demo code below for a working example of how to set up and customize animation delays.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var colors = ChartJsColorUtility.CategoricalTwelveColors;
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Label = "Windows",
Data = new List<double?> { 28000, 8000, 2000, 17000 },
BackgroundColor = new List<string> { colors[3] },
BorderColor = new List<string> { colors[3] },
BorderWidth = new List<double> { 0 }
};
datasets.Add(dataset1);
var dataset2 = new BarChartDataset()
{
Label = "macOS",
Data = new List<double?> { 8000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[4] },
BorderColor = new List<string> { colors[4] },
BorderWidth = new List<double> { 0 }
};
datasets.Add(dataset2);
var dataset3 = new BarChartDataset()
{
Label = "Other",
Data = new List<double?> { 28000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[5] },
BorderColor = new List<string> { colors[5] },
BorderWidth = new List<double> { 0 }
};
datasets.Add(dataset3);
chartData = new ChartData { Labels = labels, Datasets = datasets };
barChartOptions = new();
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Scales.X.Stacked = true;
barChartOptions.Scales.Y.Stacked = true;
barChartOptions.Plugins.Title!.Text = "Operating system";
barChartOptions.Plugins.Title.Display = true;
barChartOptions.Animation = new ChartAnimation { Duration = 1000, Delay = 1500 };
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await barChart.InitializeAsync(chartData, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
}Animations - DataSet level delay #
The Animations - DataSet Level Delay demo demonstrates how to apply different animation delays to each dataset in your Bar Chart. This allows each dataset to animate in sequence, making comparisons clearer and adding a dynamic effect to your data presentation.
How to use:
Dataset-level animation delays are ideal for highlighting differences between series and creating engaging, stepwise chart reveals.
How to use:
- Add the
BarChartcomponent to your page and define yourLabelsand multipleDatasets. - For each dataset, set the
Animationproperty with specificDurationandDelayvalues to control when each dataset animates. - Bind your
chartDataandbarChartOptionsto theBarChartcomponent. - Refer to the demo code below for a practical example of dataset-level animation configuration.
<BarChart @ref="barChart" Width="600" />
@code {
private BarChart barChart = default!;
private BarChartOptions barChartOptions = default!;
private ChartData chartData = default!;
protected override void OnInitialized()
{
var colors = ChartJsColorUtility.CategoricalTwelveColors;
var labels = new List<string> { "Chrome", "Firefox", "Safari", "Edge" };
var datasets = new List<IChartDataset>();
var dataset1 = new BarChartDataset()
{
Label = "Windows",
Data = new List<double?> { 28000, 8000, 2000, 17000 },
BackgroundColor = new List<string> { colors[6] },
BorderColor = new List<string> { colors[6] },
BorderWidth = new List<double> { 0 },
Animation = new ChartAnimation { Duration = 1000, Delay = 500 }
};
datasets.Add(dataset1);
var dataset2 = new BarChartDataset()
{
Label = "macOS",
Data = new List<double?> { 8000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[7] },
BorderColor = new List<string> { colors[7] },
BorderWidth = new List<double> { 0 },
Animation = new ChartAnimation { Duration = 1000, Delay = 1500 }
};
datasets.Add(dataset2);
var dataset3 = new BarChartDataset()
{
Label = "Other",
Data = new List<double?> { 28000, 10000, 14000, 8000 },
BackgroundColor = new List<string> { colors[8] },
BorderColor = new List<string> { colors[8] },
BorderWidth = new List<double> { 0 },
Animation = new ChartAnimation { Duration = 1000, Delay = 2500 }
};
datasets.Add(dataset3);
chartData = new ChartData { Labels = labels, Datasets = datasets };
barChartOptions = new();
barChartOptions.Responsive = true;
barChartOptions.Interaction = new Interaction { Mode = InteractionMode.Y };
barChartOptions.Scales.X!.Title = new ChartAxesTitle { Text = "Visitors", Display = true };
barChartOptions.Scales.Y!.Title = new ChartAxesTitle { Text = "Browser", Display = true };
barChartOptions.Scales.X.Stacked = true;
barChartOptions.Scales.Y.Stacked = true;
barChartOptions.Plugins.Title!.Text = "Operating system";
barChartOptions.Plugins.Title.Display = true;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await barChart.InitializeAsync(chartData, barChartOptions);
}
await base.OnAfterRenderAsync(firstRender);
}
}