Donut chartFree

Donut or pie with Other grouping, honest exclusions, labels inside or outside and a centre readout.

npx shadcn@latest add https://beautifulcharts.dev/r/donut-chart.json

Engine: ·

Code

"use client";

import { DonutChart } from "@/components/beautiful-charts/themes/instrument/recharts/donut-chart";

const data = [
  { channel: "Organic", visits: 4120 },
  { channel: "Referral", visits: 1860 },
  { channel: "Paid search", visits: 1340 },
  { channel: "Social", visits: 720 },
  { channel: "Email", visits: 410 },
];

// Segments under 6% are grouped into one "Other" segment; its tooltip lists every member.
export function TrafficSources() {
  return (
    <DonutChart
      data={data}
      nameKey="channel"
      valueKey="visits"
      title="Traffic by source"
      groupBelow={0.06}
      center={{ label: "visits" }}
    />
  );
}

Props

  • nameKey*

    keyof TDatum & string

    The field that names each segment, e.g. "channel".

  • valueKey*

    keyof TDatum & string

    The field that holds each segment's value, e.g. "revenue".

  • innerRadius

    number

    Hole size as a share of the radius, 0–0.9. 0 draws a pie.

  • padAngle

    number

    Gap between segments, in degrees.

  • cornerRadius

    number

    Corner radius of each segment, in px.

  • startAngle

    number

    Where the ring starts and ends, in degrees clockwise from 12 o'clock. A half ring is -90 to 90.

  • endAngle

    number

    Where the ring ends, in degrees clockwise from 12 o'clock.

  • groupBelow

    number

    Group segments smaller than this share (0–1) into one "Other" segment. Members stay in the table and tooltip.

  • maxSegments

    number

    Keep at most this many segments; the rest go into "Other".

  • otherLabel

    string

    Name of the grouped segment. Defaults to "Other".

  • labels

    "none" | "inside" | "outside"

    Segment labels: none, inside the ring, or outside with leader lines.

  • labelContent

    "value" | "share" | "both"

    What labels and the legend show: the value, the share, or both.

  • center

    false | { label?: string }

    The centre readout. Defaults to the shown total, or the selected segment. false leaves the hole empty.

Props every chart shares (32): data, config, header, states, legend, tooltip, formatting, motion and selection
  • data*

    TDatum[]

    Your rows, as they are. Missing values (null, undefined, NaN) stay missing and are never drawn as zero.

  • locale

    string

    BCP 47 locale. Defaults to "en-US" so server and client render the same text.

  • timeZone

    string

    IANA time zone for dates. Defaults to "UTC" so server and client agree.

  • config

    ChartConfig

    Per-series label, colour, icon, format and style, keyed by series key. shadcn ChartConfig works as it is.

  • eyebrow

    ReactNode

    Small label above the title.

  • title

    ReactNode

    Card title. A string also names the chart for screen readers.

  • description

    ReactNode

    A line under the title.

  • takeaway

    ReactNode | ((summary: string) => ReactNode) | false

    The sentence under the title. Computed from the data by default; pass text, a function of the computed sentence, or false to hide it.

  • ariaDescription

    string

    Accessible summary for screen readers. Computed from the data by default.

  • motion

    MotionPreference

    "full" (default), "subtle" or "off". Reduced-motion settings always win.

  • motionOptions

    MotionOptions

    When the entrance plays.

  • sound

    boolean

    Sound cues on hover, scrub and select. Plays only if the visitor has also turned sound on for the page.

  • density

    Density

    "comfortable" (default) or "compact" for dashboards.

  • legend

    LegendOptions | false

    Legend position, alignment, shape and click action, or false to hide it.

  • tooltip

    TooltipOptions<TDatum> | false

    Tooltip position, indicator, cursor and custom content, or false to turn it off.

  • valueFormatter

    (value: number) => string

    Values formatted for display. Series formatters in config win.

  • loading

    boolean

    Show the loading state. Over existing data, the old marks stay dimmed instead of vanishing.

  • error

    string | null

    An error message. Shows the error state in place of the plot.

  • emptyState

    ReactNode

    Shown instead of the plot when there is no data.

  • loadingState

    ReactNode

    Shown while loading is true and there is no data yet.

  • errorState

    ReactNode

    Shown when error is set.

  • selected

    number | null

    Controlled and uncontrolled interaction state. Point selection (selected) pins one row by its index in data. Series selection (selectedSeries) picks one series and dims the others. They are separate: picking a row doesn't pick a series.

  • defaultSelected

    number | null

    Initial selected row when uncontrolled.

  • hidden

    string[]

    Controlled hidden series keys.

  • defaultHidden

    string[]

    Series hidden at first when uncontrolled.

  • activeIndex

    number | null

    Controlled hover or focus index, for syncing several charts on one cursor.

  • palette

    "theme" | "shadcn"

    Series colours: "theme" (default) uses the theme's designed palette; "shadcn" uses your --chart-1..5. --bc-series-N always wins. You can also opt a whole section in with the class bc-use-shadcn-colors.

  • table

    boolean

    Show the "Show data table" disclosure. On by default.

  • stats

    ChartStat[]

    A row of figures under the title, for dashboard cards and blocks.

  • footer

    ReactNode

    A line or action at the bottom of the card, e.g. a source note or a link.

  • className

    string

    Classes on the card, for spacing and layout.

  • height

    number

    Height of the plot area in px. The frame sizes around it.

Events (4)
  • onSelectedChange

    (selection: Selection<TDatum> | null) => void

    Called when a row is selected or cleared, by pointer, keyboard or the data table.

  • onHiddenChange

    (hidden: string[]) => void

    Called when the legend hides or shows a series.

  • onActiveChange

    (index: number | null) => void

    Called as the hovered or focused row changes, or null when it leaves.

  • onReady

    (chart: EChartsType) => void

    Called once with the ECharts instance, e.g. to add event listeners.

Recharts build only (1)
  • children

    ReactNode

    Recharts children render inside the same PieChart, which receives your original rows: add a native <Pie> ring, a <Label> or anything Recharts accepts. A full ring is centred, so cx/cy "50%" line up with ours.

Apache ECharts build only (2)
  • renderer

    EChartsRenderer

    "canvas" (default) or "svg".

  • option

    OptionOverride

    Deep-merged over the generated option, or a function (generated) => option. The ring is one custom series (id "segments", one data item per segment, id = the segment's name). A full ring is centred, so a native pie series with center ["50%", "50%"] lines up with ours. Register extra modules with registerEChartsModules.

Accessibility, keyboard and motion

Pointer
Hover a segment to lift it and show its value and share; click pins it. Legend items toggle segments.
Keyboard
Tab lands once on the plot. Left and Right move between points (the first press goes to the latest), Up and Down change series, Home and End jump to the ends, Enter or Space pins, Escape clears.
Screen readers
Every move is announced as a sentence, not coordinates. "Show data table" opens the numbers as a real table, and missing values stay empty, never zero.
Motion
Segments and rings sweep in one after another; the centre label and legend values follow. With reduced motion everything appears settled at once, and nothing depends on the motion to be read.