HapplyUI

Components

Donut Chart

Segments along a half-donut. The featured segment's value and label fill the centre; pass a total to show a figure above the arc.


Installation

bunx @happlyui/cli@latest add donut-chart

Dependencies

npm packages

  • recharts@^3.8.1

Registry dependencies

These are automatically installed when you add this component.

  • happly-ui-utils
  • chart-tooltip

Usage

import * as DonutChart from "@/components/ui/donut-chart"

<DonutChart.Root
  items={[{ label: "Category A", value: 10 }, { label: "Category B", value: 55 }]}
  highlighted="Category B"
  total={{ label: "Total", value: 65 }}
  unit="Items"
/>

Examples

Default

One segment per item, with the highlighted one's value and label in the centre. Past two segments the colours continue the series ramp. Keep labels short, since the legend is one row.

Card title

  • Category A: 29
  • Category B: 21
<DonutChart.Root items={items} highlighted="Category A" />

With total

Pass total and the header shows above the arc. Pass unit and hovering a segment names what it counts.

Card title

Total

65
  • Category A: 10 Items
  • Category B: 55 Items
<DonutChart.Root
  items={items}
  highlighted="Category B"
  total={{ label: "Total", value: 65 }}
  unit="Items"
/>

With short labels

Give an item a shortLabel when its full name won't fit the one-row legend. The legend and centre show the short label; hovering a segment shows the full one.

Card title

  • CA, Category Alpha (CA): 18
  • CB, Category Beta (CB): 24
  • CG, Category Gamma (CG): 8
<DonutChart.Root
  items={[
    { label: "Category Alpha (CA)", shortLabel: "CA", value: 18 },
    { label: "Category Beta (CB)", shortLabel: "CB", value: 24 },
    { label: "Category Gamma (CG)", shortLabel: "CG", value: 8 },
  ]}
  highlighted="Category Beta (CB)"
/>

API Reference

DonutChart.Root

The optional header, the arc and the legend. Sized for a 352px card.

PropTypeDefaultDescription
items{ label: string; shortLabel?: string; value: number; color?: string }[]-Segments along the arc, left to right. shortLabel, when given, replaces label in the legend and centre; the tooltip keeps label
highlightedstring | null-Label of the segment to feature, matched against label, not shortLabel. Omitted or null leaves the centre bare
total{ label: string; value: number }-A caption over a figure above the arc. Shown only when passed
unitstring-What the values count, beside the value in the tooltip

Previous
Chart Tooltip