Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.6 — Legends, Symbols and Direct Labels
Data Visualization · Example 2 of 2

Direct labels that move apart, with leader lines

Westpark's line starts in May because defined skips its zero months. Remove the spread call (use ends instead of placed) to see the labels overlap. Look at Westpark's name: spreading pushes it below the axis line, the problem the last Pro note warns about.

A chart must explain its own colours, shapes and sizes. d3.symbol draws marker shapes, legends are small charts built with the same scales, and direct labels put the names on the lines themselves so the reader needs no lookup.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 4 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Make sizeLegend(values, sizeFn) that returns the d text for each value, using d3.symbolCircle.

02

Give every station symbol in the first example a hover title with the station name, its district and its docks.

Explain it without notes

01

What is the size argument of d3.symbol?

02

Why build a legend from the same scales as the chart?

03

What is a direct label and when does it stop working?

Direct labels that move apart, with leader lines

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

smallest gap between line ends: 1.3 px | labels need 13
after spreading: 13.0 px
labels moved: Old Town by 9.1px, Riverside by 9.5px, Hillcrest by 21.2px, Westpark by 11.7px