Skip to content
◀ Blog

Direct labels or a legend on a line chart

Label line chart series directly when you have a few and room at the right edge. Use a legend for many series or toggling. Tested on five real CPI lines.

Riley Hilliard8 min read

openchartdata-visualization

Label the lines directly when the chart has a handful of series and some room to the right of the last point for names. Use a legend when there are too many series for that margin, when you want readers to switch series on and off, or when the names are too long to sit beside a line. The five-line chart below is a clear case for direct labels.

What changes when the legend goes away?

Both figures draw the same data: the all-items consumer price index and four of its components (transportation, shelter, food and beverages, medical care), seasonally adjusted and rescaled so January 2019 equals 100. The lines cross several times, which is what makes the legend version tiresome. Prices for transportation fell to 93.5 in April 2020, the lowest quarterly point shown (May 2020 was lower, at 91.7), climbed above the other four by 2022, and sit highest in September 2026 at 143.4.

The lines connect quarterly readings (January, April, July, October) plus September 2026, so short peaks between them don’t show.

2020202590100110120130140150Index, January 2019 = 100TransportationShelterFood and beveragesAll itemsMedical care
Fig. 1 Five CPI components with a color key above the plot. To read the top line you look up its color, then look back at the chart. Source: U.S. Bureau of Labor Statistics via FRED, composed by OpenData (opendata/us-cpi-components), queried 2026-11-19, seasonally adjusted; quarterly values plus September 2026

To say which line is shelter in Fig. 1, you find the cyan swatch in the key, then go back to the plot and hunt for cyan among the shelter, food and all-items lines, which stay within about five points of each other from 2022 on. To answer the same question in Fig. 2, you read the name where the line stops.

2020202590100110120130140150Index, January 2019 = 100Transportation143Shelter 138Food andbeverages 136All items 133Medical care121
Fig. 2 The same data with each line named at its last point, with September 2026's value beside the name. The key is gone. Source: U.S. Bureau of Labor Statistics via FRED, composed by OpenData (opendata/us-cpi-components), queried 2026-11-19, seasonally adjusted; quarterly values plus September 2026

Datawrapper’s guidance for line charts is to remove the color key and label the categories directly, so readers don’t have to travel back and forth between key and chart. It does this by default for line charts on desktop.

Fig. 2 also carries a number the legend version can’t: each label ends with the September 2026 value, so you can read that transportation ended at 143 and medical care at 121 without tracing to the y-axis.

Which part of the spec makes the difference?

Two spec options differ between the figures. The line mark, the data, the encodings and the color scale are shared. This is the fragment the legend chart adds to an OpenChart spec.

{
  "legend": {
    "show": true,
    "position": "top"
  },
  "endpointLabels": false
}

And this is what the direct-label chart adds instead.

{
  "legend": {
    "show": false
  },
  "endpointLabels": {
    "content": "label value",
    "format": ".0f"
  }
}

content: "label value" puts the series name and its last value in one entry, and format rounds that value to a whole number. The entry wraps at 110 pixels by default, so “Food and beverages 136” takes two lines in Fig. 2. A width option raises the limit, at the cost of plot width.

What happens when the line ends are close together?

Look at the right edge of Fig. 2. Shelter ends at 137.6, food and beverages at 135.5 and all items at 133.2. On this chart’s 60-point axis, those three rings sit about ten pixels apart, and three labels with two lines of text each would overlap if each sat exactly at its line’s end. OpenChart spreads the labels vertically instead, keeping them in the same order as the lines, and draws a small ring at each line’s true endpoint. In Fig. 2 the food label sits a little below its ring. The OpenChart docs list a showLeader option that draws a connector from a displaced label to its ring. I left it off here and haven’t rendered it, so I can’t tell you how it looks with five lines.

There is a limit. If you fix frame.right to a width the label column can’t fit, the docs say OpenChart demotes the column to a legend even when you asked for labels. Series whose endpoints are close together, relative to the axis range, are the ones that push labels away from their lines.

