liveBook · Manning

Circle pack

As explained in D3.js in Action, Third Edition

A circle pack is a hierarchical visualization that represents nested data with non-overlapping circles whose sizes encode values.

A circle pack shows a hierarchy as nested, non-overlapping circles. The outer circle is the root; each enclosed circle is a child node. Parent-child relationships are communicated through enclosure, while leaf-circle size can represent a chosen value. section 11.0 section 11.2

Figure 11.1 In hierarchical visualizations, the parent-child relationship can be communicated via enclosure (e.g., a circle pack or a treemap), connections (e.g., a tree chart), or with adjacency (e.g., an icicle chart).
figure

Compare enclosure with the connection-based tree chart and adjacency-based icicle chart when choosing how to show a hierarchy. section 11.0

Circle packs work well for taking in an overall hierarchical organization at a glance. They are visually approachable, but they use space less efficiently than treemaps and become hard to label when many circles are small. section 11.2 section 11.2.3

Build a D3 circle pack in this sequence:

  • Create a hierarchical root from CSV with child and parent columns using d3.stratify(), or from nested JSON using d3.hierarchy(). Obtain all nodes with descendants() and leaf nodes with leaves(). section 11.5
  • Call root.sum() with the leaf value to aggregate circle sizes, then configure d3.pack() with the chart size and padding. section 11.2.1
  • Run the layout generator on root. It adds each node’s depth, radius, and center coordinates (x and y). section 11.2.1
  • Bind descendants to SVG circles and set cx, cy, and r from the generated layout values. section 11.2.2
  • Use an ordinal color scale to distinguish first-level groups, and lighter related colors for the next level if useful. section 11.2.2
  • Label only circles above a minimum radius; use mouse interaction to reveal details for smaller leaf nodes. section 11.2.3

Languages by family and branch

A language hierarchy can place “Languages” at depth 0, language families at depth 1, branches at depth 2, and individual languages at depth 3. Leaf circles encode total speakers; family circles receive distinct colors, while hovering a language reveals its name, branch, family, and speaker count. section 11.2 section 11.2.2 section 11.2.3

For multiline labels inside circles, SVG foreignObject can contain ordinary HTML div elements. This allows long names to wrap, but limiting labels to larger circles preserves readability. section 11.2.3

How D3.js in Action, Third Edition covers it

How deep it goes
Walks through root aggregation, d3.pack() layout output, SVG drawing, color, labels, and hover details.
Examples
Builds a 100-language hierarchy colored by family, sized by total speakers, and augmented with hover information.
What it assumes
Starts with hierarchical CSV or JSON data and teaches creation of the root structure from either format.
What you can do after
Build it from scratch with D3 by converting hierarchy data into positioned SVG circles and interactive labels.
How it connects
Connects hierarchical data, d3.stratify(), d3.hierarchy(), treemaps, tree charts, icicle charts, ordinal scales, and SVG.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.