PieChart
elements man ui/templates/chart-piePie and donut charts with labels, a center value, an active slice and stacked rings.
Exampleimport { PieChart } from "@elements/ui/chart-pie"; import type { ChartConfig } from "@elements/ui/chart"; const config: ChartConfig = { chrome: { label: "Chrome", color: "var(--chart-1)" }, safari: { label: "Safari", color: "var(--chart-2)" }, }; <PieChart data={rows} {config} value="visitors" name="browser" /> <PieChart data={rows} {config} value="visitors" name="browser" innerRadius={60} centerValue="1,125" centerLabel="Visitors" />
Parts
| Part | What it is |
|---|---|
PieChart |
The template. One slice per row, or one ring per value key. |
.chart-pie-slice |
One slice (an SVG path) and its own hit target; .is-active when active. |
.chart-pie-ring |
The ring outside the active slice with activeShape=ring. |
.chart-center |
The donut's center text: .chart-center-value and .chart-center-label. |
.chart |
The figure. Declares --color-<key> for each config entry; keeps a 16:9 box unless sized by class. |
.chart-plot |
The focusable plot area holding the SVG; one tab stop. |
.chart-tooltip |
The tooltip: heading, one row per item with swatch, name and value, optional footer. |
.chart-legend |
The legend row; .chart-legend-item per series, a button with legend toggle. |
.chart-table |
The data as a table, visually hidden unless showTable. |
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
data |
Row[] |
(required) | One row per slice. A row's fill field colors its slice. A row is any object, typed with an interface, a type or a class (see Chart (opens in a new tab)). |
config |
ChartConfig |
(required) | The chart config: per data key a label, a color (any CSS color, usually var(--chart-N)) or a theme { light, dark }, and an optional icon; legendType and tooltipType; strokeDasharray and strokeOpacity dash and fade a line or an area's edge. Each key becomes --color-<key> on the chart. |
value |
string | string[] |
(required) | The value field. Several draw concentric rings from the same rows, the first innermost. |
name |
string |
"" |
The name field: tooltip and legend names and colors through the config. |
innerRadius |
number | string |
0 |
px, or a percent of the radius. Above 0 draws a donut. |
outerRadius |
number | string |
"80%" |
px, or a percent of the largest radius that fits. |
maxRadius |
number |
0 |
The most the outer radius may be, px; 0 for no cap. |
startAngle |
number |
0 |
Degrees, counter-clockwise from three o'clock. |
endAngle |
number |
360 |
180 with startAngle 0 draws a half pie. |
paddingAngle |
number |
0 |
Degrees left between slices. |
minAngle |
number |
0 |
The least angle of a slice with a value, degrees. |
cornerRadius |
number |
0 |
Rounds each slice's corners, px. |
separator |
number |
-1 |
The gap drawn between slices in the surface color, px. -1 follows the theme; 0 is none. |
ringGap |
number |
10 |
px between rings. |
hide |
boolean | string[] |
false |
Rings not drawn: true for all, or their value keys; the others keep their places. |
layout |
"centric" | "radial" |
"centric" |
The chart layout. A pie places its slices by its own angles and radii, so both draw the same. |
labels |
boolean | { position?: "outside" | "inside"; key?: string; formatter?: (value, row) => string; line?: boolean; offset?: number } |
false |
Values or another field outside each slice on a leader line, or inside it. |
centerValue |
string |
"" |
Large text in the donut's hole. |
centerLabel |
string |
"" |
Small text under the center value. |
activeShape |
"none" | "grow" | "ring" |
"none" |
Draw the active slice grown by activeOffset, or grown with a ring outside it. |
inactiveShape |
"none" | "dim" |
"none" |
dim fades every slice but the active one. |
margin |
{ top?, right?, bottom?, left? } |
{} |
Space around the plot in px. |
cx, cy |
number | string, number | string |
"50%", "50%" |
The center of the pie: px from the left and top edges, or a percentage of the plot. |
activeOffset |
number |
10 |
px the active slice grows. |
tooltip |
boolean | { indicator?: "dot" | "line" | "dashed"; hideLabel?: boolean; hideIndicator?: boolean; color?: string; labelKey?: string; nameKey?: string; labelFormatter?: (label, row) => string; formatter?: (value, key, row) => string; footer?: (row) => string; footerLabel?: string; unit?: string; defaultIndex?: number; cursor?: boolean; filterNull?: boolean; trigger?: "hover" | "click"; active?: boolean; position?: { x?, y? }; reverseDirection?: { x?, y? }; itemSorter?: "name" | "value" | "key" | (item) => number | string; includeHidden?: boolean; animationDuration?: number; animationEasing?: string | (t) => number; isAnimationActive?: boolean } |
true |
The tooltip: false turns it off; an object sets the indicator, hides the heading or swatches, takes names and headings from other keys, formats them, adds a footer line, or turns off the hover cursor. More options: trigger click (a click shows it and it stays), active (always or never shown), position (a fixed spot), reverseDirection, itemSorter and includeHidden. |
legend |
boolean | { position?: "top" | "bottom" | "left" | "right" | "center" | "insideTop" | "insideBottom" | "insideLeft" | "insideRight" | "insideTopLeft" | "insideTopRight" | "insideBottomLeft" | "insideBottomRight" | { x, y }; offset?: number; iconSize?: number; verticalAlign?: "top" | "middle" | "bottom"; align?: "left" | "center" | "right"; layout?: "horizontal" | "vertical"; iconType?: "square" | "rect" | "circle" | "line" | "plainline" | "diamond" | "triangle" | "cross" | "star" | "wye" | "none"; formatter?: (name, key) => string; itemSorter?: "name" | "key" | (item) => number | string; hideIcon?: boolean; nameKey?: string; toggle?: boolean } |
false |
A legend of the slices, named through the config by the name field; toggle hides slices. |
active |
number (two-way) |
-1 |
The active slice (ring index times the row count plus the row index). Hover and arrows move it; leaving restores it. |
hidden |
string[] (two-way) |
[] |
Slice names left out; the others fill the circle. |
label |
string |
"Pie chart" |
The chart's accessible name and its table's caption. |
animate |
boolean |
true |
Marks enter with motion and move to new values. Off under prefers-reduced-motion. |
animation |
{ begin?: number; duration?: number; easing?: string | (t) => number; matchBy?: "index" | "append" | (item, index) => key; interpolate?: (items, t) => items; newValues?: boolean } |
{} |
Times the entry and the move to new data. matchBy pairs old and new items by position or by a key, matchByDataKey(field) from the chart package; interpolate draws every frame of a change from the paired items and the eased time; newValues false shows an unpaired new point at once. |
showTable |
boolean |
false |
Show the chart's data table under the plot. The table is always in the accessibility tree. |
class |
string |
"" |
Classes on the figure. Size the chart here: it keeps a 16:9 box unless given a height or aspect-ratio. |
attrs |
Record<string, string | number | boolean | undefined> |
{} |
Any other attributes for the figure: id, data-testid, aria-*. A style here is added after the chart's color variables, so style: "height: 13rem" sizes the figure. |
syncId |
string |
"" |
Charts with the same syncId share the active item and its tooltip. |
syncMethod |
"index" | "value" |
"index" |
Match the shared item by position, or by its category or name. |
onActiveChange |
(index: number) => void |
() => {} |
The user moved the active item (hover, leave or keys). |
onItemClick |
(index: number) => void |
() => {} |
A click or tap on an item, with its index. |
onHiddenChange |
(hidden: string[]) => void |
() => {} |
A legend toggle changed hidden. |
Keyboard
| Keys | Action |
|---|---|
Tab |
Focuses the chart (one tab stop) and shows the tooltip for the item the keys last reached, else the active item, else the first. |
ArrowRight / ArrowDown |
Next item. The same under dir=rtl: the plot is not mirrored, so keys follow the screen. |
ArrowLeft / ArrowUp |
Previous item. |
Home / End |
First and last item. |
Escape |
Hides the tooltip. In a dialog, the dialog stays open until the next Escape. |
Data attributes
| Attribute | Values | On |
|---|---|---|
data-chart |
"bar" | "line" | "area" | "pie" | "radar" | "radial" |
figure |
data-state |
"open" | "closed" |
.chart-tooltip |
data-index |
the item index | .chart-hit and pie, radial marks |
data-series |
the series key | marks |
data-resizing |
present while the chart redraws for a new size | figure |
data-entered |
present once the entry motion is over | figure |
aria-pressed |
true | false | .chart-legend-item (toggle) |
Accessibility
- The plot is one tab stop: role=group, aria-roledescription=chart, named by label, described by the data table.
- The SVG is aria-hidden; a screen reader gets the data from the table, which every chart renders.
- Arrow keys move through the items and each move is announced in a polite live region, for example "February: Desktop 305, Mobile 200".
- Identity is never color alone: a legend for two or more series and labels on the tooltip rows.
- Legend toggles are buttons with aria-pressed.
See also
- Chart (opens in a new tab): The config and its colors, the tooltip, the legend, the palette and the data table that every chart shares.
- BarChart (opens in a new tab): Vertical, horizontal, stacked and grouped bars with tooltip, legend and labels.
- LineChart (opens in a new tab): Lines over categories with dots, curves, labels and a crosshair tooltip.
- AreaChart (opens in a new tab): Filled areas, stacked or expanded, solid or gradient, with a crosshair tooltip.
- RadarChart (opens in a new tab): Values on radial axes, one polygon per series, over a polygon or circle grid.
- RadialChart (opens in a new tab): Radial bars and gauges, with tracks, labels along the arc and a center value.