liveBook · Manning

Stack generator

As explained in D3.js in Action, Third Edition

A D3 stack generator transforms component values into stacked series with lower and upper boundaries for each data point.

A stack generator is a D3 layout function that prepares multiseries data for stacked charts. Rather than drawing SVG itself, it transforms category values into an annotated dataset whose points hold lower and upper boundaries. Those boundaries determine where each series begins and ends in a stacked bar chart or streamgraph. section 5.0 section 5.2.1

Create one with d3.stack(), choose the value fields with keys(), and call the configured generator with the original data. The result is a multidimensional array: one array per series, identified by its key, containing one point per input record. Each point exposes its lower boundary as d[0], upper boundary as d[1], and the original record through d.data. section 5.2.1 section 5.4

Stack music-format sales

For yearly sales records with one field per music format, map format IDs into keys(), then pass the records to the generator. Each format becomes a stacked series. Bind the resulting series to SVG rectangles for stacked bars, or pass their d[0] and d[1] values to d3.area() for stacked areas. section 5.2.1 section 5.2.3 section 5.4

Configure the stack to control both appearance and scale requirements:

  • Use order() to choose vertical series ordering. The default follows the keys array; built-in alternatives reverse it, sort by total, arrange by peak appearance, or place earlier peaks toward the center. section 5.2.4
  • Use offset() to control the zero baseline and vertical distribution. Some offsets place all series above zero, while others distribute them above and below it. section 5.2.4
  • Recalculate the y-scale domain from the annotated lower and upper boundaries when an order or offset can introduce negative values or change extrema. section 5.2.4
Figure 5.14 The stack layout generator transforms data points into stacked data columns and returns an annotated dataset containing each data column’s lower and upper boundaries. Here we see an example for the year 1986.
figure

Inspect the transformation from input values to stacked columns and the lower and upper boundary pair returned for each point. section 5.2.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains configuration, nested output boundaries, SVG rendering, and order and offset controls.
Examples
Uses yearly music-format sales to build stacked bars and a streamgraph.
What it assumes
Builds on SVG containers, scales, the margin convention, and area generators.
What you can do after
Apply it via D3 to transform category fields into boundaries for stacked bars or areas.
How it connects
Connects stack layouts to area generators, linear scales, band scales, and SVG paths.

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.