Pie Chart
A pie and donut chart recipe - Recharts drawn with Appica UI tokens, for the parts of 1 whole.
A pie or donut chart shows how 1 whole splits into 2 to 5 parts. Like every chart here it is a recipe: Recharts draws it and the chart layer themes it. Each example below is a complete file to paste and adapt.
Installation
Recharts is the only extra dependency, with react-is at the same version as react:
pnpm add recharts react-isThe examples import the chart layer from @/components/chart. If your project doesn't have it yet, copy it from the Charts page first.
Examples
Donut
A donut with a 60% inner radius and a 2 degree gap between slices. Each row of the data carries its own fill, which is how a slice gets its color. The total sits in the hole as HTML: give Chart the class relative and center an element over it.
Half donut
startAngle={180} and endAngle={0} keep the upper half, and cy="100%" moves the center to the bottom edge. A frame with aspect-2/1 then fits the half circle at any width. Here the data is a status, so meaning picks the colors: delivered takes the success family and returned the error family.
Legend
A pie has no axis to read the names from, so it carries a Legend. The stroke in the surface color separates the slices of a full pie.
Accessibility
- Pass
aria-labelto the chart with what it shows ("Revenue by channel in December"). It becomes the accessible name of the SVG. - The chart takes focus, and the arrow keys move the tooltip from slice to slice.
- Keep to 5 slices at most, and name them in a
Legendor in the tooltip, so color is never the only cue. With more parts, a horizontal bar chart reads better. - A total in the hole is real text, not part of the SVG, so it is read as text.
- Nothing animates under reduced motion.