Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 44 ยท Layouts and Hierarchies

Treemap, Partition and Pack Layouts

Show hierarchical sizes with d3.treemap, d3.partition (icicle and sunburst) and d3.pack, including tiling methods, padding, colors and labels.

A tree diagram shows structure. When the nodes also have a size (disk usage, budget, population), area-based layouts are better: the bigger the number, the bigger the shape. D3 ships three of them, all working on a d3.hierarchy that has been given values with .sum():

  • d3.treemap() fills a rectangle with nested rectangles.
  • d3.partition() makes slices in rings (a sunburst) or rows (an icicle chart).
  • d3.pack() nests circles inside circles.

If you have not met hierarchies yet, read Hierarchies and Tree Layouts first.

Preparing the data

All three layouts need node.value. Leaves carry the numbers; sum adds them up to every parent:

JavaScript
const root = d3.hierarchy(data)
  .sum(d => d.size)                    // leaves provide size
  .sort((a, b) => b.value - a.value);  // biggest first

Do not put a size on both a parent and its children unless you really want the parent's own value added on top. Normally only leaves have sizes.

Treemap

d3.treemap().size([width, height])(root) adds x0, y0, x1, y1 (the corners of a rectangle) to every node. Useful options:

  • .padding(n) or .paddingInner(n) / .paddingOuter(n) add space between and around rectangles. .paddingTop(20) leaves room for a parent label.
  • .round(true) snaps edges to whole pixels so no hairline gaps appear.
  • .tile(...) selects the algorithm: d3.treemapSquarify (the default, tries to make rectangles close to squares), treemapBinary, treemapSlice, treemapDice and treemapSliceDice.
A treemap of app storage

The scale d3.scaleOrdinal and the color scheme come from the scales chapter; here it simply gives each group its own hue.

Area is easy to compare roughly but hard to read exactly. Always add labels or tooltips for the numbers, and hide labels in rectangles too small to hold them (for example when d.x1 - d.x0 < 40).

Partition: icicle and sunburst

d3.partition().size([w, h])(root) gives each node a rectangle again (x0, x1, y0, y1), but arranged by depth: the root fills one row, its children share the next row proportionally to value, and so on. That is an icicle chart. Use it directly with <rect> elements.

A sunburst is the same data in polar form. Set the size to [2 * Math.PI, radius] so x0 and x1 are angles and y0 and y1 are radii, then feed each node into d3.arc():

A sunburst with d3.partition

The <title> child gives a free native tooltip. The helper topOf(d) colours every node by its top-level ancestor so related slices share a hue.

Circle packing

d3.pack().size([w, h])(root) assigns x, y (the centre) and r (the radius) to each node. Circles are less space-efficient than rectangles, but they show nesting very clearly, which suits things like topic clusters. Add .padding(3) for space between sibling circles.

JavaScript
const root = d3.hierarchy(data).sum(d => d.size);
d3.pack().size([400, 400]).padding(3)(root);

svg.selectAll("circle")
  .data(root.descendants())
  .join("circle")
  .attr("cx", d => d.x).attr("cy", d => d.y).attr("r", d => d.r)
  .attr("fill", d => d.children ? "#e7f5ff" : "#4dabf7")
  .attr("stroke", "#1c7ed6");

Draw parents before leaves (the order descendants() returns) so the small circles stay visible on top.

flowchart TD
  H["d3.hierarchy(data).sum(...)"] --> T["d3.treemap: x0 y0 x1 y1"]
  H --> P["d3.partition: x0 x1 y0 y1"]
  H --> K["d3.pack: x y r"]
  T --> R1["rect"]
  P --> R2["rect or d3.arc"]
  K --> R3["circle"]

One hierarchy, three layouts, three kinds of marks.

Choosing between them

  • Treemap: many leaves, you care about comparing sizes, space is limited.
  • Sunburst or icicle: the depth of the hierarchy matters; users can follow a path from the centre outward.
  • Circle packing: a small number of groups, and nesting is the main message.
  • Plain tree: no sizes, only structure.

Common mistakes

  • Forgetting .sum(): all values become undefined and the layout draws nothing useful.
  • Mixing up the partition coordinates: in a sunburst x is the angle and y the radius, the opposite of what you might expect.
  • Not sorting. Sorting by value usually makes treemaps and sunbursts much easier to read.
  • Drawing labels for all nodes. Filter out the ones too small to hold text.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which method must you call on a hierarchy before using d3.treemap?

    sum computes node.value from the leaf sizes. Without values the layout has nothing to size the rectangles by.
  2. 2. What do x0 and x1 mean after d3.partition().size([2 * Math.PI, radius]) is applied?

    With that size, the first dimension spans a full circle in radians, so x0 and x1 are angles you pass to d3.arc.
  3. 3. Which properties does d3.pack add to each node?

    Pack gives each node a circle centre (x, y) and a radius r.
  4. 4. What does .tile(d3.treemapSquarify) control?

    Tiling methods choose how rectangles are split; squarify aims for near-square rectangles.

Summary

  • Give a hierarchy values with .sum() and sort it, then apply a layout.
  • treemap gives rectangles (x0, y0, x1, y1), partition gives rows or arcs, pack gives circles (x, y, r).
  • Use padding, rounding and a good tiling method for readable treemaps; hide labels that do not fit.
  • Pick the layout based on whether size, depth or nesting is the main message.

Next: Force Simulations