Check the chart with your real data before you commit to labels.

When is a legend the better choice?

A legend wins in three situations.

There are many series. A right margin holds only so many names. OpenChart’s automatic endpoint labels cover two through eight series, and above eight it falls back to a legend. I checked that threshold against the 9.0.0 package this site uses: eight series got labels and nine got a legend. Eight labels is already a stretch on a chart this size, so my own cutoff is closer to five or six.

Readers need to toggle series. In OpenChart’s interactive charts, clicking a legend entry hides or shows that series. A legend doubles as a control there, and endpoint labels don’t.

The chart is small. Datawrapper’s line chart guide says that on mobile screens all line labels move into a color legend at the top. More on that below.

A legend can also lose entries. At phone width, OpenChart’s top legend truncates after two rows and replaces the rest with “+1 more.” In my first mobile render of Fig. 1, medical care was the entry that vanished. The maxRows option on legend raises the limit, and I set it to 3 for the phone version of Fig. 1.

What do you do with eight or more series?

Neither option works well, because a legend with eight colors is hard to decode and eight labels at the line ends overlap.

Two approaches hold up.

Highlight one line and mute the rest. In OpenChart, seriesStyles does this per series, keyed by the value of the color field.

{
  "seriesStyles": {
    "Transportation": { "strokeWidth": 3.5 },
    "Shelter": { "opacity": 0.3 },
    "Food and beverages": { "opacity": 0.3 }
  }
}

I validated a spec like this and rendered it against the five series above, and the thick and faded strokes came out as written. I didn’t build an eight-series figure. Datawrapper’s piece on using fewer colors shows this with the other lines in grey and direct labels on the ones that matter. It also notes that direct labels let categories share similar colors and stay distinguishable, which a legend can’t do.

Split the chart into small multiples, one panel per series. The same article describes the tradeoff: each category’s trend gets easier to read, and comparing values across categories gets harder. If readers care about the crossings, as they do in Fig. 1, keep the lines together and highlight. If they care about each series on its own, split them.

Do direct labels help readers who can’t tell the colors apart?

They help with matching. Datawrapper’s guide to colorblind-friendly charts says color keys are hard for colorblind readers to decipher and recommends labeling line, area and pie charts directly. The label sits next to the line, so position does the matching and hue doesn’t have to.

Direct labels only cover the matching problem near the line end. A reader still has to follow a line through the crossings in the middle of Fig. 2 by color. The same guide recommends a second visual variable, such as dotted lines or symbols. OpenChart’s accessibility docs point to strokeDash on the encoding for telling line series apart without color. I haven’t run the five colors in these figures through a color-blindness simulator, so I won’t claim they pass.

How do direct labels behave on a phone?

They cost width.

The phone version of Fig. 2 is 340 pixels wide. The plot gets 151 of them, about 44 percent, and the axis and label column take the rest. The lines are still readable, but the crossings are squeezed.

When labels are automatic and the chart is too narrow for the label column, OpenChart shows a legend instead. I rendered a five-series chart at 340 pixels with no endpointLabels set, and it came back with a legend and no endpoint labels. An explicit endpointLabels setting keeps the labels, unless you also fix frame.right to a width the column can’t fit. These figures set it explicitly, which is why Fig. 2 still has labels on a phone.

If the reader mostly wants the ranking at the end, labels with values are worth the squeeze. If the shape of the middle matters, take Datawrapper’s route and let a legend at the top give the plot its width back.

Does OpenChart do this for you?

OpenChart is free, open source (Apache-2.0) and drew every chart on this page at build time from a JSON spec. Direct labels are the default there for line charts and overlapping area charts with two to eight series, and the two fragments above are the whole switch between the styles. The docs describe the label content, format, width and leader options, and you can turn the legend on beside the labels with legend: { show: true }. If you already have a charting tool that does this, nothing here depends on OpenChart. The advice is the same in any of them.