d3js - enter(), update() and exit()
A bar of five orange circles sits along a horizontal axis, with a button to swap between two data arrays of different lengths. Using D3 v3, the example demonstrates the data-join pattern: `enter()` appends new circle elements when longer data arrives, `exit()` removes surplus circles when data shrinks, and a `.transition()` updates all remaining circles’ radii and positions. The axis is drawn as simple SVG `line` elements with `text` labels, and the dataset label updates with each toggle.
AI-generated descriptionThis is a simple example of enter/update/exit in d3js.
2 arrays - of differing length - are used to generate alternating sequences of bubbles.
.enter() creates the initial join of data to elements, creating one circle element for every data element in the array.
After the button is clicked, the alternative array is rejoined to the graphical elements.
Because each array is of a different length, this means we must use .exit() to remove any circle elements no longer needed and create any new circle elements by using .enter().
Once all circle elements are properly defined, we can then update the radius of each circle, new and old, according to its value in the new array.
For more details on enter/update/exit, see this useful article by Mike Bostock, creator of d3js.
forked from <a href='http://bl.ocks.org/alansmithy/'>alansmithy</a>'s block: <a href='http://bl.ocks.org/alansmithy/e984477a741bc56db5a5'>d3js - enter(), update() and exit()</a>