Creating animated charts and graphs with SVG
SVG offers crisp visuals, direct DOM access, and powerful animation hooks for frontend developers and designers. In this guide, you’ll learn practical techniques to build animated
Creating animated charts and graphs with SVG
SVG offers crisp visuals, direct DOM access, and powerful animation hooks for frontend developers and designers. In this guide, you’ll learn practical techniques to build animated charts and graphs using scalable vector graphics, with small, actionable code snippets you can adapt in real projects. For deeper SVG patterns and design systems, see SVG Genius resources.
Why animated SVG charts?
Animations make data storytelling more engaging. With SVG you can:
- Animate paths and shapes without breaking accessibility or layout flow.
- Synchronize axes, bars, and points for coherent transitions.
- Leverage CSS or SMIL-like SMIL-free techniques for smooth updates.
Start small: animating a single bar or a line path often yields the biggest UX payoff without heavy libraries. If you’re curious about more advanced patterns, the SVG Genius site has curated patterns you can adapt.
Basic animated bar chart with SVG and CSS
Here's a compact, accessible bar chart that animates from zero to its value. It uses a simple width transition on the rect elements and a tiny CSS easing for polish.
<svg class="chart" width="600" height="180" role="img" aria-label="Animated bar chart">
<g transform="translate(40,20)">
<rect x="0" y="10" width="0" height="20" fill="#4f46e5" class="bar" style="transition: width .8s cubic-bezier(.2,.8,.2,1);" data-value="120" />
<rect x="0" y="40" width="0" height="20" fill="#8b5cf6" class="bar" style="transition: width .8s .05s cubic-bezier(.2,.8,.2,1);" data-value="85" />
<rect x="0" y="70" width="0" height="20" fill="#a78bfa" class="bar" style="transition: width .8s .10s cubic-bezier(.2,.8,.2,1);" data-value="60" />
</g>
</svg>
<script>
// Simple demo: animate widths based on data-value
const svgs = document.querySelectorAll('svg.chart');
svgs.forEach(svg => {
const bars = svg.querySelectorAll('.bar');
bars.forEach(bar => {
const value = bar.getAttribute('data-value');
// map data value to width in px (rough example)
const w = Math.max(0, Math.min(parseInt(value,10), 600));
requestAnimationFrame(() => {
bar.setAttribute('width', w);
});
});
});
</script>
Tip: keep the chart dimensions responsive by scaling the SVG with viewBox. This approach ensures animation remains crisp on high-DPI screens. For a ready-made approach, check out patterns on SVG Genius.
Animated line chart: path morphing and point highlights
Line charts convey trends. You can animate the line drawing and then reveal data points with a subtle scale-in. The key is to separate the path from the points so each part can animate independently.
<svg class="chart" viewBox="0 0 600 200" aria-label="Animated line chart">
<path d="M10,170 C80,120 180,60 260,90 C340,120 420,140 590,110" fill="none" stroke="#10b981" stroke-width="3" stroke-dasharray="1000" stroke-dashoffset="1000" class="trend" />
<g class="points">
<circle cx="10" cy="170" r="0" fill="#10b981"></circle>
<circle cx="80" cy="120" r="0" fill="#10b981"></circle>
<circle cx="180" cy="60" r="0" fill="#10b981"></circle>
<circle cx="260" cy="90" r="0" fill="#10b981"></circle>
<circle cx="340" cy="120" r="0" fill="#10b981"></circle>
</g>
</svg>
<script>
const line = document.querySelector('path.trend');
const circles = document.querySelectorAll('.points circle');
// Animate line drawing
line.style.transition = 'stroke-dashoffset 1s ease';
line.style.strokeDasharray = line.getTotalLength();
line.style.strokeDashoffset = line.getTotalLength();
requestAnimationFrame(() => {
line.style.strokeDashoffset = '0';
});
// Reveal points after a delay
circles.forEach((c, i) => {
c.style.transition = 'r .25s ease, fill .25s ease';
setTimeout(() => { c.setAttribute('r', 4); }, 600 + i * 120);
});
</script>
Notes for this pattern:
- Use stroke-dasharray and stroke-dashoffset to simulate "draw" effects on lines. This is a lightweight alternative to heavy canvas animations.
- Separate path and interactive elements for accessibility and hit-area control. See more examples at SVG Genius.
