Parts of one whole as slices, for a few parts, with the share of each on request.
| Value | Share | |
|---|---|---|
| Organic search | 48K | 43.4% |
| Direct | 29K | 26.2% |
| Referral | 16K | 14.8% |
| Social | 11K | 10.1% |
| 6.1K | 5.5% |
Give it rows of data with a name and a value. Each row is a slice, clockwise from 12 o'clock in the order of the data, in the next colour of the chart palette. Slices keep a 2px gap between them and have softly rounded corners. A pie reads best with a few parts; with more than eight, the seven largest keep their slices and the rest fold into one grey "Other".
Point at a slice, tap it or tab to the chart and use the arrow keys: the slice lifts out, the others fade back and a tooltip gives its share and value. The legend hides a slice when pressed and spotlights it when pointed at. The slices sweep in from 12 o'clock the first time the chart is on screen, and turn to their new angles when the data changes.
Pie Chart is part of the Fabricator style. It's drawn in SVG, so the same component ships for every base, and its layout comes from the same model as the React Native version. For the total in the centre and a list of values, use Donut Chart.
Installation#
bunx --bun fabricator-ui@latest add pie-chart
Usage#
import { PieChart } from "@/components/ui/pie-chart"const data = [
{ source: "Organic search", visits: 48_200 },
{ source: "Direct", visits: 29_100 },
{ source: "Referral", visits: 16_400 },
]
<PieChart
data={data}
nameKey="source"
valueKey="visits"
label="Traffic by source"
/>nameKey and valueKey name the fields to read, and default to name and value. A missing, zero or negative value leaves its slice out, and the hidden table still lists it. config is optional: keyed by a slice's name, it gives that slice a label and a color. The chart is square and fills its container's width up to 18rem.
Examples#
Labels#
labels prints each slice's share inside it, on a small pill in the card's colour so it reads on any slice. A slice too thin to hold its label leaves it to the tooltip and the table.
| Value | Share | |
|---|---|---|
| 412 | 44.3% | |
| Chat | 318 | 34.2% |
| Phone | 126 | 13.5% |
| Community | 74 | 8% |
Many parts#
With more than eight slices, the seven largest keep their place and colour and the rest fold into one "Other" slice in grey, last. Its tooltip gives the folded rows' total, and the hidden table lists each of them.
| Value | Share | |
|---|---|---|
| US | 4,210 | 36.7% |
| GB | 1,830 | 15.9% |
| DE | 1,460 | 12.7% |
| FR | 980 | 8.5% |
| CA | 870 | 7.6% |
| BR | 640 | 5.6% |
| JP | 590 | 5.1% |
| ES | 310 | 2.7% |
| NL | 280 | 2.4% |
| SE | 190 | 1.7% |
| PL | 120 | 1% |
Colours#
Slices take the chart palette in the order of the data. Give one its own with config[name].color: a palette slot, an upstream chart token (chart-1 … chart-5), a role (primary, info, success, …), a CSS variable (var(--brand)) or any CSS colour.
| Token | Hue | Token | Hue |
|---|---|---|---|
--chart-series-1 | blue | --chart-series-5 | magenta |
--chart-series-2 | orange | --chart-series-6 | green |
--chart-series-3 | aqua | --chart-series-7 | violet |
--chart-series-4 | amber | --chart-series-8 | red |
The palette ships with the foundations, chosen separately for light and dark, every slot at 3:1 or more against the page and the card. Slots never repeat, which is why a ninth part folds into "Other" in muted-foreground. Labels on slices stay in the text colour on a card-coloured pill.
Motion#
- Sweep: the first time the chart scrolls into view, the slices sweep round from 12 o'clock on the
revealrole, once per mount. - Morph: when
data,configor the visible slices change, each slice turns to its new angles on themoverole. A new slice grows from zero width where it will sit; one being hidden shrinks to nothing. - Lift: the slice under the pointer moves a few pixels out along its middle on the
gliderole while the others fade back.
All three follow the motion speed of the region they sit in. Reduce Motion and the instant speed draw the chart at once and keep only the fades; animate={false} does the same for one chart.
API Reference#
PieChart#
All other props go to the root <div>.
| Prop | Type | Default |
|---|---|---|
data | TDatum[] | required |
label | string | required: the accessible name |
nameKey | keyof TDatum | "name" |
valueKey | keyof TDatum | "value" |
config | ChartConfig, keyed by slice name | names and the palette |
labels | boolean | false |
formatValue | (value: number, key: string) => string | 1,234 |
legend | boolean | on for two or more slices |
tooltip | boolean | true |
animate | boolean | true |
loading | boolean | false |
empty | ReactNode | "No data" |
onActiveChange | (active: ChartActive<TDatum> | null) => void | - |
formatValue receives valueKey as its key. onActiveChange reports the inspected slice's row, with its name as key; for "Other" it reports the largest folded row and the key __other.
Data attributes#
| Attribute | Element | Present when |
|---|---|---|
data-slot="pie-chart" | root | always |
data-loading | root | loading is true |
data-slot="chart-plot" | the plot | always |
data-slot="chart-slice" | a slice | always; data-active when inspected, data-dimmed when not |
data-slot="chart-slice-label" | its share | labels is on and the label fits |
data-slot="chart-legend" | the legend | the legend shows |
data-slot="chart-tooltip" | the tooltip | a slice is inspected |
Accessibility#
- The plot is a slider named after
label("Traffic by source: inspect values"). Tab to it, then the arrow keys step through the slices clockwise, Home and End jump to the first and last, and Escape lets go. Its value text reads the tooltip: the slice's name, value and share. - Every row is also in a table hidden from sight, with its value and share, folded rows included.
- With two or more slices the legend shows, so no slice is told apart by colour alone. Its items are toggle buttons with
aria-pressed. - Text never takes a slice's colour: shares and values stay in the text colours, beside a coloured key or on a card-coloured pill.
loadingsetsaria-busyon the root and dims the last drawing until new data arrives.