d3js - enter(), update() and exit()
Switching between two JavaScript arrays of different lengths drives this d3.v3 demonstration of the enter, update, and exit data-join pattern. Clicking the “change data” button re-binds the circle elements to the alternate array, using `exit().remove()` for surplus circles, `enter().append()` for new ones with an initial radius of zero, and a 500-millisecond `transition` to update all circles’ positions and radii. The SVG rendering shows a horizontal axis and labeled dataset number, while circle size encodes each array value.
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.