Skip to content

Pie Chart for React

A React pie chart for part-to-whole at a glance, folding the smallest slices into “Other” after six. Pass data as readonly PieSlice[]: part-to-whole only.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/pie-chart.json

Visits by browser

Use arrow keys to read each slice.

  • Chrome63.4%
  • Safari20.5%
  • Edge5.5%
  • Other10.6%
  • PieChart
  • PieTable
  • foldSlices
  • defaultPieChartLabels
  • +2 more

Folding into “Other”

8 browsers, 6 slices: the top five keep their color slot and the rest fold into a neutral “Other”, whose tooltip lists what's inside. The table view keeps every row.

Visits by browser

Last 30 days

  • Chrome64.8%
  • Safari17.6%
  • Edge6.2%
  • Firefox3.3%
  • Opera2.9%
  • Other5.1%

Files to copy

11 folders · 25 files · 4 packages
From theme/src/
  • components/charts/{pie-chart.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-utils.ts}
  • components/charts/chart-parts/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/pie-chart.jsonRegistry item: pie-chart. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Compact

In a narrow card the labels step aside for the legend, and the tooltip docks above the pie, clear of your finger.

Storage

236 GB of 512 GB

  • Photos51%
  • Video26.9%
  • Apps16.2%
  • Documents5.9%

One slice

Only photos so far

Empty

Nothing stored yet

Patterns

Hatch fills tell the slices apart without colour. They switch on by themselves above three slices; pass patterns to force them. The legend swatches carry the same hatch.

Browser share

Hatched slices

  • Chrome67.4%
  • Safari22.8%
  • Firefox9.8%

PieChart props

PropTypeDefaultDescription
data (required)readonly PieSlice[]NonePart-to-whole only. Past maxSlices, the smallest slices fold into one "Other" slice.
maxSlicesnumber6At most this many slices are drawn (2–6).
heightnumber260Height of the pie area (the legend sits below it).
outerLabelsbooleantrueName each slice outside the pie with a leader line, only where the label fits without colliding.
legendbooleantrueLegend under the pie with each slice's share.
format((n: number) => string)formatCompactFormats values in the tooltip and legend.
formatPercent((n: number) => string)defaultFormatPercentFormats each share (a number from 0 to 100) for the labels and tooltip.
emptyStateReactNodelabels.noDataShown instead of the pie when there is nothing to plot.
patternsbooleantrue when data.length > 3Tell slices apart without colour (hatch fills).
labelsPartial<PieChartLabels>defaultPieChartLabelsEvery other string, for translation: labels={{ other: "…" }}; omitted keys keep the English of defaultPieChartLabels. All 10 keys of PieChartLabels are listed in the key table under the props table.
aria-label (required)stringNoneNames the chart for assistive tech (required).
classNamestringNoneAdds extra classes to the outer element.
PieTable{ data, maxSlices?, format?, formatPercent?, indexLabel?, caption?, labels? }NoneTable view: every row, its value and share, marking rows drawn inside “Other”.
foldSlices(data, maxSlices?)FoldedSlice[]NoneThe slices exactly as the chart draws them (id, label, value, share, color, slot, members).
PieChartLabels keys and English defaults (10)
Pass any key in labels; the rest keep the defaults of defaultPieChartLabels.
KeyEnglish default
otherOther
inOtherin Other
moremore
sliceSlice
valueValue
shareShare
totalTotal
noDataNo data
instructionsUse arrow keys to read each slice.
touchHintTap a slice to read it.

PieTable props

PropTypeDefaultDescription
data (required)readonly PieSlice[]NoneTakes the same slices as the chart.
maxSlicesnumber6Same as the chart's, so the table marks which rows the chart folds into "Other".
format((n: number) => string)formatCompactFormats a value for the data table cells: takes the number, returns the text shown.
formatPercent((n: number) => string)defaultFormatPercentFormats a share (0–100) for the data table cells: takes the share, returns the text shown.
indexLabelstringlabels.sliceSets the header of the first column.
captionstringNoneSets a visually hidden caption that names the table.
labelsPartial<PieChartLabels>defaultPieChartLabelsEvery other string, for translation: labels={{ other: "…" }}; omitted keys keep the English of defaultPieChartLabels. All 10 keys of PieChartLabels are listed in the key table under the props table.
classNamestringNoneAdds extra classes to the outer element.

When not to use a pie

To compare close values, or to show change over time, use bars or lines. Two slices read better as a single number or a meter.

Keyboard

KeysAction
TabFocus the chart. It is one tab stop; the arrow keys then read it.
→/↓Next slice.
←/↑Previous slice.
Home/EndJump to the first / last slice.
EscapeClear the reading.

Accessibility

  • The figure takes focus as one stop and is named by its aria-label, the chart title. A one-line hint explains the keys, and a ChartCard table view is linked through aria-details. Values are announced politely, once per move. Each move announces the slice, its value and its share.
  • PieTable is the table view and lists every original row, including slices folded into “Other”.
  • Under reduced motion nothing morphs: the slices render at their final angles and new data replaces the old at once.

All exports on this page

  • PieChart — A pie of the shares of a whole, with labels on the slices and a legend.
  • PieTable — The table view of a pie: every original row (nothing folded away), its value and share.
  • defaultPieChartLabels — The built-in English strings of PieChart; spread it into your own labels and change what you need.
  • foldSlices — The slices as drawn: every positive value, at most maxSlices of them.

When to use the pie chart

  • Use it as a pie chart with labels for a browser or device share on an analytics dashboard, where there are a handful of parts and the point is that one of them is clearly the biggest.
  • It suits a storage or budget breakdown in a settings page, where five or six categories add up to a whole and the legend lists each share under the pie.
  • It works in a narrow card too: labels step aside for the legend, and the tooltip docks above the pie so a finger never covers the reading.
  • Skip it when slices are close in size, there are many categories, or the data changes over time; BarChart compares values better, and DonutChart gives you a center slot for a total.
  • Pair it with ChartCard for the title and Chart / Table tabs, PieTable so folded slices stay readable, and foldSlices when you need the exact grouping the chart draws; the legend under the pie is built in.

Frequently asked questions

Guides that cover it

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .