Skip to content
Founding Lifetime: every Pro skillfirst 100 members · $169 onceUpgrade
Lookbk
Sign In

Bar chart race and animated charts: how to make them

A bar chart race re-sorts bars as values change over time. Build one from a CSV with GSAP or D3, render it to MP4, and animate bar and line charts.

For
Developers and analysts who want a ranking-over-time video from their data
Written for
Claude CodeCodexCursorGitHub CopilotGemini CLIOpenCodeAny agent that reads SKILL.md folders
Time
12 min read · updated Nov 20, 2026

The short answer

  1. A bar chart race is an animated bar chart of a ranking over time. Each bar is one name, its length is that name’s value at the moment shown, and the bars re-sort as the values change, so you watch one overtake another while a large label counts through the years or quarters.
  2. To make one, start from a table with a row per name and a column per period. Interpolate between the periods to get in-between frames, rank the names in every frame, then animate each bar’s length and its move from one rank to the next. Draw it with GSAP or D3 on a web page, and render the page frame by frame when you need an MP4. The same tools animate bar and line charts.

What a bar chart race shows

A bar chart race answers one question: who led, and when did the lead change hands? It suits a ranking across many periods, such as products by installs per quarter, cities by trips per year or teams by points per week. Each frame is an ordinary sorted bar chart. The motion between frames is what shows the overtakes.

When the question is different, pick a different animated chart:

Which animated chart fits which question
ChartShowsUse it when
Bar chart raceRank over timeSeveral names trade places over many periods
Animated bar chartA short series side by sideOne series by month or quarter, with one bar to point at
Animated line chartA trendOne number moving over time, with events along the way
Still bar chartOne momentThe ranking on one date, for a report or a post

The data: a row per name, a column per period

Lay the data out with one row per name and one column per period, and use 0 where a name has no value yet. Flourish’s bar chart race page describes the same layout, so one spreadsheet works for both routes. The numbers below are made-up sample data: six fictional bike-share networks and their trips per year, in thousands.

Sample data (made up)
name,2019,2020,2021,2022,2023,2024
Harbor,820,760,900,1010,1080,1120
Linden,640,700,880,1090,1260,1390
Mesa,300,420,610,780,990,1240
Northgate,710,690,720,760,800,830
Riverside,450,520,560,700,940,1180
Summit,150,260,400,650,880,1050

Mike Bostock’s Bar Chart Race, Explained notebook on Observable uses a long layout instead: one row per name and date, with date, name and value columns and an optional category for color. Either works. The code only needs a value for each name in each period.

How it works: keyframes, ranks and slides

Bostock’s notebook explains the method step by step. Four ideas do the work:

  1. Interpolate between periods. In the data, ranks change once per period. If a bar slid to its new rank over the whole period, many bars would drift at once and the race would be hard to follow. So the notebook splits each period into 10 keyframes, with every value interpolated in a straight line from one period to the next.
  2. Rank every keyframe. Sort the keyframe’s values from largest to smallest and number them. A rank is a whole number, so a bar’s rank jumps from one keyframe to the next, and animating between the two turns that jump into a short slide: the overtake.
  3. Rank the names below the cut-off too. The notebook shows 12 bars but ranks every name, and puts all the rest one row below the last visible bar. A name that climbs into the top group slides in from below, and one that drops slides out, instead of popping in or out.
  4. Rescale to the leader. In each keyframe the scale’s top is the leading bar’s value, so the leader always fills the width and the others are measured against it.

The notebook moves from keyframe to keyframe in 250 milliseconds with linear easing, so each period lasts 2.5 seconds and the race runs at a constant speed. Its value labels use tabular figures, digits of equal width, so the counting numbers don’t jitter.

Build it with GSAP: a complete page

This file is a whole bar chart race: the sample data, about 70 lines of JavaScript and GSAP 3.14 from a CDN. Save it as index.html and open it in a browser, and the race plays. It is also a HyperFrames composition, so it renders to MP4 without changes.

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Bar chart race</title>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js"></script>
    <style>
      body { margin: 0; background: #101218; color: #eef0f6; font: 600 30px/1 system-ui, sans-serif; }
      #root { position: relative; width: 1920px; height: 1080px; overflow: hidden; background: #101218; }
      h1 { position: absolute; left: 120px; top: 90px; margin: 0; font-size: 64px; }
      #chart { position: absolute; left: 120px; top: 230px; width: 1680px; height: 700px; overflow: hidden; }
      .bar { position: absolute; left: 0; top: 0; height: 110px; border-radius: 8px; }
      .bar span { position: absolute; top: 50%; transform: translateY(-50%); white-space: nowrap; }
      .name { left: 24px; color: #101218; }
      .value { left: calc(100% + 18px); font-variant-numeric: tabular-nums; }
      #period { position: absolute; right: 120px; bottom: 150px; font-size: 180px; opacity: 0.4; font-variant-numeric: tabular-nums; }
    </style>
  </head>
  <body>
    <div id="root" data-composition-id="main" data-start="0" data-duration="14.5" data-width="1920" data-height="1080">
      <h1>Bike-share trips per year, thousands (sample data)</h1>
      <div id="period"></div>
      <div id="chart" data-layout-allow-overflow data-layout-allow-overlap></div>
    </div>

    <!-- Made-up sample data: one row per name, one column per period. -->
    <script type="text/csv" id="data">
name,2019,2020,2021,2022,2023,2024
Harbor,820,760,900,1010,1080,1120
Linden,640,700,880,1090,1260,1390
Mesa,300,420,610,780,990,1240
Northgate,710,690,720,760,800,830
Riverside,450,520,560,700,940,1180
Summit,150,260,400,650,880,1050
    </script>

    <script>
      const TOP = 5 // bars on screen
      const STEPS = 10 // keyframes per period
      const SECONDS = 2.5 // seconds per period
      const ROW = 140 // pixels per rank: bar plus gap
      const WIDTH = 1400 // pixels for the leading bar
      const COLORS = ['#7cf2c3', '#f2a93b', '#7a9dff', '#e2683c', '#c9a0ff', '#f5d76e']

      // 1. Read the CSV.
      const [head, ...rows] = document
        .getElementById('data')
        .textContent.trim()
        .split('\n')
        .map((line) => line.split(','))
      const periods = head.slice(1)
      const names = rows.map((r) => r[0])
      const values = rows.map((r) => r.slice(1).map(Number))

      // 2. Keyframes: STEPS in-between frames per period, each one ranked.
      function keyframe(p, f) {
        const v = values.map((row) => row[p] * (1 - f) + row[p + 1] * f)
        const order = names.map((_, i) => i).sort((a, b) => v[b] - v[a])
        const rank = []
        order.forEach((i, r) => (rank[i] = Math.min(r, TOP))) // below TOP waits off screen
        return { v, rank, max: v[order[0]] }
      }
      const keyframes = []
      for (let p = 0; p < periods.length - 1; p++)
        for (let s = 0; s < STEPS; s++) keyframes.push(keyframe(p, s / STEPS))
      keyframes.push(keyframe(periods.length - 2, 1))

      // 3. One bar per name.
      const chart = document.getElementById('chart')
      const bars = names.map((name, i) => {
        const el = document.createElement('div')
        el.className = 'bar'
        el.style.background = COLORS[i % COLORS.length]
        el.innerHTML = '<span class="name"></span><span class="value"></span>'
        el.firstChild.textContent = name
        chart.append(el)
        return el
      })

      // 4. Draw any moment. t counts keyframes: 2.5 is halfway from keyframe 2 to 3.
      const lerp = (a, b, f) => a + (b - a) * f
      function render(t) {
        const i = Math.min(Math.floor(t), keyframes.length - 2)
        const f = t - i
        const a = keyframes[i]
        const b = keyframes[i + 1]
        const max = lerp(a.max, b.max, f)
        bars.forEach((el, n) => {
          const v = lerp(a.v[n], b.v[n], f)
          el.style.width = `${(v / max) * WIDTH}px`
          el.style.transform = `translateY(${lerp(a.rank[n], b.rank[n], f) * ROW}px)`
          el.style.zIndex = b.rank[n] < a.rank[n] ? 1 : 0 // a bar moving up passes in front
          el.style.visibility = a.rank[n] === TOP && b.rank[n] === TOP ? 'hidden' : 'visible'
          el.lastChild.textContent = Math.round(v).toLocaleString('en-US')
        })
        document.getElementById('period').textContent =
          periods[Math.floor(t / STEPS + 1e-6)]
      }

      // 5. One GSAP tween moves t from the first keyframe to the last.
      const clock = { t: 0 }
      const tl = gsap.timeline({ paused: true })
      tl.to(clock, {
        t: keyframes.length - 1,
        duration: (periods.length - 1) * SECONDS,
        ease: 'none',
        onUpdate: () => render(clock.t),
      })
      tl.to({}, { duration: 2 }) // hold on the final standings
      render(0)

      window.__timelines = window.__timelines || {}
      window.__timelines.main = tl // HyperFrames seeks this timeline frame by frame
      tl.play() // in a plain browser tab, just play it
    </script>
  </body>
</html>
  • keyframe() interpolates every value at one point between two periods and ranks the names. Every name past TOP gets rank TOP, one row below the chart, which overflow: hidden clips, so names slide in and out at the bottom edge.
  • render(t) draws any moment. t counts keyframes, and between two keyframes it interpolates both the values and the ranks, so a bar glides from one row to the next instead of jumping.
  • The leading value sets the scale, so the leader is always WIDTH pixels long and the others shrink against it as it grows.
  • One GSAP tween with ease: 'none' moves t from the first keyframe to the last, and its onUpdate calls render. GSAP can tween a property of any object, not only CSS, and none is its linear ease. Without it the tween takes GSAP’s default ease, power1.out, and the race slows down toward the end.
  • A bar moving up is drawn in front, so the one overtaking covers the one it passes. Names are set with textContent, so text in the CSV is never read as HTML.
  • There is no axis. The value printed at the end of each bar does that job; Bostock’s notebook draws one with D3’s axisTop if you want gridlines.
  • The data- attributes are for HyperFrames: those on #root set the video’s size and length, and the two on #chart tell its checker that bars crossing and waiting below the edge are on purpose. A browser ignores them.

In our test (Chromium 153 through Playwright 1.63, HyperFrames 0.8.192), the page played in a plain browser tab with no errors, and the period label read 2019, 2021 and 2024 at 1, 6 and 14.5 seconds.

Render it to an MP4

Everything on screen is a function of t, so a renderer can jump to any frame and get the same picture every time. HyperFrames does this: it finds the timeline the page puts in window.__timelines, seeks it to each frame in headless Chrome and encodes the frames with FFmpeg. In a folder with index.html, run:

Terminal
npx --yes hyperframes@0.8.192 check
npx --yes hyperframes@0.8.192 render -o renders/race.mp4

In our test on an M4 Mac mini, check passed and the 14.5-second race rendered in under 8 seconds to a 1920×1080 H.264 MP4 at 30 frames a second, about 0.7 MB. The frames matched the numbers the code computes: at 9.5 seconds, 80% of the way from 2022 to 2023, Linden read 1,226 and Harbor 1,066, and Summit had entered from below while Northgate had dropped out.

HyperFrames needs Node.js 22 or newer and FFmpeg. HTML to video covers the other ways to turn a page into an MP4, including a frame-by-frame script of your own with Playwright and FFmpeg, and why a screen recording can stutter.

The same race in D3

Bostock’s notebook builds the race with D3 transitions: for each keyframe it sets the scale, joins the top names to bars keyed by name, starts a 250-millisecond linear transition and waits for it to end before the next. D3’s own defaults are 250 milliseconds with cubic easing, which eases in and out of every keyframe, so the notebook sets linear easing.

D3 transitions run on d3-timer, which uses requestAnimationFrame and performance.now, so they play in real time. That suits a page someone watches. A frame-by-frame renderer has to set the time itself, so keep D3 for what it does well, scales and the data join, and call it from the same render(t). Replace steps 3 and 4 of the page above with this:

index.html: steps 3 and 4 with D3
// 3 and 4 with D3: an SVG, two scales, and a join keyed by name.
const svg = d3.select('#chart').append('svg').attr('width', 1680).attr('height', TOP * ROW)
const x = d3.scaleLinear().range([0, WIDTH])
const color = d3.scaleOrdinal(names, COLORS)
const lerp = (a, b, f) => a + (b - a) * f
function render(t) {
  const i = Math.min(Math.floor(t), keyframes.length - 2)
  const f = t - i
  const a = keyframes[i]
  const b = keyframes[i + 1]
  x.domain([0, lerp(a.max, b.max, f)])
  const rows = names.map((name, n) => ({
    name,
    value: lerp(a.v[n], b.v[n], f),
    y: lerp(a.rank[n], b.rank[n], f) * ROW,
    up: b.rank[n] < a.rank[n],
    off: a.rank[n] === TOP && b.rank[n] === TOP,
  }))
  svg
    .selectAll('g')
    .data(rows, (d) => d.name)
    .join((enter) => {
      const g = enter.append('g')
      g.append('rect').attr('height', 110).attr('rx', 8).attr('fill', (d) => color(d.name))
      g.append('text').attr('x', 24).attr('y', 66).attr('fill', '#101218').text((d) => d.name)
      g.append('text').attr('class', 'value').attr('y', 66).attr('fill', '#eef0f6')
      return g
    })
    .attr('transform', (d) => `translate(0,${d.y})`)
    .attr('visibility', (d) => (d.off ? 'hidden' : 'visible'))
    .call((g) => g.select('rect').attr('width', (d) => x(d.value)))
    .call((g) =>
      g.select('.value')
        .attr('x', (d) => x(d.value) + 18)
        .text((d) => d3.format(',d')(Math.round(d.value))),
    )
    .call((g) => g.filter((d) => d.up).raise()) // a bar moving up passes in front
  document.getElementById('period').textContent =
    periods[Math.floor(t / STEPS + 1e-6)]
}

Then load D3 after GSAP and give SVG text a font, in the <head>. Steps 1, 2 and 5 stay as they are.

index.html: add to the head
<script src="https://cdn.jsdelivr.net/npm/d3@7.9.0/dist/d3.min.js"></script>
<style>
  svg text { font: 600 30px system-ui, sans-serif; font-variant-numeric: tabular-nums; }
</style>
  • .data(rows, (d) => d.name) keys each bar by its name, so a bar stays with its name however the order changes.
  • selection.raise() moves an element to the end of its parent, and SVG draws later elements on top, so a rising bar passes in front.
  • In our test the D3 version showed the same values at the same moments as the GSAP one, played in a browser tab and passed hyperframes check.

Animated bar charts and line charts

Most other animated charts are simpler than a race: a few tweens on one timeline. Two common ones follow, both tested as one HyperFrames composition.

An animated bar chart

The bars grow from zero one after another while their values count up. gsap.from() animates from the values you give to the ones the element already has, so set each bar’s final height and tween from 0. stagger starts each bar a little after the one before, and snap rounds the counting number to a whole number on every frame, not only at the end.

JavaScript
// Columns: each one's final height is set here; the tween grows it from 0.
const quarters = [38, 52, 47, 68] // sample data
const max = Math.max(...quarters)
for (const v of quarters) {
  const col = document.createElement('div')
  col.className = 'col'
  col.style.height = `${(v / max) * 100}%`
  col.innerHTML = `<b>${v}</b>`
  document.getElementById('cols').append(col)
}

const tl = gsap.timeline({ paused: true })
tl.from('.col', { height: 0, duration: 0.8, ease: 'power2.out', stagger: 0.15 }, 0)
tl.from('.col b', { textContent: 0, snap: { textContent: 1 }, duration: 0.8, ease: 'power2.out', stagger: 0.15 }, 0)

#cols is a flex row with align-items: flex-end, so each column grows up from the baseline, and each value sits in a <b> placed above its column.

An animated line chart

Set pathLength="1" on the line so dash lengths are measured as a share of the whole path. A dash array of 1 is then one dash as long as the line, and a dash offset of 1 shifts it out of sight. Tweening the offset from 1 to 0 draws the line from start to end; at 0.5 half of it shows.

index.html (excerpt)
<svg viewBox="0 0 800 700">
  <path id="line" fill="none" stroke="#7cf2c3" stroke-width="8"
        pathLength="1" stroke-dasharray="1" stroke-dashoffset="1" />
</svg>
<script>
  // Line: one point every 130 px across, scaled up from the bottom.
  const points = [12, 18, 15, 26, 31, 29, 40] // sample data
  document.getElementById('line').setAttribute(
    'd',
    points.map((v, i) => `${i ? 'L' : 'M'}${i * 130},${680 - v * 15}`).join(' '),
  )
  tl.to('#line', { attr: { 'stroke-dashoffset': 0 }, duration: 3, ease: 'none' }, 0)
</script>

Tween the offset as an attribute, as above. GSAP rounds CSS pixel values to whole numbers during a tween by default, so tweening the CSS property strokeDashoffset from 1px to 0px jumps from hidden to fully drawn halfway through. That is what our first test did.

Without code

If you don’t need the code, online chart tools have bar chart race templates. Flourish, for example, takes a spreadsheet with a row per participant and a column per period, adds captions and images, and shares the race as an interactive chart by link or embed. If you need a video file, check the export options first: Flourish’s page lists PNG, JPEG and SVG downloads on its Presenter, Publisher and Enterprise plans. Bostock’s notebook can also be forked on Observable with your own data dropped in.

Let an agent build it

A coding agent such as Claude Code, Codex or Cursor can write the page above from your CSV and run the render. For a finished film rather than a starting point, Lookbk’s chart skills give the agent a designed template, a brief whose limits a script checks before anything renders, and the scripts that render it on your machine.

Ranking Race Film: an example of what it makes
video
Ranking Race FilmClaude CodeCodexCursor+3 more agents
Data Story: Bars: an example of what it makes
video
Data Story: BarsClaude CodeCodexCursor+3 more agents
Line Story Film: an example of what it makes
video
Line Story FilmClaude CodeCodexCursor+3 more agents
Infographic Poster: an example of what it makes
image
Infographic PosterClaude CodeCodexCursor+3 more agents
  • Ranking Race Film: a 32 to 36 second 1080p bar chart race of up to 10 names over 6 to 12 periods, with lifted overtakes, a period label that rolls like a counter, up to three callouts placed where no bar runs under them, a winner plate for the leader and an original soundtrack written by code. It refuses a brief until the data is either marked as illustrative or given a source line.
  • Data Story: Bars: a 12-second animated bar chart from a CSV. A script takes the first text column as the labels and the first numeric column as the values; the bars grow with counting values, a trend line draws through them and one bar gets a callout. Up to 12 bars, positive values, no soundtrack.
  • Line Story Film: a 40-second animated line chart told like a newspaper graphic. The camera follows the line as it draws, slows at each event you name and ends on the change from start to end, with a script that drafts the brief from a CSV and an original soundtrack.
  • Infographic Poster: for a still instead of a video, a one-page poster with bar, column and ring charts drawn as SVG from a CSV or JSON file, checked for overflow and contrast, as a PNG, a vector PDF and an HTML page.

The three films render with HyperFrames and need Node.js 22 or newer and FFmpeg, and HyperFrames downloads its own Chrome; the poster needs Node.js 18 or newer and Chrome. Animated chart videos lists every chart skill and the data each one takes.

Common problems

  • Two bars flicker past each other again and again

    Their values stay nearly equal for a while, so their ranks swap on many keyframes. Round the data, or keep the old order until the gap is bigger than a small share of the leader’s value.

  • Bars pop in or out instead of sliding

    Rank every name, not only the ones on screen, give the rest the first rank below the chart, and clip the chart with overflow: hidden.

  • The numbers jitter as they count

    Set font-variant-numeric: tabular-nums on the value labels, so every digit is the same width.

  • The race speeds up and slows down

    An ease is shaping the time. Use ease: 'none' on the GSAP tween that moves t, or d3.easeLinear on D3 transitions. GSAP’s default ease is power1.out, and D3’s is cubic.

  • The MP4 stutters but the page plays smoothly

    It was recorded in real time. Render it frame by frame from render(t) as above.

  • The line chart appears all at once

    GSAP rounded a CSS pixel value. Tween attr: { 'stroke-dashoffset': 0 }, or pass autoRound: false.

Common questions

What is a bar chart race?
An animated horizontal bar chart of a ranking over time. Each bar is a name, its length is the name’s value at that moment, and the bars re-sort as values change, so the viewer sees who overtakes whom and when the lead changes.
How long should each period last?
Bostock’s notebook uses 2.5 seconds per period, 10 keyframes of 250 milliseconds, and the page above does the same with SECONDS = 2.5. Six periods give 12.5 seconds of racing. For a long series, lower SECONDS.
How many bars should I show?
As many as stay readable at your video size. The notebook shows 12; the page above shows 5 of its 6 names so you can see one enter and one leave. Change TOP and ROW together so the bars fit the chart’s height.
Is the data in this guide real?
No. The bike-share networks and their numbers are made up. If you publish a race of real data, name the source on screen.
Can I make a bar chart race without writing code?
Yes. Online chart tools have bar chart race templates that take the same row-per-name table. A coding agent can also write and render the page above from your CSV, and Ranking Race Film does it as a finished film.

Sources

What this guide says about other companies’ tools comes from their own documentation, checked on Nov 20, 2026.

Did this guide help?

See a skill’s result before you install it.

Every skill page shows what it makes, what it needs and which agents can run it.