# Doughnut chart

> A whole split between categories around its total, on one or several rings, with each share in a tooltip, keyboard navigation and a details modal.

Source: https://eunomia-ds.com/charts/doughnut-chart/

A doughnut chart shares a whole out between categories, a slice each, around its total written in the middle: revenue per sales channel, a budget per team. Several series are drawn as rings inside one another, to compare two splits. It takes the same data as the bar and line charts, its categories becoming the slices. Hovering a slice, or moving along them with the arrow keys, moves it out and writes its value in the middle, and a click can open a modal with its details.

Dependencies

- [eun-skeleton · while loading](https://eunomia-ds.com/components/skeleton/)
- [eun-modal · if details is set](https://eunomia-ds.com/components/modal/)

## Overview

### When to use

Reach for a doughnut chart when the parts of a whole matter, along with the whole itself: the total revenue and how it splits between channels. Its middle writes the total, then the hovered category's value, and several series draw as rings to compare two splits, this year's and last year's. Like a [pie chart](https://eunomia-ds.com/charts/pie-chart/), it reads best with two to five categories; past that, or to compare values that don't add up to a whole, prefer a [bar chart](https://eunomia-ds.com/charts/bar-chart/).

The data follows the contract shared by every chart, see [Charts](https://eunomia-ds.com/charts/) for its fields.

### Install & usage

**HTML / any framework:**

```bash
npm install @eunomia/elements @eunomia/charts
```

```js
import "@eunomia/charts/doughnut-chart.js";
```

```html
<eun-doughnut-chart label="Revenue per channel"></eun-doughnut-chart>

<script type="module">
  const chart = document.querySelector("eun-doughnut-chart");
  chart.data = {
    categories: [
      { key: "web", label: "Website" },
      { key: "app", label: "App" },
      { key: "store", label: "Store" },
    ],
    series: [
      { key: "revenue", label: "Revenue", values: [42800, 23100, 15600] },
    ],
  };
</script>
```

Importing the file registers `<eun-doughnut-chart>` as a custom element, with no further setup needed. It draws as large as `--chart-height` and its width allow, centered, its total in the middle. Import `@eunomia/elements/skeleton.js` too to show a placeholder while `loading`, and `@eunomia/elements/modal.js` to use `details`.

**React:**

```bash
npm install @eunomia/elements @eunomia/charts
```

```jsx
import { useMemo } from "react";
import "@eunomia/charts/doughnut-chart.js";

const euros = { style: "currency", currency: "EUR" };

function ChannelsChart({ channels }) {
  const data = useMemo(
    () => ({
      categories: channels.map((channel) => ({
        key: channel.id,
        label: channel.name,
      })),
      series: [
        {
          key: "revenue",
          label: "Revenue",
          values: channels.map((channel) => channel.revenue),
        },
      ],
    }),
    [channels],
  );

  return (
    <eun-doughnut-chart
      label="Revenue per channel"
      data={data}
      format={euros}
    />
  );
}
```

React 19 sets `data` as a property. On React 18, set it through a ref, or with `eunProps` from `@eunomia/elements/react`.

**Vue:**

```bash
npm install @eunomia/elements @eunomia/charts
```

```html
<script setup>
  import "@eunomia/charts/doughnut-chart.js";

  defineProps({ data: Object });
</script>

<template>
  <eun-doughnut-chart label="Revenue per channel" :data="data" />
</template>
```

**Angular:**

```bash
npm install @eunomia/elements @eunomia/charts
```

```ts
import { CUSTOM_ELEMENTS_SCHEMA, Component, Input } from "@angular/core";
import type { EunomiaDoughnutChartData } from "@eunomia/charts";
import "@eunomia/charts/doughnut-chart.js";

@Component({
  selector: "app-channels-chart",
  template: `
    <eun-doughnut-chart
      label="Revenue per channel"
      [data]="data"
      (eunchartselect)="open($event.category.key)"
    ></eun-doughnut-chart>
  `,
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class ChannelsChartComponent {
  @Input() data!: EunomiaDoughnutChartData;
  open(key: string) {}
}
```

#### Server rendering

Before it upgrades, a chart is an empty tag. Import `chart-ssr.css` with the page's other styles to reserve its room from the first paint: `--chart-height`, plus a row for the legend once `legend="visible"`, or 100% of its parent's height with `fill`.

```css
@import "@eunomia/charts/chart-ssr.css";
```

Without a bundler, link `node_modules/@eunomia/charts/dist/ssr/chart-ssr.css` in the page's `<head>`. Two cases still shift the page on upgrade: a legend left to `legend="auto"`, which only shows with the data, and a legend that wraps onto more than one row. See [Size: Before it upgrades](https://eunomia-ds.com/charts/size/#before-it-upgrades).

### Alternatives

| You want… | Prefers |
| --- | --- |
| A single split, its slices filling the whole circle | `eun-pie-chart` |
| To compare values precisely, or more than five categories | `eun-bar-chart` |
| To list exact values, to be read, sorted or compared one by one | `eun-table` |

### Guidance

> **Recommended**
>
> - Give every chart a `label`: it names the plot for assistive technology and captions its data table
> - Keep to two to five categories, grouping the smallest ones into « Other »
> - Order the categories from the largest to the smallest, so the slices read clockwise from twelve o'clock
> - Keep the legend: slices are told apart by color only

> **Avoid**
>
> - Values that don't add up to a whole, such as averages or rates: a slice means a share
> - Negative values: they get no slice
> - More than two or three rings: inner rings get thin and hard to compare

## API

### Properties

`data` and `label` are the two you'll always set. The data's own fields, shared with every chart, are listed on [Charts: The data contract](https://eunomia-ds.com/charts/#the-data-contract).

### Doughnut chart `<eun-doughnut-chart>`

#### Attributes

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| data | EunomiaChartData<S> | EMPTY_DATA | What the chart draws, as JSON: its categories and its series |
| hide-center | boolean | false | Writes nothing in the middle |
| center-label | string | — | The wording under the total in the middle. Defaults to the localized string (English: "Total") |
| legend-position | EunomiaChartLegendPosition | 'start' | How the legend's entries line up under the plot |
| hide-tooltip | boolean | false | Shows no tooltip on hover or keyboard moves |
| empty-label | string \| undefined | — | The text shown in place of the slices while there is nothing to share out |
| hide-empty-label | boolean | false | Shows no text while there is nothing to share out |
| format | Intl.NumberFormatOptions \| undefined | — | The Intl.NumberFormat options the values are written with, as JSON |
| select-hint | string \| undefined | — | The hint at the bottom of the tooltip while slices are clickable |
| non-interactive | boolean | false | Draws the chart as a plain picture: no focus stop, no hover, no tooltip, no selection |
| cutout | number | 0.6 | The hole's radius, as a share of the chart's, from 0 to 0.9 |
| locale | string \| undefined | — | The locale used to resolve dictionary and `Intl` formatting. Defaults to the shared default set via `setDefaultLocale()`, itself defaulting to `"en"`. |
| label | string \| undefined | — | The chart's accessible name, also the caption of its data table. |
| legend | EunomiaChartLegend | "auto" | When the legend is shown — see EunomiaChartLegend. |
| fill | boolean | false | Takes the whole height of the chart, rather than `--chart-height`: the plot stretches to what's left once the legend is laid out, and redraws whenever that changes. The chart takes 100% of its parent's height by default; size it like any block otherwise (`height`, `flex: 1`, a grid cell). |
| loading | boolean | false | Shows a placeholder in place of the plot while the data loads. |
| refreshing | boolean | false | Keeps the plot shown as a placeholder while new data loads, such as after a change of period: the marks faded, their colors kept, under a shimmer, the axes kept, the tooltip off. |
| animated | boolean | false | Draws the marks in once the chart is first seen. |
| selectable | boolean | false | Makes every category clickable, firing `eunchartselect`. |
| details | boolean | false | Opens a modal listing a category's values once it's clicked. Implies selectable. |

Import the exact TypeScript type behind any property above, see [Types](https://eunomia-ds.com/guides/types/).

#### Properties

JS-only — no matching HTML attribute, set these from a script or a template binding.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| valueFormatter | EunomiaChartValueFormatter \| undefined | — | Writes a value in the tooltip, the details and the data table, in place of format. Property only |
| tooltipRenderer | EunomiaChartTooltipRenderer<S> \| undefined | — | Writes the content of the tooltip in place of the values and the category's label. Receives the category, the values of every series written and colored, and the total; returns a DOM node, a string shown as text or a Lit template, null keeping the default content. Property only |
| resolvedLocale | string | — | This instance's own `locale` when set, the shared default locale otherwise. |
| dictionary | ChartDictionary | — | This instance's locale-resolved chart strings. |

#### Methods

| Name | Returns | Description |
| --- | --- | --- |
| replayAnimation() | Promise<void> | Plays the entrance animation again, such as once new data is shown. Does nothing while the person asked to reduce motion. |

#### Slots

| Name | Description |
| --- | --- |
| details | Extra content shown in the details modal, below the category's values, such as the records behind them. Update it from eunchartselect. A <template slot="details"> is filled for the clicked category instead, as the tooltip's: data-field, data-each and data-bind-* attributes such as data-bind-href="/orders?day={key}" |
| tooltip | A <template> writing the content of the tooltip in plain HTML, in place of the values and the category's label: data-each="values" (or "drawn") repeats an element per series, data-field writes category, key, tick, total, and per series series, text, value or color. tooltipRenderer takes over when set. data-bind-<attr> writes an attribute, {category}, {key}, {tick} and {index} replaced |

#### Events

| Name | Type | Description |
| --- | --- | --- |
| eunchartselect | ChartSelectEvent | Fired when a slice is clicked, or a category activated with Enter or Space, while selectable or details is set. Cancelable to keep the details modal from opening |

Every event above follows the same naming convention, covered in [Events](https://eunomia-ds.com/guides/events/).

#### CSS custom properties

| Name | Description |
| --- | --- |
| --chart-height | Sets the height of the plot, the chart's diameter following it, ignored with fill. 220px by default |
| --chart-min-height | Sets the least height of the plot with fill. 80px by default |
| --chart-color-1 | Sets the color of the first category, and of every eighth one after it. The primary color by default |
| --chart-color-2 | Sets the color of the second category. The secondary color by default |
| --chart-color-3 | Sets the color of the third category |
| --chart-color-4 | Sets the color of the fourth category |
| --chart-color-5 | Sets the color of the fifth category |
| --chart-color-6 | Sets the color of the sixth category |
| --chart-color-7 | Sets the color of the seventh category |
| --chart-color-8 | Sets the color of the eighth category |
| --chart-surface-color | Sets the color of the surface the chart sits on, drawing the gaps between slices |
| --chart-slice-gap | Sets the width of the gap between two slices. 2px by default |
| --chart-grid-color | Sets the color of the empty track drawn while there is nothing to share out |
| --chart-axis-color | Sets the color of the legend |
| --chart-tooltip-background | Sets the background color of the tooltip |
| --chart-tooltip-color | Sets the text color of the tooltip |
| --chart-focus-outline-color | Sets the color of the plot's focus ring |
| --chart-animation-duration | Sets the duration of the entrance animation |
| --chart-refresh-opacity | Sets the opacity the slices take while refreshing, their colors kept. 0.4 by default |
| --chart-refresh-highlight-color | Sets the color of the shimmer sweeping the plot while refreshing |
| --chart-center-font-size | Sets the largest font size of the value in the middle. 22px by default |

#### CSS shadow parts

| Name | Description |
| --- | --- |
| plot | The svg the chart is drawn in |
| tooltip | The tooltip |
| legend | The legend |
| legend-item | An entry of the legend, its swatch and its name |
| legend-swatch | The swatch of an entry of the legend |
| details | The details modal |

### Types

```ts
import type {
  EunomiaDoughnutChartData,
  EunomiaRadialChartSeries,
  EunomiaChartCategory,
  EunomiaChartLegend,
  EunomiaChartLegendPosition,
  EunomiaChartValueFormatter,
} from "@eunomia/charts";
import { ChartSelectEvent } from "@eunomia/charts";
```

`ChartSelectEvent` carries the selected `category`, its `index` in `data.categories`, and `values`: every series with its value on that category, `null` for none.

## Examples

### Data

#### A slice per category

The categories become the slices, from twelve o'clock clockwise, in the order they come in. Missing, zero and negative values get no slice.

```js
chart.data = {
  categories: [
    { key: "web", label: "Website" },
    { key: "app", label: "App" },
    { key: "store", label: "Store" },
    { key: "marketplace", label: "Marketplace" },
  ],
  series: [
    { key: "revenue", label: "Revenue", values: [42800, 23100, 15600, 6900] },
  ],
};
```

#### The same data as the other charts

The doughnut takes the data of a bar or line chart as is, and draws each series as a ring, the first one outermost. Here the orders of 2026 and 2025 shared out between the quarters, from the same object the bar chart draws side by side: the tooltip and the details give each ring's value and share.

```js
chart.data = {
  categories: quarters,
  series: [
    { key: "2026", label: "2026", values: [48, 57, 49, 71] },
    { key: "2025", label: "2025", values: [42, 51, 38, 64] },
  ],
};
```

A series set to `display: "none"` is never drawn, its values only in the tooltip, the details and the data table, such as the orders behind the revenue of the first example.

#### Three rings and more

Every drawn series gets a ring, as many as there are series, the first one outermost and the rings thinning to share the room. Here three years: hovering a quarter lights it up on every ring, the tooltip giving each year's value and share. Past three or four rings, they get too thin to read: prefer a bar chart.

```js
chart.data = {
  categories: quarters,
  series: [
    { key: "2026", label: "2026", values: [48, 57, 49, 71] },
    { key: "2025", label: "2025", values: [42, 51, 38, 64] },
    { key: "2024", label: "2024", values: [36, 44, 31, 52] },
  ],
};
```

### The middle

#### Total and hovered value

The middle writes the total of the outer ring, under it "Total", or `center-label`. Hovering a slice, or moving along the categories with the keyboard, writes that category's value and label instead; the text shrinks to fit the hole. `hide-center` writes nothing.

```html
<eun-doughnut-chart
  label="Budget per team"
  center-label="Yearly budget"
  format='{"style":"currency","currency":"EUR","notation":"compact"}'
></eun-doughnut-chart>
```

#### Thickness

`cutout` sets the hole's radius as a share of the chart's, 0.6 by default, from 0 (a pie) to 0.9 (a thin ring). The middle stays empty once the hole gets too small for its text.

```html
<eun-doughnut-chart
  label="Revenue per channel"
  cutout="0.8"
  hide-center
></eun-doughnut-chart>
```

#### Colors

Slices take the palette's colors in category order, `--chart-color-1` (the primary color) first. A category's own `color` takes over, any CSS color or a token, for categories that carry a meaning of their own.

```js
categories: [
  { key: "done", label: "Done", color: "var(--eun-color-green-500)" },
  { key: "doing", label: "In progress", color: "var(--eun-color-secondary-500)" },
  { key: "todo", label: "To do", color: "var(--eun-color-grey-400)" },
],
```

### Legend and tooltip

The legend lists the categories, from two of them on (`legend="auto"`), always (`visible`) or never (`hidden`). `legend-position` lines it up from the `start`, in the `center` or against the `end`. `hide-tooltip` shows no tooltip, the hovered slice still moving out and its value still written in the middle: a compact chart for a card.

```html
<eun-doughnut-chart
  label="Revenue per channel"
  legend-position="center"
></eun-doughnut-chart>
<eun-doughnut-chart
  label="Revenue per channel"
  hide-tooltip
  legend="hidden"
></eun-doughnut-chart>
```

The legend and the tooltip work the same on every chart: see [Legend](https://eunomia-ds.com/charts/legend/) for its swatches, its appearance and how to build your own, and [Tooltip](https://eunomia-ds.com/charts/tooltip/) for its hint, its appearance and writing its content in HTML or JavaScript.

### States

#### Loading and refreshing

`loading` shows a round placeholder while the first data loads. `refreshing` turns the current slices into a placeholder while new data loads, such as after a change of period: they're faded, their colors kept, under a shimmer, and the tooltip is off until the new data comes in.

```js
chart.refreshing = true;
chart.data = await fetchPeriod(period);
chart.refreshing = false;
```

#### Empty

While there's nothing to share out, no category or no value above zero, the chart draws an empty track and writes "No data". `empty-label` writes your own message, `hide-empty-label` none.

#### Entrance animation

By default, a chart is drawn at once, with no motion: the right choice for a dashboard refreshed often, or for one chart among many on a page.

`animated` turns the rings in once the chart is first seen. `replayAnimation()` plays it again. It's skipped when the person asked their system to reduce motion.

```html
<!-- No animation, the default -->
<eun-doughnut-chart label="Revenue per channel"></eun-doughnut-chart>

<!-- Animated when first seen -->
<eun-doughnut-chart label="Revenue per channel" animated></eun-doughnut-chart>
```

### Selection

#### Selectable slices

`selectable` makes every slice clickable, and every category activatable with Enter or Space once the plot is focused, firing `eunchartselect` with the category and its values.

```js
chart.addEventListener("eunchartselect", (event) => {
  filterOrders(event.category.key);
});
```

#### Details modal

`details` opens a modal on click, headed by the category's label and listing the value and share of every series. It needs `eun-modal` to be registered, and takes extra content through its `details` slot.

The modal works the same on every chart: see [Details modal](https://eunomia-ds.com/charts/details/) for what it lists, content of your own in HTML or from JavaScript, opening your own modal instead, and its appearance.

#### Non-interactive

`non-interactive` draws the chart as a plain picture: no focus stop, no hover, no tooltip and no selection, even with `selectable` or `details`. It fits a small chart in a table cell or a card, where the row or the card already takes the click. The plot keeps its accessible name and its data table. See [Size: Small charts](https://eunomia-ds.com/charts/size/#small-charts).

```html
<eun-doughnut-chart
  label="Revenue per channel"
  non-interactive
></eun-doughnut-chart>
```

### Customization

#### Custom appearance

The custom properties below restyle the chart, and its parts (`plot`, `tooltip`, `legend`, `details`) take any other rule. Here a taller doughnut in shades of teal, with wider gaps between the slices, a larger total and the legend in the center.

```css
.revenue-chart {
  --chart-height: 280px;
  --chart-color-1: #0f766e;
  --chart-color-2: #14b8a6;
  --chart-color-3: #5eead4;
  --chart-color-4: #ccfbf1;
  --chart-slice-gap: 4px;
  --chart-center-font-size: 28px;
  --chart-tooltip-background: #134e4a;
}

.revenue-chart::part(tooltip) {
  border-radius: 12px;
}
```

| Variable | Sets |
| --- | --- |
| `--chart-height` | Sets the height of the plot, the doughnut's diameter following it |
| `--chart-color-1` … `-8` | Sets the palette the categories take their color from, in order |
| `--chart-slice-gap` | Sets the width of the gap between two slices. 2px by default |
| `--chart-center-font-size` | Sets the largest font size of the value in the middle. 22px by default |
| `--chart-surface-color` | Sets the color of the surface the chart sits on, drawing the gaps |
| `--chart-grid-color` | Sets the color of the empty track |
| `--chart-axis-color` | Sets the color of the legend |
| `--chart-tooltip-background` | Sets the background color of the tooltip |
| `--chart-tooltip-color` | Sets the text color of the tooltip |
| `--chart-focus-outline-color` | Sets the color of the plot's focus ring |
| `--chart-animation-duration` | Sets the duration of the entrance animation |
| `--chart-refresh-opacity` | Sets the opacity the slices take while refreshing, their colors kept. 0.4 by default |

## Accessibility

### Keyboard interactions

The plot is a single focus stop. Once focused, it shows the last category, and the arrow keys move along the others, in the order of the slices.

| Key | Action |
| --- | --- |
| `Tab` / `Shift+Tab` | Moves focus to or away from the plot |
| `ArrowLeft` / `ArrowRight` | Shows the previous or the next category |
| `Home` / `End` | Shows the first or the last category |
| `Enter` / `Space` | Selects the shown category, while `selectable` or `details` is set |

### Aria attributes and rules

- The plot is an `svg` with `role="img"`, named by `label`. Give every chart one.
- Every value stays reachable in a visually hidden table, captioned by `label`, a row per category and a column per series. Screen reader users read the data there rather than through the drawing.
- Moving along the categories with the keyboard announces each one through a polite live region, its label followed by its values.
- The tooltip, the legend and the middle's text are `aria-hidden`: they repeat what the table already holds.
- While `refreshing`, the plot is marked `aria-busy`.
- `animated` is skipped when the person asked their system to reduce motion.
- With `non-interactive`, the plot is no focus stop and reacts to neither the pointer nor the keyboard; its accessible name and its data table are still read by screen readers.

### Color and contrast

Slices are told apart by color only, so keep the legend shown and give categories meaningful labels: the tooltip, the details and the data table name every value. Neighboring slices are separated by a gap in the surface color, and each should keep a 3:1 contrast with the surface around the chart (WCAG 1.4.11); the default palette does on the default themes, check your own colors when you set them.

### Reference links

- [WAI Web Accessibility Tutorials: Complex images](https://www.w3.org/WAI/tutorials/images/complex/)
- [Understanding WCAG 2.2: Non-text contrast](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html)
