liveBook · Manning

Layout

As explained in D3.js in Action, Third Edition

A D3 layout is a preprocessing function that transforms a dataset into an annotated dataset containing attributes needed to draw a visualization.

A D3 layout prepares data for a particular visualization. It takes a dataset and produces annotated data containing values needed to draw the chart. The layout does not draw anything: drawing comes afterward, using the calculated values. section 5.0

Choose a layout for the values your chart needs:

  • Use d3.pie() to calculate each slice’s start and end angles. Pass those angles to an arc generator to draw pie or donut slices. section 5.4
  • Use d3.stack() to calculate each series’ lower and upper boundaries. Those boundaries position stacked bars or guide an area generator for a streamgraph. section 5.4
  • Use pack() with hierarchical data to calculate circle centers and radii for a circle pack. section 11.2.1

Prepare a circle pack

Start with a hierarchical root. Call root.sum(d => d.total_speakers) to calculate sizes, configure pack().size([innerWidth, innerHeight]).padding(3), then pass the root to the generator. Its nodes now have x, y, and r values you can use to place SVG circles. section 11.2.1

Figure 11.5 To create a circle pack visualization with D3, we start with hierarchical data, often named root. To compute the layout, we call the root sum() method to calculate the size of the circles. Then we pass the root data structure to D3’s pack() layout generator. This generator appends each circle’s position and radius into the data. Finally, we append circles to an SVG container and set their position and size with the appended data.
figure

Follow the full handoff from hierarchical data, through computed circle positions and radii, to SVG circles. section 11.2.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines layout input and output, then walks through pie, stack, and pack configuration and their drawing handoffs.
Examples
Uses music-industry sales for pie and stacked charts and language-speaker data for a circle pack.
What it assumes
Introduces layout preprocessing from its dataset input and output; builds the pack example from an existing hierarchical root.
What you can do after
Apply it via d3.pie(), d3.stack(), or pack() to produce values used to draw chart shapes.
How it connects
Connects pie layouts to arc generators, stack layouts to area generators, and pack layouts to hierarchical data.

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.