---
title: "Donut Chart"
description: "Rounded share segments with a linked center readout and legend."
category: "Charts"
publishedAt: "2026-10-12"
updatedAt: "2026-11-10"
documentation: "https://beui.dev/charts/donut-chart"
markdown: "https://beui.dev/charts/donut-chart.md"
license: "MIT"
---

# Donut Chart

> Rounded share segments with a linked center readout and legend.

## Install

```bash
npx shadcn@latest add @beui/donut-chart
```

## Dependencies

- `clsx`
- `motion`
- `react`
- `tailwind-merge`

## Usage

```tsx
"use client";

import {
  DonutChart,
  DonutChartCenter,
  DonutChartLegend,
  DonutChartPlot,
  type DonutSegment,
} from "@/components/charts/donut-chart";

/** Pass a handful of nonnegative parts; shares are computed from the total. */
export function DonutChartExample({ segments }: { segments: readonly DonutSegment[] }) {
  return (
    <DonutChart
      segments={segments}
      label="Monthly revenue by plan"
      formatValue={(value) =>
        value.toLocaleString("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 })
      }
    >
      <div className="grid items-center gap-6 sm:grid-cols-[minmax(0,16rem)_1fr]">
        <DonutChartPlot>
          <DonutChartCenter totalLabel="Monthly revenue" />
        </DonutChartPlot>
        <DonutChartLegend />
      </div>
    </DonutChart>
  );
}
```

## API Reference

### DonutChart

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `segments` | `readonly DonutSegment[]` | — | Yes | Slices of the ring, each with an id, name, color and value. |
| `thickness` | `number` | — | No | Ring thickness in viewBox units; the chart is 200 wide. |
| `gap` | `number` | — | No | Degrees of empty space between neighbouring segments. |
| `activeId` | `string \| null` | — | No | Focused segment, shared by the ring, center, and legend. Controlled. |
| `defaultActiveId` | `string \| null` | — | No | The initially focused segment id when uncontrolled. Default: null |
| `onActiveChange` | `((id: string \| null) => void)` | — | No | Called with the segment id, or null, when the focused segment changes. |
| `onSelect` | `((id: string) => void)` | — | No | Called when a legend item is clicked. |
| `selectedId` | `string \| null` | — | No | Marks a legend item as pressed. |
| `formatValue` | `((value: number) => string)` | — | No | Formats values in the center and legend. Default: en-US number format. |
| `label` | `string` | — | No | Accessible name for the chart. |
| `labels` | `Partial<{ label: string; total: string; }>` | — | No | Override any built-in string. |
| `className` | `string` | — | No | Classes merged onto the root element. |

### DonutChartCenter

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `children` | `((segment: PlacedSegment \| null, total: number) => ReactNode)` | — | No | Custom content for the hole. Receives the focused segment, or null when nothing is focused, and the total. Defaults to a rolling value and caption. |
| `totalLabel` | `ReactNode` | — | No | Caption under the total when nothing is focused. |
| `className` | `string` | — | No | Classes merged onto the root element. |

### DonutChartLegend

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `renderDetail` | `((segment: PlacedSegment) => ReactNode)` | — | No | Extra line under each name, such as terms or a note. |
| `renderValue` | `((segment: PlacedSegment) => ReactNode)` | — | No | Trailing value. Defaults to the share and formatted value. |
| `className` | `string` | — | No | Classes merged onto the root element. |

### DonutChartPlot

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `children` | `ReactNode` | — | No | Centered overlay, usually `DonutChartCenter`. |
| `className` | `string` | — | No | Classes merged onto the root element. |

## Types

### DonutSegment

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | Yes | Unique key for the segment, used for the active and selected segment. |
| `name` | `string` | Yes | Name shown in the legend and the ring's center while active. |
| `color` | `string` | Yes | Any CSS color, such as "#2563eb" or "var(--chart-1)". |
| `value` | `number` | Yes | Nonnegative value. Zero and invalid values are left out of the ring. |

## About Donut Chart

Donut Chart is an animated React donut chart with round-capped segments separated by even gaps over a muted track. The ring sweeps in with Framer Motion when it scrolls into view, the focused segment pops outward while the others fade, and a center readout rolls between the total and the focused segment's value. A linked legend lists each segment with its share bar, and the center, legend and ring share one focus state.

### When to use it

- A billing or analytics page shows revenue, traffic or costs split by source.
- A personal finance app shows spending by category with a total in the middle.
- A dashboard card summarizes a breakdown such as plan mix or device share in a compact ring.

### Accessibility

The ring is an image with an accessible label, and the legend carries keyboard access: each row is a button, focusing a row highlights its segment, and Arrow Up and Arrow Down move between rows. The center readout is a polite live region, so the focused value is announced, and legend rows expose a pressed state when you use selection. With reduced motion enabled, the ring appears in place and focus changes happen without easing.

### FAQ

**How do I make a donut chart in React?**

Install the chart with the shadcn CLI and pass segments to DonutChart, each with an id, name, color and value. Compose DonutChartPlot with DonutChartCenter inside it and DonutChartLegend, or leave children empty for the default layout.

**How do I change the center label?**

Set totalLabel on DonutChartCenter for the caption under the total, or pass a function as children to render your own content from the focused segment and total.

**Can users select a segment?**

Yes. Pass onSelect and selectedId to DonutChart to make legend rows selectable. activeId and onActiveChange control which segment is focused.

## Source

- Registry detail: https://beui.dev/r/donut-chart
- Raw source: https://beui.dev/r/donut-chart/raw
- GitHub: https://github.com/starc007/ui-components
