Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 44 ยท Selections and Data Joins

Nested Selections and Grouped Data

Build tables and grouped marks with nested D3 selections: rows then cells, how data flows from parent to child, selectAll versus select, and parents and groups.

Real data is often two-dimensional: an array of rows, each containing an array of values; or an array of series, each with its own points. D3 handles this with nested selections: you join the outer array to parent elements, then join each parent's inner array to its children. This is the idiom behind HTML tables, grouped bar charts, small multiples and multi-line charts.

A table from a 2D array

To draw a table, first create one <tr> per row, then one <td> per value inside each row. The second data() call uses a function that receives the row datum (the parent's d) and returns the inner array.

JavaScript
const rows = d3.select("tbody")
  .selectAll("tr")
  .data(matrix)
  .join("tr");

rows.selectAll("td")
  .data(row => row)       // each row is an array
  .join("td")
  .text(d => d);

Here rows.selectAll("td") returns one group per tr, and .data(row => row) binds each tr's own datum (the row array) to the tds in that group. In effect the data flows through two levels.

A table from a matrix

select versus selectAll for data inheritance

This is the single most confusing point of nested selections, so read it twice.

  • selection.selectAll(selector) finds descendants and groups them by parent. It does not pass the parent's datum down. You must call .data() yourself, usually with a function of the parent datum, as in the table.
  • selection.select(selector) finds the first descendant of each element, keeps the same grouping, and propagates the parent's datum to the child. Use it to update one known child (a rect or text inside each g).
  • selection.append(tag) also propagates the datum.
JavaScript
bars.select("rect").attr("width", d => d.value);    // d is the bar's datum (inherited)
bars.selectAll("rect").attr("width", d => d.value); // d is undefined: nothing was bound to these

The second line is a common bug: d is undefined, the attribute becomes NaN, and the shapes vanish.

select inherits data, selectAll does not

The rectangles were created with append, so they hold their parent's data and d.v works. The circles were created outside D3 and selectAll does not copy data to them, so their datum is undefined and d.v throws a TypeError.

Multi-series data: one group per series

For several series (e.g. one line per country), join the series array to g elements, then join each series' points to marks inside. The inner join uses d => d.values.

JavaScript
const series = svg.selectAll("g.series")
  .data(seriesData, d => d.name)
  .join("g")
  .attr("class", "series")
  .attr("fill", d => color(d.name));

series.selectAll("circle")
  .data(d => d.values)
  .join("circle")
  .attr("cx", p => x(p.x))
  .attr("cy", p => y(p.y))
  .attr("r", 4);

The color is set once on each g and inherited by every circle, which keeps code short and CSS-friendly.

Two series of points

(d3.scaleLinear([1, 4], [40, 380]) is the short form of .domain().range(); scales come next in Scales.)

Parents and groups under the hood

A selection is really an array of groups, where each group is an array of nodes, and a parents array records which element each group belongs to. d3.selectAll produces one group; a nested selectAll produces one group per parent. Methods like selection.each(function(d, i, nodes) {...}) give you i and nodes relative to the group, so i restarts at 0 in each parent. That is why in a grouped bar chart, (d, i) => ... often gives the position within the group, which is what you want to offset bars inside a cluster.

If you need the parent's index or datum from a child accessor, use a closure: store the parent datum in the inner function (row => ... inside data(...)), or read it with this.parentNode.__data__.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you bind each row of a matrix to its cells in a table?

    selectAll groups cells by row, and data(row => row) binds each row's own array to the cells in that group.
  2. 2. Why is d undefined in rows.selectAll('rect').attr('width', d => d.value) when nothing was bound to the rects?

    Only select and append copy the parent's datum to children. selectAll groups elements but requires its own data() call.
  3. 3. In a nested selection, what does the index i represent in an accessor?

    Accessors receive the index within the current group, which restarts at 0 for each parent.
  4. 4. What is a good way to share a color among all marks of one series?

    Presentation attributes such as fill are inherited by descendants, so one attribute on the g covers every child.

Summary

  • Nest joins: join the outer array to parents, then selectAll(child).data(d => d.inner) for children.
  • select and append pass the parent's datum down; selectAll does not.
  • A selection is an array of groups; the accessor index i is relative to the group.
  • Group elements share attributes such as fill with their children through inheritance.

Next: Data Utilities: d3-array