# Legend

> When a chart shows its legend, what it lists, how its swatches mirror the marks, and how to place and restyle it.

Source: https://eunomia-ds.com/charts/legend/

The legend names what a chart draws: a swatch and a name per entry, under the plot. It works the same on every chart, from the [bar chart](https://eunomia-ds.com/charts/bar-chart/) to the [bubble chart](https://eunomia-ds.com/charts/bubble-chart/): this page covers when it shows, what it lists, and how to place and restyle it.

## When it shows

`legend` chooses when the legend is drawn:

| Value | Shows the legend |
| --- | --- |
| `auto` (default) | As soon as there is more than one entry |
| `visible` | Always, a single entry included |
| `hidden` | Never, such as when the card around the chart already names the measure |

A single series gets no legend by default: the chart's title, or the card around it, already says what it shows. Set `legend="visible"` to name it anyway.

```html
<eun-line-chart label="Sessions per month, in thousands" legend="visible"></eun-line-chart>
```

The legend is also left out while the chart is `loading`, its placeholder standing in for the whole chart.

## What it lists

On the bar, line and combo charts, the legend lists the **drawn series**, in series order. A series set to `display: "none"` is left out: it isn't drawn, so there's nothing to name, its values staying in the tooltip, the details and the data table.

On the pie, doughnut and bubble charts, the legend lists the **categories** instead, since they're the slices or the bubbles, each with its own color.

## Swatches

Each swatch mirrors how its entry is drawn, so a reader can match them at a glance, without relying on color alone:

| Drawn as | Swatch |
| --- | --- |
| Bars | A small square |
| A line | A short bar |
| A `dashed` line | A short dashed bar |
| A `faded` series | The same, semi-transparent |
| `points` | A dot, in its `pointShape` |
| Pie, doughnut and bubble slices | A dot in the category's color |

```js
series: [
  { key: "orders", label: "Orders", values: orders },
  { key: "trend", label: "Trend", values: trend, display: "line" },
  { key: "target", label: "Target", values: target, display: "line", faded: true, dashed: true },
  { key: "rate", label: "Conversion", values: rate, axis: "secondary", display: "points", pointShape: "diamond" },
  { key: "visits", label: "Visits", values: visits, display: "none" },
],
```

Here the visits, drawn as `none`, are left out of the legend.

## Position

`legend-position` lines the entries up under the plot, from the `start` (default), in the `center`, or against the `end`. Entries wrap onto as many rows as they need on a narrow chart.

```html
<eun-line-chart legend-position="center"></eun-line-chart>
```

## Colors

A swatch takes its entry's color: the series' `color`, or else the palette (`--chart-color-1` to `--chart-color-8`, in series order), or the category's `color` on a pie, doughnut or bubble chart. Change a color once and the mark, the swatch and the tooltip follow.

The names take `--chart-axis-color`, shared with the axis labels.

## Custom appearance

Three parts restyle the legend from outside the chart:

| Part | Element |
| --- | --- |
| `legend` | The list of entries |
| `legend-item` | An entry, its swatch and its name |
| `legend-swatch` | The swatch of an entry |

```css
.sessions-chart::part(legend) {
  gap: 8px;
  margin-top: 12px;
}

.sessions-chart::part(legend-item) {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--eun-surface-sunken);
  font-weight: 600;
}

.sessions-chart::part(legend-swatch) {
  border-radius: 50%;
}
```

## Your own legend

When the legend has to live elsewhere (in a card's header, next to a filter), hide the chart's own with `legend="hidden"` and draw yours from the same data: give each series its `color`, so both use the same one.

```js
const series = [
  { key: "web", label: "Website", values: web, color: "var(--chart-color-1)" },
  { key: "app", label: "App", values: app, color: "var(--chart-color-2)" },
];
chart.legend = "hidden";
chart.data = { categories, series };
header.replaceChildren(
  ...series.map(({ label, color }) => {
    const item = document.createElement("span");
    item.style.setProperty("--dot", color);
    item.textContent = label;
    return item;
  }),
);
```

## Layout

The legend sits under the plot and adds one row (more when it wraps) to the chart's height, on top of `--chart-height`. With [`fill`](https://eunomia-ds.com/charts/size/#filling-its-parent), it's laid out inside the chart's height instead, the plot taking what's left.

Before a chart upgrades, `chart-ssr.css` reserves the plot's height, plus the legend's row once `legend="visible"`. With `legend="auto"`, the row only comes with the data, so a chart with several series shifts by one row on upgrade: set `legend="visible"` to reserve it up front.

## Accessibility

- The legend is `aria-hidden`: it repeats the series names the data table, read by screen readers, already holds.
- Swatches carry the way an entry is drawn (bar, line, dashed, points and their shape), so two series aren't told apart by color alone. Prefer `dashed`, `faded` or a `pointShape` for a series that must stand out.
