Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 44 ยท Projects and Practice

Project: A Drill-down Treemap with Breadcrumbs

Build a clickable treemap that drills into categories with an animated zoom, shows a breadcrumb trail, tooltips and a color legend, using d3.hierarchy and d3.treemap.

Hierarchical data such as a budget, a file system or a product catalog is best explored top-down: start with the big categories, click one to see its contents, click a breadcrumb to go back. In this project you build that interaction on top of d3.hierarchy and d3.treemap (Treemap, Partition and Pack).

The new ideas compared with a static treemap are:

  • the current root is part of the state; clicking changes it;
  • the layout is recomputed for the current root and animated from the old rectangles to the new ones;
  • a breadcrumb built from node.ancestors() lets users go back to any level.

Design

flowchart TD
  H["hierarchy: sum + sort"] --> S["state: current node"]
  S --> L["treemap layout for current node"]
  L --> D["join rectangles by name + transition"]
  S --> B["breadcrumb from ancestors"]
  D -->|"click on a group"| S
  B -->|"click on a crumb"| S

Navigation changes only the state; both the treemap and the breadcrumb are drawn from it.

A simple and reliable way to implement drill-down is to re-run the treemap layout on the clicked node's subtree, so its children fill the whole area. Because the node already carries its values from the parent's sum, we just call d3.treemap()(node.copy()). The copy() matters: layouts write x0, y0, x1, y1 onto the nodes, and we want each node's coordinates for the current view without disturbing the original tree.

The project

Drill-down treemap

Click a group (Education, Transport, ...) to zoom into it, click a crumb to climb back up. Leaves have no children, so they only show a tooltip.

Walking through it

  • State is just current, a node of the original hierarchy. show(node) does everything: layout, drawing and breadcrumb.
  • node.copy() clones the subtree, so the new layout never overwrites coordinates of the main tree. When you click, we look up the matching real child in current.children by name, because the tile's datum belongs to the temporary copy.
  • Keys (d => d.data.name) let tiles that stay (when going back up) slide to new positions, while new tiles start growing from the centre and old ones fade out in exit.
  • Labels are shown only if the rectangle is large enough; otherwise an empty string avoids text overflowing small tiles.
  • Colors come from the top-level ancestor, so all tiles inside "Health" share a hue at any depth. When we are already inside a group the whole view uses that group's color.
  • Breadcrumbs use node.ancestors().reverse() which lists the path from the root down to the current node. The current crumb is a disabled button.
  • Tooltips show the share of the current view, computed from view.value.

Extending the project

  1. Show parent labels as headers with paddingTop(18) and draw nested rectangles (all levels at once), as in a classic zoomable treemap.
  2. Add a toggle between treemap and sunburst using the same current node (Treemap, Partition and Pack).
  3. Support the keyboard: Enter on a focused tile drills in, Backspace goes up (Accessibility).
  4. Load the hierarchy from a CSV with d3.stratify, using path columns like Health/Hospitals.
  5. Add a legend with swatches for the top-level categories.
  6. Switch the metric (budget versus change from last year) with a select and a second sum accessor.

Common mistakes

  • Running the treemap on the original root repeatedly, so coordinates of hidden nodes overwrite those of visible ones. Copy the subtree.
  • Using the tile datum from the temporary copy to navigate; find the real node in the original tree.
  • No labels check: text spilling out of small tiles looks broken.
  • Forgetting an exit transition, so tiles disappear instantly and the zoom feels abrupt.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why call node.copy() before running d3.treemap on a subtree?

    The temporary copy receives the new x0, y0, x1 and y1 values for the drill-down view.
  2. 2. What does node.ancestors().reverse() give you?

    ancestors() starts at the node and goes up to the root; reversing it gives root first.
  3. 3. How are tiles matched between two views so that they slide instead of re-appearing?

    A key function d => d.data.name tells the join which tile is the same item in the new view.
  4. 4. When is it sensible to hide a tile's text label?

    Tiny tiles cannot fit text; leave the label out and provide the information in a tooltip.

Summary

  • Drill-down = a current node in state, a layout of its subtree, a keyed animated join and a breadcrumb from its ancestors.
  • Copy the subtree before applying a layout, and map clicks back to the real nodes.
  • Show labels only when they fit, and use tooltips for the rest.
  • Colors by top-level ancestor keep a hierarchy readable at every depth.

Next: Practice Exercises and Next Steps