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.jsonVisits by browser
- Chrome63.4%
- Safari20.5%
- Edge5.5%
- Other10.6%
PieChartPieTablefoldSlicesdefaultPieChartLabels- +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%
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { PieTable, PieChart, type PieSlice } from "@/components/sheenui/charts/pie-chart";
// Package build (after the first npm release, or a packed tarball): import { ChartCard, PieTable, PieChart, type PieSlice } from "@sheenui/theme";
const data: PieSlice[] = [
{ label: "Chrome", value: 612000, color: 1 },
{ label: "Safari", value: 198000, color: 2 },
{ label: "Edge", value: 53000, color: 3 },
{ label: "Firefox", value: 34000, color: 4 },
{ label: "Other mobile", value: 26000, color: 5 },
{ label: "Opera", value: 22000, color: 6 },
{ label: "Brave", value: 11000, color: 7 },
{ label: "UC Browser", value: 9000, color: 8 },
]; // the smallest fold into "Other"
export function Example() {
return (
<ChartCard
title="Visits by browser"
table={<PieTable data={data} indexLabel="Browser" />}
>
<PieChart data={data} aria-label="Monthly visits by browser" />
</ChartCard>
);
}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.tsxcomponents/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.tslib/{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
import { formatCompact } from "@/components/sheenui/charts/chart-utils";
import { PieChart, type PieSlice } from "@/components/sheenui/charts/pie-chart";
// Package build (after the first npm release, or a packed tarball): import { formatCompact, PieChart, type PieSlice } from "@sheenui/theme";
const gb = (n: number) => formatCompact(n) + " GB";
const storage: PieSlice[] = [
{ label: "Photos", value: 182, color: 1 },
{ label: "Video", value: 96, color: 2 },
{ label: "Apps", value: 58, color: 3 },
{ label: "Documents", value: 21, color: 4 },
];
export function Example() {
return (
<>
<PieChart data={storage} format={gb} outerLabels={false} height={200} aria-label="Storage used by type" />
<PieChart data={[{ label: "Photos", value: 182 }]} format={gb} height={200} aria-label="Storage used by type (photos only)" /> {/* one slice: a full disc */}
<PieChart data={[]} aria-label="Storage used by type" emptyState="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%
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { PieChart, PieTable } from "@/components/sheenui/charts/pie-chart";
// Package build (after the first npm release, or a packed tarball): import { ChartCard, PieChart, PieTable } from "@sheenui/theme";
const browsers = [
{ label: "Chrome", value: 62 },
{ label: "Safari", value: 21 },
{ label: "Firefox", value: 9 },
];
export function Example() {
return (
<ChartCard title="Browser share" table={<PieTable data={browsers} indexLabel="Browser" caption="Browser share" />}>
<PieChart patterns data={browsers} aria-label="Browser share, hatched" />
</ChartCard>
);
}PieChart props
PieChartLabels keys and English defaults (10)
PieTable props
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
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
Related pages
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 .