Donut chart

The parts of one whole, as a ring, with the total in the middle and each part's value and share in the legend. For a few parts where the share of the whole is the point.

Donut chart

Needs Alpine and the package's script for the readout and keyboard access to each segment. Without them the chart is a correct static image with a generated text alternative. See getting started.

When not to use it

A donut answers one question: roughly what share of the whole is each part? For almost anything else, another chart is better.

  • Comparing parts with each other. Angles are read far less accurately than lengths; two parts a few percent apart look the same. Use a bar chart, sorted.
  • A change over time. Two donuts side by side cannot be compared. Use a line chart, or a stacked bar chart.
  • More than five or six parts. The small ones become slivers. maxSegments folds the tail into "Other", but if the tail is what matters, use bars.
  • Parts that are not a whole: survey answers where several could be chosen, values that can be zero or negative. It throws on those.
  • One number against a target. That is a gauge or a progress bar.

Use it

<x-ds::donut-chart label="Traffic by source" :data="['Direct' => 1200, 'Search' => 800, 'Social' => 400, 'Email' => 250]" />

<x-ds::donut-chart label="Seats by plan" size="sm" legend="bottom" :data="$plans" :format="['suffix' => ' seats']" />

{{-- Your own content in the middle --}}
<x-ds::donut-chart label="Browsers" :data="$browsers" :maxSegments="4">
    <x-slot:centre>
        <span class="text-title font-semibold text-fg">65%</span>
        <span class="text-meta text-fg-muted">Chrome</span>
    </x-slot:centre>
</x-ds::donut-chart>

Props

PropTypeDefaultWhat it does
dataarray[]['Direct' => 1200, …]. Every value must be a number above zero; anything else throws, naming the part.
labelstring—What the whole is. Starts the summary, captions the table.
sizesm md lgmd128, 160 or 208px across.
thicknesssm md lgmdThe ring's width.
legendright bottom nonerightThe legend, with each part's value and share. right wraps underneath when there is no room.
maxSegmentsint, 2 to 86The most segments drawn. Past it, the smallest fold into one neutral "Other".
otherLabelstringOtherWhat the folded segment is called.
showTotalbooltrueThe total in the middle. The centre slot replaces it.
totalLabelstringTotalThe word under the total (not shown at sm).
formatarray[]How numbers read; counter's keys.
summarystringgeneratedThe text alternative, in place of the generated one.
tableboolfalseAlso give part, value and share as a visually hidden table.
interactivebooltrueWith the script: the readout and keyboard access.
emptystringNo data to showThe empty state's title.
SlotWhat it does
centreAnything for the hole, in place of the total. It is inset to the ring's inner edge and wraps there. Hidden from nothing: say in it only what the summary or the legend also says, or it is read twice.

The percentages always add up to exactly 100: they are rounded by largest remainder, not each on its own.

Accessibility

  • Without the script the chart is role="img". With a legend, named by the gist: "Traffic by source: 4 parts, total 2,650; the largest is Direct at 45%." The legend lists every part as text. With legend="none", the name lists every part.
  • With it, one Tab stop: any arrow moves segment to segment, Home and End to the ends, Escape puts the readout away. Each segment is named "Search: 800 (30%)". The focus ring goes round the whole donut and the focused segment widens.
  • The legend is not a toggle here: hiding a part would change the whole.
  • legend="none" leaves colour as the only link between a segment and its name. Use it only when the parts are named beside the chart already.

Don't

  • Don't pass zeros. Filter them out; an empty part has no arc.
  • Don't put a percentage in the centre that the legend contradicts. Take it from the same data.
  • Don't draw several donuts to compare them. Use one bar chart.

More in specs/donut-chart.md.


From the package repo: edit this page on GitHub.

Was this page helpful?