Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Donut Chart

PreviousNext

Parts of one whole as a ring, with the total in the centre and a list of values beside it.

Revenue by plan
Acme Inc, monthly recurring revenue
  • Enterprise43%$48K
  • Team28.2%$32K
  • Pro20.4%$23K
  • Starter8.4%$9.4K
Revenue by plan
ValueShare
Enterprise$48K43%
Team$32K28.2%
Pro$23K20.4%
Starter$9.4K8.4%

Give it rows of data with a name and a value. Each row is a slice of the ring, clockwise from 12 o'clock in the order of the data, in the next colour of the chart palette, with a 2px gap and rounded corners. The total sits in the centre over a short label, and a list beside the ring gives each slice's share and value with a thin bar for its share. On a narrow container the list moves under the ring.

Point at a slice or a row of the list, tap it or tab to the chart and use the arrow keys: the slice lifts out, the others fade back and the centre swaps to that slice's value, name and share. The ring sweeps in from 12 o'clock the first time the chart is on screen, and the slices turn to their new angles when the data changes.

Donut 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 and Pie Chart.

Installation

bunx --bun fabricator-ui@latest add donut-chart

Usage

import { DonutChart } from "@/components/ui/donut-chart"
const data = [
  { plan: "Enterprise", revenue: 48_200 },
  { plan: "Team", revenue: 31_600 },
  { plan: "Pro", revenue: 22_900 },
]
 
<DonutChart
  data={data}
  nameKey="plan"
  valueKey="revenue"
  label="Revenue by plan"
  centerLabel="MRR"
/>

nameKey and valueKey name the fields to read, and default to name and value. formatValue formats the total, the slices' values and the list. A missing, zero or negative value leaves its slice out; with more than eight slices, the seven largest keep theirs and the rest fold into one grey "Other". config is optional: keyed by a slice's name, it gives that slice a label and a color.

Examples

Without the list

list={false} leaves the ring on its own, as wide as its container up to 18rem. The legend then names the slices, and hides one when pressed.

Storage by file type
ValueShare
Images42.1 GB42.9%
Video31.4 GB32%
Documents12.8 GB13%
Backups8.6 GB8.8%
Other3.2 GB3.3%

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 centre and the list stay in the text colours; each row's colour is in its key and its share bar. 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.

Motion

  • Sweep: the first time the chart scrolls into view, the ring sweeps round from 12 o'clock on the reveal role and the list's bars grow in, once per mount.
  • Morph: when data, config or the visible slices change, each slice turns to its new angles and each bar to its new length on the move role. A new slice grows from zero width; one being hidden shrinks to nothing.
  • Lift and swap: the inspected slice moves a few pixels out on the glide role, and the centre crossfades to its value on the feedback role.

All 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

DonutChart

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
centerLabelstring"Total"
listbooleantrue
labelsbooleanfalse
formatValue(value: number, key: string) => string1,234
legendbooleanon without the list, for two or more
tooltipbooleanfalse: the centre reads the slice
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="donut-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="donut-chart-center"the centrethere is data
data-slot="donut-chart-list"the value listlist is on
data-slot="donut-chart-list-item"a rowlist is on; data-active while its slice is inspected
data-slot="chart-legend"the legendthe legend shows

Accessibility

  • The plot is a slider named after label ("Revenue by plan: 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 slice's name, value and share.
  • The list is a list named after label, and each row can take focus, which spotlights its slice as pointing at it does.
  • Every row is also in a table hidden from sight, with its value and share, folded rows included. The centre is hidden from screen readers, since the table and the list already hold its numbers.
  • Text never takes a slice's colour: the centre and the list stay in the text colours, beside a coloured key.
  • loading sets aria-busy on the root and dims the last drawing until new data arrives.