
Change the chart to a pie
chart by setting ChartType
Demo
Setting the height and width will automatically disable the constraint chart ratio, and the chart will fill the container
Demo
@page "/charts/pie"
@inject IStringLocalizer<Pie> Localizer
<DemoBlock Title="@Localizer["P1"]" Introduction="@Localizer["P2"]" Name="Pie">
<Chart ChartType="ChartType.Pie" OnInitAsync="@OnInit" OnAfterInitAsync="@OnAfterInit" OnAfterUpdateAsync="@OnAfterUpdate" @ref="PieChart" />
<div class="text-center mt-2 chart">
<div class="btn-group">
<button class="btn btn-primary" @onclick="e => Utility.RandomData(PieChart)"><i class="fa-solid fa-chart-pie"></i><span>@Localizer["P3"]</span></button>
<button class="btn btn-primary" @onclick="OnReloadChart"><i class="fa-solid fa-chart-column"></i><span>@Localizer["Reload"]</span></button>
<button class="btn btn-primary" @onclick="e => Utility.AddDataSet(PieChart, ref PieDatasetCount)"><i class="fa-solid fa-circle-plus"></i><span>@Localizer["P4"]</span></button>
<button class="btn btn-primary" @onclick="e => Utility.RemoveDataSet(PieChart, ref PieDatasetCount)"><i class="fa-solid fa-circle-minus"></i><span>@Localizer["P5"]</span></button>
<button class="btn btn-primary" @onclick="e => Utility.AddData(PieChart, ref PieDataCount)"><i class="fa-solid fa-plus"></i><span>@Localizer["P6"]</span></button>
<button class="btn btn-primary" @onclick="e => Utility.RemoveData(PieChart, ref PieDataCount)"><i class="fa-solid fa-minus"></i><span>@Localizer["P7"]</span></button>
</div>
</div>
<BlockLogger @ref="Logger" class="mt-3" />
</DemoBlock>
<DemoBlock Title="@Localizer["AspectRatio"]" Introduction="@Localizer["AspectRatioIntro"]" Name="BarAspectRatio">
<Chart ChartType="ChartType.Bar" OnInitAsync="@OnInit" Height="500px" Width="300px" />
</DemoBlock>
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace BootstrapBlazor.Shared.Samples.Charts;
/// <summary>
///
/// </summary>
public partial class Pie
{
private Random Randomer { get; } = new Random();
private int PieDatasetCount = 1;
private int PieDataCount = 5;
[NotNull]
private Chart? PieChart { get; set; }
[NotNull]
private BlockLogger? Logger { get; set; }
/// <summary>
///
/// </summary>
/// <param name="firstRender"></param>
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
if (firstRender)
{
Logger.Log("Pie loading data ...");
}
}
private Task OnAfterInit()
{
Logger.Log("Pie initialization is complete");
return Task.CompletedTask;
}
private Task OnAfterUpdate(ChartAction action)
{
Logger.Log($"Pie Figure update data operation completed -- {action}");
return Task.CompletedTask;
}
private Task<ChartDataSource> OnInit()
{
var ds = new ChartDataSource();
ds.Options.Title = "Pie chart";
ds.Labels = Utility.Colors.Take(PieDataCount);
for (var index = 0; index < PieDatasetCount; index++)
{
ds.Data.Add(new ChartDataset()
{
Label = $"Set {index}",
Data = Enumerable.Range(1, PieDataCount).Select(i => Randomer.Next(20, 37)).Cast<object>()
});
}
return Task.FromResult(ds);
}
/// <summary>
/// 强刷控件,重新初始化控件外观
/// </summary>
private Task OnReloadChart()
{
PieDataCount = Randomer.Next(5, 15);
PieChart?.Reload();
return Task.CompletedTask;
}
}