Code

D3.js

Transitions & Animations

Intermediate Animate attribute changes with selection.transition(), duration, and easing functions. Click Randomise to watch the bars animate to new values. d3-transition

// Initial render
bars = g.selectAll('rect')
  .data(data)
  .join('rect')
  .attr('y',      d => y(d))
  .attr('height', d => iH - y(d));

// On button click — update with transition
function randomise() {
  const newData = labels.map(
    () => 10 + Math.random() * 88
  );

  bars.data(newData)
    .transition()
    .duration(600)
    .ease(d3.easeCubicInOut)
    .attr('y',      d => y(d))
    .attr('height', d => iH - y(d));

  labels.data(newData)
    .transition()
    .duration(600)
    .ease(d3.easeCubicInOut)
    .attr('y', d => y(d) - 4)
    .text(d => Math.round(d));
}
Key APIs
  • selection.transition() — returns a transition object for animated attribute changes
  • .duration(ms) — animation length in milliseconds
  • .ease(easeFn) — easing function e.g. d3.easeCubicInOut, d3.easeElastic
  • .delay(ms) — per-element stagger delay (can be a function of d, i)
  • .on('end', fn) — callback when transition completes
  • d3.schemeTableau10 — a 10-colour categorical palette