join() reconciles a D3 selection with a dataset. Chained after selectAll() and data(), it creates one SVG element for each datum, binding each element to its data. section 3.3 section 3.6
selection
.selectAll("selector")
.data(myData)
.join("element to add");
Use the three-method pattern to connect a selection, dataset, and element type. section 3.3
The same pattern updates an existing visualization. When filtered data changes, join() determines which elements enter, update, or exit, so a scatterplot can add, retain, and remove circles with little code. section 7.4.2
For stable updates, handle the three selection states deliberately:
- Enter creates new elements and can initialize them before a transition. section 7.4.2
- Update retains elements already displayed and applies changed attributes or positions. section 7.4.2
- Exit represents elements no longer present and removes them from the DOM. section 7.4.2
- Pass a key function to data() when elements must keep representing the same datum across reordered or filtered data. section 7.4.2
Filtering a scatterplot
Filtering cetaceans changes the dataset passed to data(). Using d => d.uid as the key keeps surviving circles attached to the same cetaceans instead of matching elements by position. section 7.4.2
