Pie Chart
React SVG pie chart with outside labels on leader lines, a legend and slices that pull out on hover or focus to show value and share, plus a data table.
Installation
npx shadcn@latest add @minidev/pie-chart
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/pie-chart.json
Usage
import { PieChart } from "@/components/ui/pie-chart"
export function Example() {
return (
<PieChart />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| dataSlices in clockwise order, starting at the top. | PieDatum[] | SAMPLE |
| titleHeading shown above the chart and used as the accessible name. | string | "Revenue by plan" |
| captionCaption under the total when no slice is active. | string | "Monthly recurring revenue" |
| formatFormats values in the readout, legend and table. | (value: number) => string | defaultFormat |
| showLabelsLabels outside the slices with leader lines. Hidden automatically when the chart is narrow. | boolean | true |
| showLegendLegend with values under the chart. | boolean | true |
| pullOutDistance a hovered or focused slice pulls out, in chart units. | number | 6 |
| className | string | - |
When to use
Hover or focus a slice and it pulls out on a spring while the readout shows its value and share. Slices are one tab stop; arrow keys, Home and End move between them, and a visually hidden table carries the data for screen readers. Labels hide below 320px wide. Use it for 2 to 6 parts of a whole; for more categories or comparisons over time use BarChart, for a ring with a centre total use DonutChart.
Keyboard interactions
| Key | Action |
|---|---|
| Tab | Moves focus into the pie. All slices share one Tab stop. |
| ArrowRightorArrowDown | Moves focus to the next slice, pulls it out and shows its value. |
| ArrowLeftorArrowUp | Moves focus to the previous slice. |
| HomeorEnd | Moves focus to the first or last slice. |
Accessibility
- The chart is a
<figure>labelled by its title. The SVG isrole="group", and each slice is a focusablerole="img"named "Label: value, share%". - Slices with a value of 0 draw nothing and are skipped by the arrow keys.
- A visually hidden
<table>lists every segment's value and share with a total row; the readout, outside labels and legend arearia-hidden. - Under reduced motion the focused slice moves out instantly instead of on a spring.
Frequently asked questions
How do I format PieChart values as currency or plain numbers?
Pass format. It is used in the readout, the legend, each slice's accessible name and the hidden table. The default is compact US dollars, such as "$48.2K", with a fixed en-US locale.
Why did the outside labels disappear?
Leader-line labels hide when the chart's box is under 320px wide, measured with a ResizeObserver; the pie then grows to fill the space and the legend drops to one column. Set showLabels={false} to hide them at every width.
What happens with more than five PieChart slices?
Colours come from --chart-1 to --chart-5 and repeat after five, so a sixth slice reuses the first colour unless you pass color. For more categories use a HorizontalBarChart.
Dependencies
npm
Built from
Examples
Custom data and format
Pass label and value pairs and a format function; the readout, legend and labels all use it.
Compact without labels
Hide the leader line labels and the legend for a small tile; hover still shows each slice.
Semantic colours
Each datum can carry its own colour; use the status tokens when the slices mean something.