Skip to main content
100%

D3 Grouped Selections

✓ Published2🌍 Public
CCurran Kelleher
Last edited Oct 9, 2025
Created on May 13, 2024

This example demonstrates a grouped selection pattern in D3, where the top 10 countries by population from the UN population estimates dataset are rendered as SVG groups, each containing a bar and a text label. The visualization shows a bar chart with country names on the x-axis and population values on the y-axis, using `d3.scaleBand` and `d3.scaleLinear`. The core technique uses `.selectAll('g.mark').join()` with an enter function that appends both a `rect` and `text` element per group, then updates their attributes via nested `.select()` calls. The code imports from `d3` and uses the `@curran/responsive-axes` library for axis handling.

AI-generated description

A response to the question:

Do you have a good example of a D3 grouped selection? I can never remember how to do the pattern and don't do it often enough. For example an SVG group (for each data element) that has say a bar and a label within it. Appending is easy, joining is where it gets a bit more gnarly

MIT Licensed

Similar vizzes