Donut Chart for React
An animated React donut chart with the total in the center, a full legend and keyboard-readable slices. Pass data as readonly DonutSlice[]: part-to-whole only, at most 6 slices: fold the rest into "Other".
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/donut-chart.jsonTraffic sources
DonutChartDonutTabledefaultDonutChartLabelsdefaultDonutTableLabels- +2 more
Donut chart with a center total
Hover a slice to read it in the center. Click a legend key to leave a slice out; the ring re-flows and the total re-counts.
Traffic sources
Sessions this month
import { ChartCard } from "@/components/sheenui/charts/chart-parts/chart-card";
import { ChartTable } from "@/components/sheenui/charts/chart-parts/empty-table";
import { DonutChart } from "@/components/sheenui/charts/donut-chart";
// Package build (after the first npm release, or a packed tarball): import { ChartCard, ChartTable, DonutChart } from "@sheenui/theme";
const traffic = [
{ label: "Organic search", value: 41200 },
{ label: "Direct", value: 22800 },
];
export function Example() {
return (
<ChartCard
title="Traffic sources"
description="Sessions this month"
table={
<ChartTable
data={traffic.map((t) => ({ source: t.label, sessions: t.value }))}
index="source"
indexLabel="Source"
series={[{ key: "sessions", label: "Sessions", color: 1 }]}
/>
}
>
<DonutChart data={traffic} labels={{ center: "Sessions" }} aria-label="Sessions by traffic source" />
</ChartCard>
);
}Files to copy
11 folders · 24 files · 4 packages
- From
theme/src/ components/charts/{donut-chart.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.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/donut-chart.jsonRegistry item: donut-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".
DonutChart props
DonutChartLabels keys and English defaults (7)
DonutTable props
DonutTableLabels keys and English defaults (4)
When to use a donut chart
A React donut chart, which some teams call a doughnut chart, shows how one total divides into a few parts, such as traffic sources, plan mix or storage by file type. The hole in the middle holds the total, which a plain pie chart has no room for, and the ring stays legible on a glass card.
If the parts are close in size or there are many, a bar chart is easier to compare. See the pie chart, the radial bar chart and the gauge chart for related shapes, or the React bar chart for an exact comparison.
Frequently asked questions
All exports on this page
DonutChart— A donut for the share of a whole, with a centre label (labels.center, default "Total") and a legend that can hide slices.DonutTable— The table view of a DonutChart: every slice with its value and its share of the total.defaultDonutChartLabels— The built-in English strings of DonutChart; spread it into your own labels and change what you need.defaultDonutTableLabels— English defaults of DonutTableLabels.
Keyboard
Accessibility
- Tab focuses the ring as one stop; the arrow keys step slice by slice and announce the label, the value and its share of the total.
- The center total is text, not an image, and the Chart / Table tabs switch to a DonutTable with slice, value and share columns.
- Legend entries are toggle buttons (Enter or Space) that hide a slice; with more than three slices each gets its own hatch (patterns).
- Reduced motion: the ring appears at its final angles and data changes jump.
Related pages
48 charts pages in all: browse them on Charts.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .