PieChart
Parts of a whole as a pie or donut, with slice labels, a highlighted slice on hover and a legend that hides slices.
Status: beta. Category: Data display.
import { PieChart, DonutChart } from '@glyphui/ui/chart'
Exports: PieChart, DonutChart.
Notes
- Use five slices or fewer; fold the rest into “Other”. People compare angles poorly, so for exact comparisons use a bar chart.
- Hiding a slice in the legend keeps every slice’s color, so colors keep meaning the same thing.
- DonutChart is PieChart with innerRadius 0.68. The center label only shows on a donut.
PieChart props
| Prop | Type | Default | Description |
|---|---|---|---|
category (required) | string | Field naming each slice. | |
data (required) | readonly T[] | ||
title (required) | string | ||
value (required) | string | Field holding each slice's value. | |
centerLabel | ReactNode | Content in the middle of a donut, e.g. the total. | |
className | string | ||
colors | readonly ChartColorValue[] | One color per slice, in order. Defaults follow the theme palette. | |
description | ReactNode | ||
empty | ReactNode | Shown when there is no data. Defaults to a short "No data" message. | |
error | unknown | Shows an error message in place of the plot, with a retry button when onRetry is set. | |
hiddenSlices | readonly string[] | Slices hidden through the legend (controlled), by category name. | |
hideTitle | boolean | ||
innerRadius | number | Hole size as a share of the radius, from 0 (pie) to 0.9. Default 0 for PieChart. | |
labels | "none" | "value" | "name" | "percent" | Text drawn next to each slice. Default none. | |
loading | boolean | Shows a skeleton in place of the plot. | |
onHiddenSlicesChange | ((hidden: string[]) => void) | ||
onRetry | (() => unknown) | ||
onSliceClick | ((row: T) => void) | ||
showLegend | boolean | ||
size | number | Diameter in pixels. Default 200. | |
valueFormatter | ((value: number) => string) |
Example: Donut
A headline number in the middle.
import { DonutChart } from '@glyphui/ui/chart'
const plans = [
{ plan: 'Free', seats: 1840 },
{ plan: 'Pro', seats: 960 },
{ plan: 'Team', seats: 410 },
{ plan: 'Enterprise', seats: 230 },
]
export default function Example() {
return (
<DonutChart
title="Seats by plan"
data={plans}
category="plan"
value="seats"
size={200}
centerLabel={
<>
<span className="font-display text-h3 tabular-nums">3,440</span>
<span className="text-caption text-muted-foreground">seats</span>
</>
}
/>
)
}
Example: Pie with labels
labels="percent" shows each share.
import { PieChart } from '@glyphui/ui/chart'
const sources = [
{ source: 'Organic search', visits: 48200 },
{ source: 'Direct', visits: 21900 },
{ source: 'Referral', visits: 12400 },
{ source: 'Paid', visits: 9100 },
]
export default function Example() {
return (
<PieChart
title="Traffic by source"
description="Last 30 days"
data={sources}
category="source"
value="visits"
labels="percent"
size={220}
/>
)
}
Live page: https://glyphui.xyz/components/pie-chart