Donut Chart

View as Markdown

Shares of one total, with the total in the middle.

Active customers by plan.
SliceValueShare
Free8,41266.5%
Pro2,96023.4%
Team1,1849.4%
Enterprise960.8%

When to use

Shares of one total, with the total in the middle. Use Bar Chart instead when there are more than five parts, or they're close in size.

Usage

import type { ChartDatum } from "./ui/uicontext/components/charts/Data.ts"import { PieChart } from "./ui/uicontext/components/charts/PieChart.ts"
const Message = UiSchema.TaggedEnum({	SelectedPlanSlice: { id: Schema.String },})export const CustomersByPlan = (h: Html.H<typeof Message.Type>, data: ReadonlyArray<ChartDatum>) =>	PieChart(h, {		props: { label: "Active customers by plan", data, center: "customers" },		onEmit: {			// filter the customer list to the plan			RequestedSelect: ({ id }) => Message.SelectedPlanSlice({ id }),			Highlighted: () => undefined,			RequestedToggle: () => undefined,			RequestedRetry: () => undefined,		},	})

Examples

Many slices

Past eight, the smallest slices fold into Other.

Signups by source, last 30 days.
SliceValueShare
Search41,20039%
Direct28,90027.4%
Referral partners12,40011.7%
Social9,1008.6%
Email newsletter6,3006%
Docs4,8004.5%
Events1,9001.8%
Podcasts8200.8%
Print1400.1%
Other websites950.1%

API reference

Props

PieChart props
NameTypeDefaultDescription
labelrequiredstring——
datarequiredChartDatum[]——
formatChartFormat——
centerstring—What the total counts, under it in the middle: "customers"
donutboolean—False draws a full pie, without a hole or the total
sizenumber200The chart's largest size in pixels; it shrinks with its container. 200 by default
hiddenstring[]—Slice ids to hide. Leave it out and the chart keeps track itself
tableToggleboolean——
statusChartStatus——
errorstring——
emptystring——
localestring——

Events

PieChart events
EventPayloadDescription
Highlighted{ id: string | null }—
RequestedSelect{ id: string }—
RequestedToggle{ id: string; hidden: boolean }—
RequestedRetry{}—

Keyboard and accessibility

  • Tab to the donut, then the arrow keys read each slice's value and share of the total. Enter selects it.
  • The total in the middle is part of the chart's name, and the table lists every slice, including those folded into Other.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/donut-chart

Source