Skip to main content
100%

General Update Pattern, II

✓ Published0🌍 Public
Mmbostock
Last edited Nov 17, 2019
Created on Sep 30, 2012

A subset of the alphabet is displayed as SVG text, and every 1.5 seconds, a new random subset replaces it. The data-join key ensures that letters already on screen are updated in place rather than recreated, changing their x-position and class, while new letters enter in green and removed letters exit. This example demonstrates the D3 v4 general update pattern using `selectAll`, `data`, `enter`, `merge`, and `exit` operations on the SVG root. The alphabet array is shuffled and sliced by `d3.interval` to generate the changing data.

AI-generated description

By adding a key to the data-join, letters that are already displayed are put in the update selection. Now updates can occur anywhere in the array, depending on the overlap between the old letters and the new letters. The text content only needs to be set on enter because the mapping from letter to element never changes; however, the x-position of the text element must now be recomputed on update as well as enter.

It'll be easier to see what's going on when we add animated transitions next!

Next: Update Transitions

Previous: General Update Pattern

gpl-3.0 Licensed

Similar vizzes