To create a chart, follow these steps:
- Add the specific chart namespace (e.g.,
@using ChartJs.Blazor.PieChart) to your component. - Add the
<Chart> component to your markup, passing a configuration object to the Config parameter. - Initialize the configuration object in the
@code block, defining Options, Data.Labels, and Data.Datasets.
@using ChartJs.Blazor
@using ChartJs.Blazor.PieChart
<Chart Config="_config"></Chart>
@code {
private PieConfig _config;
protected override void OnInitialized()
{
_config = new PieConfig
{
Options = new PieOptions
{
Responsive = true,
Title = new OptionsTitle
{
Display = true,
Text = "ChartJs.Blazor Pie Chart"
}
}
};
foreach (string color in new[] { "Red", "Yellow", "Green", "Blue" })
{
_config.Data.Labels.Add(color);
}
PieDataset<int> dataset = new PieDataset<int>(new[] { 6, 5, 3, 7 })
{
BackgroundColor = new[]
{
ColorUtil.ColorHexString(255, 99, 132),
ColorUtil.ColorHexString(255, 205, 86),
ColorUtil.ColorHexString(75, 192, 192),
ColorUtil.ColorHexString(54, 162, 235),
}
};
_config.Data.Datasets.Add(dataset);
}
}