Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 13.4 — Trees, Treemaps, Packs and Sunbursts
Data Visualization · Example 2 of 3

The fleet as a treemap with labels that fit

Four rectangles are too small for their names. In the picture they keep a tooltip (hover to see it).

Some data is boxes inside boxes: a fleet is split into districts, stations and bike types. d3.hierarchy wraps that data in nodes, sum adds the values up, and a layout (treemap, pack, partition or tree) gives every node a position and a size.

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 the treemap show the e-bike share of each station as a second colour: a darker rectangle at the bottom of each cell, with a height proportional to the e-bikes.

02

Write labelFits(node, fontSize) that returns true only if the node's rectangle is wide and tall enough for its name, using getComputedTextLength of a temporary text element.

Explain it without notes

01

What does d3.hierarchy return, and what does sum add?

02

How is the data of a treemap different from the data of a force layout?

03

Why is the node's label dropped when a rectangle is small, and what keeps the information reachable?

04

A chart has twelve levels of nesting with padding at each level. What goes wrong?

The fleet as a treemap with labels that fit

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

station rectangles: 25 | labelled: 21 | too small for a label: Rope Walk, Clock Tower, Station Road, West Gate
biggest: Market 102x93 (20 bikes) | smallest: Clock Tower 43x62 (6 bikes)
area ratio 3.6 | bikes ratio 3.3 (rounding to whole pixels moves it a little)
every station inside its district: true | tiling: squarify (the default)