Skip to main content

Blazor Polar Area Chart

A Blazor Bootstrap polar area charts are similar to pie charts, but each segment has the same angle - the radius of the segment differs depending on the value.

Blazor Chart Component - Blazor Polar Area Chart

Parameters

NameTypeDefaultRequiredDescriptionAdded Version
Heightint?nullGets or sets chart container height.3.0.0
HeightUnitUnitUnit.PxGets or sets chart container height unit of measure.3.0.0
Widthint?nullGets or sets chart container width.3.0.0
WidthUnitUnitUnit.PxGets or sets chart container width unit of measure.3.0.0

Methods

NameReturn typeDescriptionAdded Version
AddDataAsync(ChartData chartData, string dataLabel, IChartDatasetData data)Task<ChartData>Adds data to chart.3.0.0
AddDataAsync(ChartData chartData, string dataLabel, List<IChartDatasetData> data)Task<ChartData>Adds dataset to chart.3.0.0
AddDatasetAsync(ChartData chartData, IChartDataset chartDataset, IChartOptions chartOptions)Task<ChartData>Adds dataset to chart.3.0.0
InitializeAsync(ChartData chartData, IChartOptions chartOptions, string[]? plugins = null)TaskInitialize the chat.3.0.0
ResizeAsync(int width, int height, Unit widthUnit = Unit.Px, Unit heightUnit = Unit.Px)TaskResize the chart.3.0.0
UpdateAsync(ChartData chartData, IChartOptions chartOptions)TaskUpdate the chart.3.0.0

ChartData Members

Property NameTypeDefaultRequiredDescriptionAdded Version
DatasetsList<IChartDataset>null✔️Gets or sets the Datasets.3.0.0
LabelsList<string>null✔️Gets or sets the Labels.3.0.0

PolarAreaChartDataset Members

info

PolarAreaChartDataset implements IChartDataset and inherits from the parent class ChartDataset.

Property NameTypeDefaultRequiredDescriptionAdded Version
BackgroundColorList<string>?nullArc background color.3.0.0
BorderAlignList<string>?nullSupported values are 'center' and 'inner'. When 'center' is set, the borders of arcs next to each other will overlap. When 'inner' is set, it is guaranteed that all borders will not overlap.3.0.0
BorderColorList<string>?nullArc border color.3.0.0
BorderDashList<double>?nullArc border length and spacing of dashes.3.0.0
BorderDashOffsetdouble0.0Arc border offset for line dashes.3.0.0
BorderJoinStyleList<string>?nullArc border join style. Supported values are 'round', 'bevel', 'miter'.3.0.0
BorderWidthList<double>?nullArc border width (in pixels).3.0.0
CircularbooltrueBy default the Arc is curved. If false, the Arc will be flat.3.0.0
Clipstring?nullHow to clip relative to chartArea. Positive value allows overflow, negative value clips that many pixels inside chartArea. 0 = clip at chartArea. Clipping can also be configured per side: clip: {left: 5, top: false, right: -2, bottom: 0}3.0.0
DataList<double?>?null✔️Get or sets the Data.3.0.0
DatalabelsPieChartDatasetDataLabelsGet or sets the data labels3.0.0
HiddenboolfalseConfigures the visibility state of the dataset. Set it to true, to hide the dataset from the chart.3.0.0
HoverBackgroundColorList<string>?nullArc background color when hovered.3.0.0
HoverBorderColorList<string>?nullArc border color when hovered.3.0.0
HoverBorderDashList<double>?nullArc border length and spacing of dashes when hovered.3.0.0
HoverBorderDashOffsetdouble?nullArc border offset for line dashes when hovered.3.0.0
HoverBorderJoinStyleList<string>?nullArc border join style when hovered. Supported values are 'round', 'bevel', 'miter'.3.0.0
HoverBorderWidthList<double>?nullArc border width when hovered (in pixels).3.0.0
Labelstring?nullThe label for the dataset which appears in the legend and tooltips.3.0.0
Orderint0The drawing order of dataset. Also affects order for stacking, tooltip and legend.3.0.0

PolarAreaChartDatasetDataLabels Members

info

PolarAreaChartDatasetDataLabels inherits from the parent class ChartDatasetDataLabels.

Property NameTypeDefaultRequiredDescriptionAdded Version
AlignmentAlignmentAlignment.NoneGets or sets the data labels alignment.3.0.0
AnchorAnchorAnchor.NoneGets or sets the data labels anchor.3.0.0
BorderWidthdouble2Gets or sets the border width3.0.0

PolarAreaChartOptions Members

info

PolarAreaChartOptions inherits from the parent class ChartOptions.

Property NameTypeDefaultRequiredDescriptionAdded Version
Localestring?Gets or sets the locale. By default, the chart is using the default locale of the platform which is running on.3.0.0
MaintainAspectRatiobooltrueMaintain the original canvas aspect ratio (width / height) when resizing.3.0.0
PluginsPolarAreaChartPluginsGets or sets the Plugins.3.0.0
ResponsiveboolfalseResizes the chart canvas when its container does.3.0.0

Examples

Prerequisites

