Pie chart
Since 0.1.0-alpha.22Use a pie when the point is how one total splits, and there are five or fewer parts with clearly different shares. People judge angles poorly. To compare similar shares or rank them, use a bar chart.
A donut with the total
Give PieChart the Items, each slice's name as Argument, its size as Value, and a Title. Donut cuts a hole in the middle, and Center fills it, here with the total.
Static
Preview
2,544
subscriptions
- Starter 1,284 50%
- Pro 862 34%
- Team 341 13%
- Enterprise 57 2.2%
| Plan | Subscriptions | Share |
|---|---|---|
| Starter | 1,284 | 50% |
| Pro | 862 | 34% |
| Team | 341 | 13% |
| Enterprise | 57 | 2.2% |
Code
@* Paying subscriptions by plan, with the total in the middle. *@
<PieChart Items="_plans" Argument="p => p.Plan" Value="p => p.Subscriptions" Title="Subscriptions by plan" Donut>
<Center>
<TextBlock Size="TextSize.Xl2" Weight="FontWeight.Semibold" TabularNums>_plans.Sum(p => p.Subscriptions).ToString("N0")</TextBlock>
<TextBlock Size="TextSize.Sm" Color="Color.Muted">subscriptions</TextBlock>
</Center>
</PieChart>
{
private record PlanCount(string Plan, int Subscriptions);
private readonly PlanCount[] _plans =
[
new("Starter", 1_284),
new("Pro", 862),
new("Team", 341),
new("Enterprise", 57),
];
}Add this example to a page with the izzy CLI:
izzy add component PieChart --at <file>:<line>The legend names each slice with its value and share, beside the ring when there's room and under it when there isn't. Point at a slice for its tooltip.
A pie with its slices named
To write each label just outside its slice, joined to it by a short line, set Labels="ChartLabels.Outside".
Static
Preview
| Source | Visits | Share |
|---|---|---|
| Organic search | 48,210 | 53% |
| Direct | 21,940 | 24% |
| Referral | 9,315 | 10% |
| Social | 6,870 | 7.6% |
| 4,122 | 4.6% |
Code
@* Last month's visits by where they came from, each slice named beside it. *@
<PieChart Items="_sources" Argument="s => s.Source" Value="s => s.Visits" Title="Visits by source, October" ValueFormat="N0" Labels="ChartLabels.Outside" Legend="ChartLegend.None" StartAngle="180" />
{
private record TrafficSource(string Source, int Visits);
private readonly TrafficSource[] _sources =
[
new("Organic search", 48_210),
new("Direct", 21_940),
new("Referral", 9_315),
new("Social", 6_870),
new("Email", 4_122),
];
}Slices run clockwise from the top in item order, so sort the items largest first. StartAngle turns the circle, here by 180° so the largest slice fills the left half.
A label is the slice's share, and the legend names the slices. Here Legend="ChartLegend.None", so each label names its slice too. Labels that would collide are nudged apart.
ChartLabels.Inside writes each share in its slice instead, and a slice too thin for it gets its label outside. To write the labels yourself, such as "English: 56%" beside a legend, set LabelTemplate.
Under 30rem there's no room outside the circle, so the labels are left out and the legend names the slices.
A half donut
Sweep sets how far round the slices go, in degrees: 180 is a half circle. With StartAngle="-90" they start at the left and run over the top to the right, and the chart takes only the half's height.
Static
Preview
326 for a majority
- Labour 411 63%
- Conservative 121 19%
- Liberal Democrat 72 11%
- Other 46 7.1%
| Party | Seats | Share |
|---|---|---|
| Labour | 411 | 63% |
| Conservative | 121 | 19% |
| Liberal Democrat | 72 | 11% |
| Other | 46 | 7.1% |
Code
@* Seats in the House of Commons after the July 2024 election, as a half ring. *@
<PieChart Items="_parties" Argument="p => p.Party" Value="p => p.Seats" Title="House of Commons seats, 2024" ValueFormat="N0"
Donut="true" StartAngle="-90" Sweep="180">
<Center><strong>650 seats</strong><br />326 for a majority</Center>
</PieChart>
{
private record PartySeats(string Party, int Seats);
private readonly PartySeats[] _parties =
[
new("Labour", 411),
new("Conservative", 121),
new("Liberal Democrat", 72),
new("Other", 46),
];
}A donut's Center sits in the hole, on the flat edge.
A half circle suits a whole that has a midpoint people read against, as a majority here. For most breakdowns a whole circle, or bars, compare better.
Details
The legend beside it (under it, when narrow) names each slice with its value and share.
Slices go clockwise from the top (or StartAngle) in item order, in the palette's eight colors, then gray. Missing, zero and negative values get no slice but keep their row in the legend and the table. Past about six slices they get hard to tell apart: group the smallest as "Other", or use a Chart with bars.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| A pie, or a donut with content in its middle | Yes | Yes |
| A half circle, or any part of one, that takes only its own height | Yes | Yes |
| A legend with each slice's value and share, beside the circle or under it | Yes | Yes |
| Legend items that hide and show slices, the rest closing up | Yes | Yes |
| Labels by their slices with short lines, or in the slices, beside a legend or in its place | Yes | Yes |
| Labels you write with a template | Yes | Yes |
| A table of the values behind a "Show data" button | Yes | Yes |
| Tooltips on hover and from the keyboard | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off.
- Legend items that hide and show slices, the rest closing up: waits for the script
- Tooltips on hover and from the keyboard: waits for the script
Accessibility
- The chart is a figure captioned by
Title, or named by a heading on the page withTitledBy. - The circle is one image named by the caption.
- The legend is text: each slice's name, value and share, read in order.
- "Show data" under the chart opens a table of every value and share (
DataTable). - With script, the circle is one Tab stop: the arrow keys move between slices and say each one.
Keyboard navigation
On the circle
| Press this key | To do this |
|---|---|
| Right or Down | Show the next slice's tooltip |
| Left or Up | Show the slice before |
| Home or End | Go to the first or last slice |
| Enter | Open the slice's level below (with ChartDrillLevels) |
| Escape | Hide the tooltip |
Parameters
| Prop | Type | Default |
|---|---|---|
ChildContentChartDrillLevel<T>s, from the top, for slices that drill down: each level's groups are the slices (the level's argument in place of Argument), their values added up by Aggregate, and selecting one shows the next level. | RenderFragment? | |
DrillWith drill-down levels: the page address's query value that holds the level ("drill" by default; give each chart that drills on a page its own). | string? | |
HideBreadcrumbWith drill-down levels: leaves out the breadcrumb above the chart. | bool | false |
AggregateWith drill-down levels, how each slice adds up its items' values: Sum by default, or Avg, Min, Max or Count. | DataAggregate | DataAggregate.Sum |
ValueEach slice's size, such as p => p.Customers: any number. | Expression<Func<TItem, object?>> | Required |
DonutDraws a ring with a hole in the middle, for Center. | bool | false |
CenterContent in a donut's hole, such as the total. | RenderFragment? | |
LabelsLabels each slice on the chart. | ChartLabels | ChartLabels.None |
LabelTemplateWrites each label yourself, such as "English: 56%" or a value, from the slice's name, value and share (PieLabelContext<T>), in its slice or outside it. | RenderFragment<PieLabelContext<TItem>>? | |
ItemsThe items, one slice each (in each ring, on a NestedDonutChart<T>). | IEnumerable<TItem>? | Required |
ArgumentEach slice's name, such as p => p.Plan. | Expression<Func<TItem, object?>>? | |
TitleThe chart's title, such as "Customers by plan", shown above it as its caption. | string? | |
HideTitleHides the title, which still names the chart for screen readers. | bool | false |
DownloadShows a button across from the title that saves the chart as a PNG, named after its title, in the color scheme on screen. | bool | false |
TitleAlignWhere the title sits: at the start, over the chart's first label, by default (the left, in a left-to-right page); ChartAlign.Center or ChartAlign.End. | ChartAlign | ChartAlign.Start |
TitledByThe id of a heading already on the page that titles the chart, such as a card's, in place of Title: the chart takes its name from it and shows no title of its own. | string? | |
StartAngleWhere the first slice starts, in degrees clockwise from the top: 90 starts it at the right, so it runs down that side. | double | 0 |
SweepHow far round the slices go, in degrees: 360 (the default) for a whole circle, 180 for a half. | double | 360 |
SizeThe circle's largest size. | Length? | computed |
ValueFormatA .NET format string for the values, such as C0 or N0. | string? | |
TooltipShows a tooltip with the slice's value and share under the pointer or keyboard focus, with script. | bool | true |
PaletteThe colors in order: Colors (the theme's eight, the default), Shades of one color for slices in an order, Diverging for slices either side of a middle, or your own with Of. | ChartPalette? | |
LegendThe legend: each slice's name by its color, with its value and share on a PieChart<T> (names only on a NestedDonutChart<T>, whose values differ ring by ring). | ChartLegend | ChartLegend.Auto |
LegendAlignWhere the legend sits along its edge: centered on the circle, by default (beside it or under it); ChartAlign.Start for the left above or below (in a left-to-right page) or the top beside it, ChartAlign.End for the right or the bottom. | ChartAlign? | |
DataTableThe values as a table: by default behind a "Show data" button that opens it over the chart; ChartDataTable.Collapsed puts it in a disclosure under the chart. | ChartDataTable | ChartDataTable.Popover |
RefreshHow often the chart reads its data again, at least a second, such as Refresh="TimeSpan.FromSeconds(30)". | TimeSpan? | |
RefreshFromOn a static page with Refresh: a lighter page to fetch than this one, which renders the same chart with the same id. | string? | |
OnRefreshOn an interactive page with Refresh: raised at each interval, to load new data. | EventCallback |
Plus 17 item props and 4 common props.
Full APISee also
- Chart: draws a chart on the server from a list of items, with axes, value labels, reference lines, trends and a table of the values, for every chart type.
- Gauge: shows one value against its range, such as progress toward a goal.