Shares of one total, with the total in the middle.
- 7.6 kB JS 5.5 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 33 tests
| Slice | Value | Share |
|---|---|---|
| Free | 8,412 | 66.5% |
| Pro | 2,960 | 23.4% |
| Team | 1,184 | 9.4% |
| Enterprise | 96 | 0.8% |
import type { Html } from "effect/ui/html"import { PieChart } from "./ui/uicontext/components/charts/PieChart.ts"import { customersByPlan } from "./metrics.ts"// Active customers by plan. Hover, tap or arrow to a slice and the middle reads it; hide a plan from// the legend and the total and shares recount.export const CustomersByPlan = <M>(h: Html.H<M>) => PieChart(h, { props: { label: "Active customers by plan", data: customersByPlan, center: "customers" }, onEmit: { Highlighted: () => undefined, RequestedSelect: () => undefined, RequestedToggle: () => undefined, RequestedRetry: () => undefined }, })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.
| Slice | Value | Share |
|---|---|---|
| Search | 41,200 | 39% |
| Direct | 28,900 | 27.4% |
| Referral partners | 12,400 | 11.7% |
| Social | 9,100 | 8.6% |
| Email newsletter | 6,300 | 6% |
| Docs | 4,800 | 4.5% |
| Events | 1,900 | 1.8% |
| Podcasts | 820 | 0.8% |
| 140 | 0.1% | |
| Other websites | 95 | 0.1% |
import type { Html } from "effect/ui/html"import type { ChartDatum } from "./ui/uicontext/components/charts/Data.ts"import { PieChart } from "./ui/uicontext/components/charts/PieChart.ts"const sources: ReadonlyArray<ChartDatum> = [ { id: "search", label: "Search", value: 41_200 }, { id: "direct", label: "Direct", value: 28_900 }, { id: "referral", label: "Referral partners", value: 12_400 }, { id: "social", label: "Social", value: 9_100 }, { id: "email", label: "Email newsletter", value: 6_300 }, { id: "docs", label: "Docs", value: 4_800 }, { id: "events", label: "Events", value: 1_900 }, { id: "podcasts", label: "Podcasts", value: 820 }, { id: "print", label: "Print", value: 140 }, { id: "other-web", label: "Other websites", value: 95 },]// Ten sources is more than eight colors can tell apart, so the smallest fold into Other. The table// still lists every one.export const SignupSources = <M>(h: Html.H<M>) => PieChart(h, { props: { label: "Signups by source, last 30 days", data: sources, center: "signups", size: 180 }, onEmit: { Highlighted: () => undefined, RequestedSelect: () => undefined, RequestedToggle: () => undefined, RequestedRetry: () => undefined }, })API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
labelrequired | string | — | — |
datarequired | ChartDatum[] | — | — |
format | ChartFormat | — | — |
center | string | — | What the total counts, under it in the middle: "customers" |
donut | boolean | — | False draws a full pie, without a hole or the total |
size | number | 200 | The chart's largest size in pixels; it shrinks with its container. 200 by default |
hidden | string[] | — | Slice ids to hide. Leave it out and the chart keeps track itself |
tableToggle | boolean | — | — |
status | ChartStatus | — | — |
error | string | — | — |
empty | string | — | — |
locale | string | — | — |
Events
| Event | Payload | Description |
|---|---|---|
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