Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 44 ยท Transitions and Interaction

Transitions

Animate attribute and style changes with selection.transition(): duration, delay, chaining, named transitions, enter/exit animation in joins and events.

A chart that jumps from one state to another is hard to follow. A chart that moves between states lets the eye track what changed: a bar grows, a point slides, an old element fades out. D3 transitions do this by interpolating attributes and styles from their current value to a new value over time.

You create a transition from a selection with .transition(). Everything after it describes the end state; D3 reads the start state from the element itself.

JavaScript
d3.select("rect")
  .attr("width", 50)            // immediately
  .transition()
  .duration(800)
  .attr("width", 300);          // animated from 50 to 300 over 800 ms

Duration and delay

  • .duration(ms) sets how long it takes (default 250 ms).
  • .delay(ms) waits before starting (default 0).
  • Both accept a function (d, i, nodes), so each element can differ. The classic "staggered" effect is .delay((d, i) => i * 60).
Staggered bars

The bars start with zero height and grow one after another. Click the button to replay.

What can be transitioned

.attr() and .style() can be transitioned. Numbers, colors, transforms and path strings are interpolated automatically; .text() is set at the start (a transition cannot interpolate text unless you use .textTween). Properties like .classed() and .on() are not available on a transition, so set those on the selection before or after.

Chaining and sequences

Calling .transition() again on a transition schedules a new transition that starts after the previous one ends:

JavaScript
circle
  .transition().duration(600).attr("cx", 300)
  .transition().duration(600).attr("cy", 150).attr("fill", "tomato")
  .transition().duration(600).attr("r", 5);

Three steps run one after another. You can also react to lifecycle events: .on("start", fn), .on("end", fn), .on("interrupt", fn), and .on("cancel", fn). A promise is also available: await selection.transition().duration(500).attr("x", 10).end().

Interrupting and named transitions

Only one transition of a given name can run on an element at a time. If you start a new transition while one is running, the old one is interrupted. This is usually what you want, for example when the data changes again before the animation has finished.

Use names to run unrelated transitions in parallel: .transition("move") and .transition("fade") do not cancel each other. You can stop animations with selection.interrupt() or selection.interrupt("move").

Transitions in data joins

The big use case is animating data changes. With the modern selection.join() you pass functions for enter, update and exit:

Animated enter, update and exit

Study the three colours: new bars are green and grow from the bottom, remaining bars are blue and slide into their new place, leaving bars turn red, shrink and are removed with .remove() at the end of the transition. The key function d => d.k is essential: without it D3 matches by index, and bars would morph into the wrong categories.

Passing a transition to another transition (transition(t())) is a way to give several elements the same timing settings.

flowchart LR
  S["Start state: read from DOM"] --> I["Interpolator per attribute"]
  I --> T["Timer ticks: t from 0 to 1 (eased)"]
  T --> E["End state: your value"]
  T -->|"new transition with same name"| X["Interrupt"]

Each tick moves every attribute along its interpolator.

Common mistakes

  • Setting the start state after creating the transition. Set it on the selection before .transition().
  • Forgetting the key function in .data(), so animations connect the wrong elements.
  • Expecting .classed() or .on() to be transitioned. They apply instantly or are not available.
  • Not calling .remove() on exiting elements after their transition: they stay in the DOM invisible, collecting over time.
  • Animating thousands of elements: each has its own timer work. Animate fewer, or use canvas.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does D3 get the starting value of a transitioned attribute?

    The interpolator starts at whatever the element currently has, then goes to the value you gave after .transition().
  2. 2. How do you make bars start one after another?

    delay accepts a function of the datum and index, so each element can wait a different time.
  3. 3. What happens when a new transition with the same name starts on an element?

    Each element can have only one active transition per name; starting a new one interrupts the old one.
  4. 4. Why use a key function in .data(data, d => d.id)?

    Without a key, data is matched by index, so elements would animate to unrelated items.

Summary

  • selection.transition().duration().delay() animates attributes and styles to a new value.
  • Chain transitions to sequence them; use names to run them in parallel.
  • In join(enter, update, exit), animate entering, updating and exiting elements and remove exited ones at the end.
  • Always use a key function when data can change order.

Next: Easing and Interpolation