Legend for several lines

On a line chart with several lines, the names are shown at the end of the lines. If you want them in a legend under the chart instead, add data-chart-legend to the figure. Each name has a short sample of its line.

Apples per tree
Year North South East
2016 41 30 22
2019 58 39 35
2022 77 52 41
2025 105 61 70
HTML
<figure data-chart="line" data-chart-legend>
	<figcaption><b>Apples per tree</b></figcaption>
	<table>
		<thead>
			<tr><th>Year</th><th>North</th><th>South</th><th>East</th></tr>
		</thead>
		<tbody>
			<tr><th>2016</th><td>41</td><td>30</td><td>22</td></tr>
			<tr><th>2019</th><td>58</td><td>39</td><td>35</td></tr>
			<tr><th>2022</th><td>77</td><td>52</td><td>41</td></tr>
			<tr><th>2025</th><td>105</td><td>61</td><td>70</td></tr>
		</tbody>
	</table>
</figure>

If you want to use a legend for every chart, set the legend option. Then data-chart-legend="false" turns it off for one chart.

JS
init({ legend: true });

Area, column and bar legends

In an area chart, data-chart-legend works the same way. Each name has a square of the colour of its area.

A grouped or stacked column or bar chart always has a legend under the chart, with a square for each name.

Donut legend

A donut chart always has a legend under the ring. See Donut.

Turn series off and on

With data-chart-legend="toggle", each name in the legend is a button, underlined as a link is. If you click a name, or press Enter or Space on it, its series is turned off, and the name gets a line through it. If you click it again, the series is turned on again. The last series that is on stays on.

Apples sold
thousands
Month Farm shop Market Online
Jan 34 41 120
Feb 38 45 140
Mar 42 53 180
Apr 51 49 210
HTML
<figure data-chart="column" data-chart-legend="toggle">
	<figcaption><b>Apples sold</b><br><span>thousands</span></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Farm shop</th><th>Market</th><th>Online</th></tr>
		</thead>
		<tbody>
			<tr><th>Jan</th><td>34</td><td>41</td><td>120</td></tr>
			<tr><th>Feb</th><td>38</td><td>45</td><td>140</td></tr>
			<tr><th>Mar</th><td>42</td><td>53</td><td>180</td></tr>
			<tr><th>Apr</th><td>51</td><td>49</td><td>210</td></tr>
		</tbody>
	</table>
</figure>

If a series is turned off, the chart is rendered again for the series that are left:

  • The scale fits the values that are left, so small values get more room.
  • In a group, the other columns or bars close up. In a stack, the total is the sum of the parts that are left.
  • The numbers are placed again, so a number that had no room can come back.
  • The tooltip shows only the series that are on.

The columns, lines, points and numbers that stay move to their new place. What is new fades in, and what is gone fades out. If the chart has no animation, or the reader asked for less motion, the chart changes at once.

The table does not change, so a screen reader still reads all values. Toggles work on line, area, column and bar charts with several value columns, and on a combo chart. If you want toggles on every chart with a legend, set the legend option to "toggle".

JS
init({ legend: "toggle" });

If a name has focus from the keyboard, it gets a focus ring. If the page has its own focus style for :focus-visible, the names use that style. If not, --tablechart-legend-focus-color, --tablechart-legend-focus-width and --tablechart-legend-focus-offset set the colour, width and space of the ring.

Toggles with two value axes

On a chart with two value axes, each axis has a scale for the series that are on at its side. If every series of one axis is turned off, the chart has one value axis, on the side of the series that are left. If you turn one of them on again, the second axis comes back.

In this example, rainfall and irrigation use the axis on the left, and temperature the axis on the right. If you turn off Temperature, the chart has one axis, on the left. If you turn off Rainfall and Irrigation, the temperature line keeps its axis on the right.

Water at the orchard
per month
Month Rainfall (mm) Irrigation (mm) Temperature (°C)
Apr 41 12 9.9
May 58 18 13.4
Jun 65 30 16.1
Jul 74 42 18.3
Aug 80 35 18.0
Sep 76 15 15.0
HTML
<figure data-chart="column" data-chart-grid data-chart-legend="toggle">
	<figcaption><b>Water at the orchard</b><br><span>per month</span></figcaption>
	<table>
		<thead>
			<tr><th>Month</th><th>Rainfall (mm)</th><th>Irrigation (mm)</th><th data-chart="line" data-axis="right">Temperature (°C)</th></tr>
		</thead>
		<tbody>
			<tr><th>Apr</th><td>41</td><td>12</td><td>9.9</td></tr>
			<tr><th>May</th><td>58</td><td>18</td><td>13.4</td></tr>
			<tr><th>Jun</th><td>65</td><td>30</td><td>16.1</td></tr>
			<tr><th>Jul</th><td>74</td><td>42</td><td>18.3</td></tr>
			<tr><th>Aug</th><td>80</td><td>35</td><td>18.0</td></tr>
			<tr><th>Sep</th><td>76</td><td>15</td><td>15.0</td></tr>
		</tbody>
	</table>
</figure>

Style the legend

A legend is as wide as its items. These variables set its background, border, padding and corners. By default, there are none.

Year North South
2016 41 30
2019 58 39
2022 77 52
2025 105 61
HTML
<figure data-chart="line" data-chart-legend style="--tablechart-legend-border: 1px solid currentColor; --tablechart-legend-padding: 0.3em 0.8em; --tablechart-legend-radius: 0.4em">
	<table>
		<thead>
			<tr><th>Year</th><th>North</th><th>South</th></tr>
		</thead>
		<tbody>
			<tr><th>2016</th><td>41</td><td>30</td></tr>
			<tr><th>2019</th><td>58</td><td>39</td></tr>
			<tr><th>2022</th><td>77</td><td>52</td></tr>
			<tr><th>2025</th><td>105</td><td>61</td></tr>
		</tbody>
	</table>
</figure>
Variable Sets
--tablechart-legend-surface The background
--tablechart-legend-border The border
--tablechart-legend-padding The space inside the border
--tablechart-legend-radius The corners
--tablechart-legend-gap The space above the legend of a line chart