Donut Chart

Rounded share segments linked to a center readout and legend.

Preview

"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>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/donut-chart

API Reference

DonutChart

segments
readonly DonutSegment[]

Slices of the ring, each with an id, name, color and value.

Default—
thickness?
number

Ring thickness in viewBox units; the chart is 200 wide.

Default—
gap?
number

Degrees of empty space between neighbouring segments.

Default—
activeId?
string | null

Focused segment, shared by the ring, center, and legend. Controlled.

Default—
defaultActiveId?
string | null

The initially focused segment id when uncontrolled. Default: null

Default—
onActiveChange?
((id: string | null) => void)

Called with the segment id, or null, when the focused segment changes.

Default—
onSelect?
((id: string) => void)

Called when a legend item is clicked.

Default—
selectedId?
string | null

Marks a legend item as pressed.

Default—
formatValue?
((value: number) => string)

Formats values in the center and legend. Default: en-US number format.

Default—
label?
string

Accessible name for the chart.

Default—
labels?
Partial<{ label: string; total: string; }>

Override any built-in string.

Default—
className?
string

Classes merged onto the root element.

Default—

DonutChartCenter

children?
((segment: PlacedSegment | null, total: number) => ReactNode)

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.

Default—
totalLabel?
ReactNode

Caption under the total when nothing is focused.

Default—
className?
string

Classes merged onto the root element.

Default—

DonutChartLegend

renderDetail?
((segment: PlacedSegment) => ReactNode)

Extra line under each name, such as terms or a note.

Default—
renderValue?
((segment: PlacedSegment) => ReactNode)

Trailing value. Defaults to the share and formatted value.

Default—
className?
string

Classes merged onto the root element.

Default—

DonutChartPlot

children?
ReactNode

Centered overlay, usually `DonutChartCenter`.

Default—
className?
string

Classes merged onto the root element.

Default—

Updated