Skip to main content
100%

General Update Pattern, III

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

This example demonstrates the general update pattern for animating simultaneous changes to a dataset. Using D3 v4, an SVG displays the alphabet, and every 1.5 seconds a randomly selected, sorted subset of letters replaces the current set. Each letter’s state is classified as entering, updating, or exiting, with distinct colors and positions. The code defines a shared `d3.transition` with a 750-millisecond duration, then applies it to separate selections via `.transition(t)` for entrance from the top, update movement horizontally, and exit downward with fading opacity. The data join uses a key function on the letter itself to maintain object constancy across updates.

AI-generated description

By adding transitions, we can more easily follow the elements as they are entered, updated and exited. Separate transitions are defined for each of the three states. To avoid repeating transition timing parameters for the enter, update, and exit selections, a top-level transition t sets the duration, and then subsequent transitions use selection.transition, passing in t to inherit timing:

var t = d3.transition()
    .duration(750);

Want to read more? Try these tutorials:

See the D3 wiki for even more resources.

Previous: Key Functions

gpl-3.0 Licensed

Similar vizzes