Skip to content

Block 256 of 297

Donut Chart

How a total breaks down, with the total in the center and values and percentages in the legend.

Puck key
DonutChartBlock
Export
donutChartBlock
React component
<DonutChart />
Path
components/puck/donut-chart-block.tsx
Dependencies
None
License
MIT

About this block

When to use it, how to write for it, and what to change in the code.

What it's for

Shows how a total breaks into parts, with the total (or your own figure) in the center and a legend listing each segment's value and share. Segments sort largest first, and past six the smallest combine into "Other". In a narrow card the legend sits under the ring; in a wider one it moves beside it. Values accept P1 data bindings.

  • Enrollment or membership by group
  • Where a budget or a donation goes, by category
  • Program participants by county or age band
Writing the content
  • Enter positive numbers only; zero and negative values are dropped. Percentages are calculated for you.
  • Leave Center value blank to show the computed total, and use Center label to say what it counts, like Students.
  • Segments accept up to 20 rows, but only five plus Other are drawn. If one small group matters, report it separately.
  • Totals of 100,000 or more show in compact form, like 1.2M, in the center and legend.
Accessibility
  • The ring is an SVG with role="img" and a summary listing the total and every segment's value and percentage.
  • The legend prints each label, value and percent as text, so the chart can be read without telling the indigo shades apart (WCAG 1.4.1).
  • The center text is aria-hidden to avoid repetition; a custom center value is repeated in hidden text so it is still announced.
Customizing the code
  • Segment colors are in the COLORS array, darkest first; the last entry colors Other.
  • MAX_SEGMENTS sets when grouping into Other starts; the ring size is size-40 @md:size-44.

Editor fields

What content editors see in the P1 sidebar when they select this block.

  • Titletext
  • Heading levelselect
  • Subtitle or periodtext
  • Segmentsarray
  • Center valuetext
  • Center labeltext
  • Value prefixtext
  • Value suffixtext
  • Footnotetext