Transitions
🎯 Introduction
Animation can make charts more engaging: bars grow from the baseline and smoothly change height when data updates. This article explains transition, including duration, delay, and easing, so changes feel fluid and natural.
🧱 What is a transition?
Without animation, attr changes happen immediately. A height jumps from 0 to 200, like flipping a light switch.
Insert .transition() before attr and D3 changes the attribute gradually over time:
rects.attr('height', 200); // Immediately set the height to 200
rects.transition().duration(800).attr('height', 200); // Grow from 0 to 200 over 800ms
Conceptually, those 800 milliseconds look like this:
At 0ms: height = 0
At 200ms: height ≈ 50
At 400ms: height ≈ 100
At 600ms: height ≈ 150
At 800ms: height = 200
D3 calculates intermediate values between the starting and ending values and applies them frame by frame. That is a transition.
Click the button to compare: the left bar jumps immediately, while the right bar changes smoothly with a transition:
🧱 Three animation controls
Chain three configuration methods after transition():
rects
.transition()
.duration(800) // Duration in milliseconds
.delay((d, i) => i * 100) // Stagger starts by 100ms per bar
.ease(d3.easeCubicOut) // Start fast and slow down
.attr('height', d => d);
- duration: How long the animation lasts, commonly 300–1000 milliseconds.
- delay: How long to wait before starting. With
(d, i) => i * 100, the first bar starts immediately, the second after 0.1 seconds, and the third after 0.2 seconds, producing a staggered entrance. - ease: How progress changes over time.
d3.easeCubicOutstarts fast and slows down, like a car braking, for a natural effect.
Choose an easing function and click “Play” to compare the three timing patterns:
🛠 Entrance animation: grow bars from the baseline
The pattern is simple: set the starting state before transition and the ending state after it.
svg.selectAll('rect')
.data(data)
.join('rect')
.attr('x', d => xScale(d.day))
.attr('width', xScale.bandwidth())
.attr('fill', '#5470c6')
.attr('y', height) // Starting position: the baseline
.attr('height', 0) // Starting height: zero
.transition() // Animate the attributes below
.duration(800)
.delay((d, i) => i * 100)
.attr('y', d => yScale(d.value)) // Ending position
.attr('height', d => height - yScale(d.value)); // Ending height
transition() marks a boundary: attributes before it define where to start; attributes after it define where to finish. Reversing these states reverses the effect, making bars appear to fall from above.Click “Replay” to watch the bars grow from the baseline one after another:
🛠 Update animation: smoothly change bar heights
When data changes, for example after clicking a refresh button, bind the new data and add a transition to move existing bars smoothly to their new heights:
function refresh() {
const newData = [
{ day: 'Mon', value: 90 },
{ day: 'Tue', value: 160 },
{ day: 'Wed', value: 220 },
{ day: 'Thu', value: 130 },
{ day: 'Fri', value: 180 },
];
svg.selectAll('rect')
.data(newData) // Positional join: all five bars belong to update
.transition()
.duration(600)
.attr('y', d => yScale(d.value))
.attr('height', d => height - yScale(d.value));
}
All five bars already exist, so D3 only needs to transition their y and height from old values to new ones. They expand or shrink smoothly rather than jumping abruptly.
Click “Refresh data” to watch the bars transition to new heights:
🪤 Things to watch out for
- Use animation sparingly: Entrance and data-update animations are usually enough. Constant movement can overwhelm users.
- Repeated triggers can conflict: If new data arrives before an animation finishes, bars may appear to jerk. Call
selection.interrupt()before starting a new animation to stop the old one. - Some values cannot be interpolated: Numeric coordinates, dimensions, and colors can transition smoothly, but values such as
fill: noneswitch directly.
❓ Questions and answers
Q1: Why does my animation finish instantly?
The starting and ending states may be identical or reversed. Check the attributes before and after transition().
Q2: Can axes animate?
Yes. When the domain changes, render the axis through a transition to move ticks smoothly: axisSelection.transition().call(d3.axisLeft(newYScale)).
Q3: Which easing functions are common?
d3.easeLinear uses constant speed, d3.easeCubicIn accelerates, d3.easeCubicOut decelerates, and d3.easeBounceOut bounces at the end. easeCubicOut is a natural everyday choice.
Q4: Do animations affect performance?
SVG animations with a few hundred elements are generally manageable. For tens of thousands of elements, consider Canvas; this is unlikely to matter in beginner examples.
🧪 Exercise
Add a “Refresh data” button to animate bar-chart updates:
- On click, use
Math.random()to generate five values between 0 and 200. - Bind the new data and add a 600ms transition.
- Click repeatedly and observe how the bars change height.
<template>
<button @click="refresh">Refresh data</button>
<svg ref="svgRef" class="chart"></svg>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import * as d3 from 'd3';
const svgRef = ref(null);
const refresh = () => {
// Write your code here
};
onMounted(() => {
// Write your code here (draw the initial bar chart)
});
</script>
<style scoped>
.chart {
width: 480px;
height: 280px;
}
</style>
🎉 Your charts now animate! Next, we will add mouse interactions and zooming so they can respond to users too.