Refer to the getting started guide for setting up charts.

How it works

In the following example, a categorical 12-color palette is used.

tip

For data visualization, you can use the predefined palettes ColorBuilder.CategoricalTwelveColors for a 12-color palette and ColorBuilder.CategoricalSixColors for a 6-color palette. These palettes offer a range of distinct and visually appealing colors that can be applied to represent different categories or data elements in your visualizations.

Blazor Bootstrap: Polar Area Chart Component - How it works
<PolarAreaChart @ref="polarAreaChart" Width="600" />

<div class="mt-5">
<button class="btn btn-primary btn-sm" @onclick="async () => await RandomizeAsync()"> Randomize </button>
<button class="btn btn-primary btn-sm" @onclick="async () => await AddDatasetAsync()"> Add Dataset </button>
<button class="btn btn-primary btn-sm" @onclick="async () => await AddDataAsync()">Add Data</button>
</div>
@code {
private PolarAreaChart polarAreaChart = default!;
private PolarAreaChartOptions polarAreaChartOptions = default!;
private ChartData chartData = default!;
private string[]? chartColors;

private int datasetsCount = 0;
private int dataLabelsCount = 0;

private Random random = new();

protected override void OnInitialized()
{
chartColors = ColorUtility.CategoricalTwelveColors;
chartData = new ChartData { Labels = GetDefaultDataLabels(5), Datasets = GetDefaultDataSets(1) };
polarAreaChartOptions = new();
polarAreaChartOptions.Responsive = true;
polarAreaChartOptions.Plugins.Title!.Text = "2022 - Sales";
polarAreaChartOptions.Plugins.Title.Display = true;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await polarAreaChart.InitializeAsync(chartData, polarAreaChartOptions);
}
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 PolarAreaChartDataset polarAreaChartDataset
&& polarAreaChartDataset is not null
&& polarAreaChartDataset.Data is not null)
{
var count = polarAreaChartDataset.Data.Count;

var newData = new List<double?>();
for (var i = 0; i < count; i++)
{
newData.Add(random.Next(0, 100));
}

var backgroundColors = new List<string>();
for (var index = 0; index < count; index++)
{
backgroundColors.Add(chartColors![index].ToColor().ToRgbaString(0.5)); // RGBA
}

polarAreaChartDataset.Data = newData;
polarAreaChartDataset.BackgroundColor = backgroundColors;
newDatasets.Add(polarAreaChartDataset);
}
}

chartData.Datasets = newDatasets;

await polarAreaChart.UpdateAsync(chartData, polarAreaChartOptions);
}

private async Task AddDatasetAsync()
{
if (chartData is null || chartData.Datasets is null) return;

var chartDataset = GetRandomPolarAreaChartDataset();
chartData = await polarAreaChart.AddDatasetAsync(chartData, chartDataset, polarAreaChartOptions);
}

private async Task AddDataAsync()
{
if (dataLabelsCount >= 12)
return;

if (chartData is null || chartData.Datasets is null)
return;

dataLabelsCount += 1;

var data = new List<IChartDatasetData>();
foreach (var dataset in chartData.Datasets)
{
if (dataset is PolarAreaChartDataset polarAreaChartDataset)
data.Add(new PolarAreaChartDatasetData(polarAreaChartDataset.Label, random.Next(0, 100), chartColors![dataLabelsCount - 1].ToColor().ToRgbaString(0.5), null));
}

chartData = await polarAreaChart.AddDataAsync(chartData, GetNextDataLabel(), data);
}

#region Data Preparation

private List<string> GetDefaultDataLabels(int numberOfLabels)
{
var labels = new List<string>();
for (var index = 0; index < numberOfLabels; index++)
{
dataLabelsCount += 1;
labels.Add(GetNextDataLabel());
}

return labels;
}

private string GetNextDataLabel() => $"Product {dataLabelsCount}";

private List<IChartDataset> GetDefaultDataSets(int numberOfDatasets)
{
var datasets = new List<IChartDataset>();

for (var index = 0; index < numberOfDatasets; index++)
{
datasets.Add(GetRandomPolarAreaChartDataset());
}

return datasets;
}

private PolarAreaChartDataset GetRandomPolarAreaChartDataset()
{
datasetsCount += 1;
return new()
{
Label = $"Team {datasetsCount}",
Data = GetRandomData(),
BackgroundColor = GetRandomBackgroundColors()
};
}

private List<double?> GetRandomData()
{
var data = new List<double?>();
for (var index = 0; index < dataLabelsCount; index++)
{
data.Add(random.Next(0, 100));
}

return data;
}

private List<string> GetRandomBackgroundColors()
{
var colors = new List<string>();
for (var index = 0; index < dataLabelsCount; index++)
{
colors.Add(chartColors![index].ToColor().ToRgbaString(0.5)); // RGBA
}

return colors;
}

private List<string> GetRandomBorderColors()
{
var colors = new List<string>();
for (var index = 0; index < dataLabelsCount; index++)
{
colors.Add(chartColors![index].ToColor().ToRgbString()); // RGB
}

return colors;
}

#endregion Data Preparation
}

See the demo here.