Pie chart

Slant / Charts

A donut of shares with a centre readout.

Demo

Usage

@tuttn/slant
import { PieChart } from '@tuttn/slant'

const data = [{ label: 'Opus', value: 4820 }, { label: 'Sonnet', value: 3150 }, /* … */]

<PieChart data={data} label="Tokens by model, this week" unit="K tokens"
  format={value => value.toLocaleString('en')} />

// A bigger ring, with your own centre while no slice is shown.
<PieChart data={data} label="Tokens by model" size={200} thickness={32}
  center={<Text variant="mono">4 models</Text>} />

// The cursor keeps its own state; pass active and onActiveChange to control it.
<PieChart data={data} label="Tokens by model" active={slice} onActiveChange={setSlice} />

Props

PropTypeDefaultDescription
dataPieChartDatum[]—The slices, clockwise from 12 o'clock. Past five entries, the first four keep their slices and the rest fold into one "Other".
labelstring—The chart's accessible name.
unitstring—Read after each value, e.g. "tokens", and shown under the total in the centre.
format(value: number) => stringvalue => String(value)Formats each value and the total, in the centre, the table and announcements.
sizenumber160The donut's diameter in pixels, including the 3px a shown slice lifts by.
thicknessnumber24The ring's width in pixels. It leaves a hole of at least 16px.
centerReactNode—Shown in the hole while no slice is shown, in place of the total. Visual only: screen readers read the summary instead.
tablebooleanfalseShows the data table under the chart. It is always there for screen readers.
activenumber—The slice the keyboard cursor sits on, for a controlled chart. With more than five entries, "Other" is index 4.
defaultActivenumber0The slice the keyboard cursor starts on.
onActiveChange(index: number) => void—Called with the slice index when the keyboard moves the cursor.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root.

The slices run clockwise from 12 o'clock in data order, separated by a 2px gap, and take the chart colours 1 to 5 in turn (or the slot an entry names with color). Colour never repeats: past five entries the first four keep their slices and the rest fold into one "Other", drawn in the muted colour. Sort the data first if the largest should lead. With two or more slices a legend names each one with its share, so identity never rests on colour alone.

The centre shows the total and the unit. While a slice is shown, by hovering it, its legend entry, or with the keyboard, that slice lifts, the rest fade, and the centre reads its value, name and share instead. center replaces the total with your own content. The centre is visual only.

The chart is one tab stop, named by label and described by a summary: the total, then each slice's name, value and share. Left and Right step through the slices without wrapping, Home and End jump to the first and the last, and each step is announced, for example "Sonnet, 3,150 K tokens, 32%". Escape hides the readout until the next step. A data table, with every entry including those folded into "Other", is always there for screen readers; table shows it.

The drawing is physical: SVG never mirrors, so under dir="rtl" the slices still run clockwise from the top and Right still steps clockwise. The legend beside the donut follows the reading direction.