Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Pie Chart

PreviousNext

Parts of one whole as slices, for a few parts, with the share of each on request.

Traffic by source
Acme Inc, visits in October
Traffic by source
ValueShare
Organic search48K43.4%
Direct29K26.2%
Referral16K14.8%
Social11K10.1%
Email6.1K5.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.

Support tickets by channel
ValueShare
Email41244.3%
Chat31834.2%
Phone12613.5%
Community748%

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.

Orders by country
ValueShare
US4,21036.7%
GB1,83015.9%
DE1,46012.7%
FR9808.5%
CA8707.6%
BR6405.6%
JP5905.1%
ES3102.7%
NL2802.4%
SE1901.7%
PL1201%

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.

TokenHueTokenHue
--chart-series-1blue--chart-series-5magenta
--chart-series-2orange--chart-series-6green
--chart-series-3aqua--chart-series-7violet
--chart-series-4amber--chart-series-8red

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 reveal role, once per mount.
  • Morph: when data, config or the visible slices change, each slice turns to its new angles on the move role. 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 glide role 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>.

PropTypeDefault
dataTDatum[]required
labelstringrequired: the accessible name
nameKeykeyof TDatum"name"
valueKeykeyof TDatum"value"
configChartConfig, keyed by slice namenames and the palette
labelsbooleanfalse
formatValue(value: number, key: string) => string1,234
legendbooleanon for two or more slices
tooltipbooleantrue
animatebooleantrue
loadingbooleanfalse
emptyReactNode"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

AttributeElementPresent when
data-slot="pie-chart"rootalways
data-loadingrootloading is true
data-slot="chart-plot"the plotalways
data-slot="chart-slice"a slicealways; data-active when inspected, data-dimmed when not
data-slot="chart-slice-label"its sharelabels is on and the label fits
data-slot="chart-legend"the legendthe legend shows
data-slot="chart-tooltip"the tooltipa 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.
  • loading sets aria-busy on the root and dims the last drawing until new data arrives.