Skip to content
OpenChart

How to make a bar chart race in JavaScript

A bar chart race plays a ranking one period at a time. This post builds one from World Bank GDP data, swaps it to lines and a table, and exports a GIF.

1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024$0$5T$10T$15T$20T$25T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91TUnited Kingdom$3.69TFrance$3.16TItaly$2.38TCanada$2.24TBrazil$2.19T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024$0$5T$10T$15T$20T$25T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91TUnited Kingdom$3.69TFrance$3.16TItaly$2.38TCanada$2.24TBrazil$2.19T2024
The 10 largest economies, 1980-2024. Source: World Bank, GDP (current US$).

Made with OpenChart. How to build this race chart

China’s economy passed Japan’s in 2010 and reached $18.7 trillion in 2024, 65% of the United States’ $28.8 trillion, according to the World Bank. A bar chart race shows that climb a year at a time: the 10 largest economies are bars sorted by size, and the bars slide past each other as each year’s numbers arrive. In OpenChart a race is a spec with type: "race", three encoding fields and one row per country per year.

The race runs 31.5 seconds, 700 milliseconds for each of the 45 years. The control strip under the chart pauses, plays and scrubs to any year, and with focus on the strip the arrow keys step one year at a time.

The data: one row per entry per period

A race reads long data, one row for each country in each year:

const rows = [
  { country: "China", year: 2009, gdp: 5190e9 },
  { country: "China", year: 2010, gdp: 6193e9 },
  { country: "Japan", year: 2009, gdp: 5289e9 },
  { country: "Japan", year: 2010, gdp: 5759e9 },
  // ...one row per country and year
];

The World Bank’s download, like most spreadsheets, is wide: one row per country and a column per year. Reshape it before it goes in the spec:

const rows = wide.flatMap(({ country, ...byYear }) =>
  Object.entries(byYear)
    .filter(([, gdp]) => gdp !== "" && gdp != null)
    .map(([year, gdp]) => ({ country, year: Number(year), gdp: Number(gdp) })),
);

The filter matters because Number("") is 0, and a blank cell would otherwise rank as an economy of zero dollars.

Each country gets at most one row per year. Two rows with the same country and year stop the race with an error that asks you to aggregate them first, and the data needs at least two periods. A period can be a number, a date or a label such as "2023-Q3". Numbers and dates sort by value; labels keep the order they first appear in unless encoding.time.sort lists them.

A complete race spec

This spec races the five largest economies. encoding.time names the period field, encoding.key the field that identifies an entry from one year to the next, and encoding.value the number to rank by. format: "$.3~s" prints values such as $28.8T.

1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T.Only 8 economies have been in the top five since 1980GDP in current US dollars, 1980-2024$0$5T$10T$15T$20T$25T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T.Only 8 economies have been in the top five since 1980GDP in current US dollars, 1980-2024$0$5T$10T$15T$20T$25T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91T2024
1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T.Only 8 economies havebeen in the top five since1980GDP in current US dollars, 1980-2024$0$10T$20T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T.Only 8 economies havebeen in the top five since1980GDP in current US dollars, 1980-2024$0$10T$20T2024United States$28.8TChina$18.7TGermany$4.69TJapan$4.03TIndia$3.91T2024
Live example. Source: World Bank, GDP (current US$).

Made with OpenChart. How to build this race chart

{
  "type": "race",
  "mark": "bar",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "race": {
    "top": 5,
    "duration": 20000
  },
  "chrome": {
    "title": "Only 8 economies have been in the top five since 1980",
    "subtitle": "GDP in current US dollars, 1980-2024"
  },
  "data": [
    {
      "country": "China",
      "year": 1980,
      "gdp": 191000000000
    },
    {
      "country": "China",
      "year": 1981,
      "gdp": 196000000000
    },
    {
      "country": "China",
      "year": 1982,
      "gdp": 205000000000
    },
    {
      "country": "China",
      "year": 1983,
      "gdp": 231000000000
    },
    {
      "country": "China",
      "year": 1984,
      "gdp": 260000000000
    },
    {
      "country": "China",
      "year": 1985,
      "gdp": 310000000000
    },
    // … 354 more rows; Copy includes them
  ]
}
import { Race } from "@opendata-ai/openchart-react";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 360 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "bar",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "race": {
    "top": 5,
    "duration": 20000
  },
  "chrome": {
    "title": "Only 8 economies have been in the top five since 1980",
    "subtitle": "GDP in current US dollars, 1980-2024"
  }
};

export function Example() {
  return <Race spec={spec} />;
}
<script setup lang="ts">
import { Race } from "@opendata-ai/openchart-vue";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 360 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "bar",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "race": {
    "top": 5,
    "duration": 20000
  },
  "chrome": {
    "title": "Only 8 economies have been in the top five since 1980",
    "subtitle": "GDP in current US dollars, 1980-2024"
  }
};
</script>

<template>
  <Race :spec="spec" />
</template>
<script lang="ts">
import { Race } from "@opendata-ai/openchart-svelte";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 360 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "bar",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "race": {
    "top": 5,
    "duration": 20000
  },
  "chrome": {
    "title": "Only 8 economies have been in the top five since 1980",
    "subtitle": "GDP in current US dollars, 1980-2024"
  }
};
</script>

<Race {spec} />
import { createRace } from "@opendata-ai/openchart-vanilla";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 360 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "bar",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "race": {
    "top": 5,
    "duration": 20000
  },
  "chrome": {
    "title": "Only 8 economies have been in the top five since 1980",
    "subtitle": "GDP in current US dollars, 1980-2024"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);

The spec’s data keeps only the eight of the 20 economies that reach the top five in some year, 360 rows of the dataset’s 892, because a top-five race can never draw the other 12. For the top-10 race the same cut would keep 15 economies.

To put it on a page, pass a container and the spec to createRace:

import { createRace } from "@opendata-ai/openchart-vanilla";

const player = createRace(document.getElementById("race"), spec);

The race fills its container, so give the container a height. One with no height gets 520 pixels, 44 of them for the control strip.

Top N, speed and scale

race.top sets the number of slots, 10 by default. Each extra slot makes every bar thinner, so pick the smallest number that holds the story. Five slots show every change among the leaders. Ten add the climbs further down: China starts 12th in 1980 and India 13th, and India is 5th by 2024.

Playback defaults to 700 milliseconds per period, with the total kept between 8 and 90 seconds. Set the total in milliseconds with race.duration, as the spec above does with 20 seconds, or the time per period with race.step, but not both. The last frame holds for 1.5 seconds (race.holdEnd), and race.loop: true starts over after it.

By default each frame fits its scale to the bars on screen (race.rescale: "frame"), so the leader’s bar stays near full width and the others read as fractions of it. rescale: "fixed" keeps one scale for the whole run, which shows real growth: the United States’ bar in 1980 is a tenth of its 2024 length. race.rescale: { min } sets a floor on the frame’s maximum, so a race that starts small doesn’t open with bars stretched to full width.

Gaps, late starters and ties

A country with no rows before some year enters the race that year. The World Bank’s series for Russia starts in 1988, so Russia appears then. The World Bank publishes no Soviet series, so the 1980s rankings have no entry for the Soviet Union at all. Check what your data leaves out before you publish a ranking.

A missing year between two observed ones is filled by interpolating between them, the default race.gaps: "interpolate". "hold" carries the last value forward, and "drop" takes the entry out of the ranking until its next value.

Ties keep the previous period’s order. In this data, which is rounded to the billion, Canada and India both have $1,828 billion in 2012, tied for 10th. India was ahead in 2011, so India keeps 10th and Canada drops out. Entries with no earlier order fall back to alphabetical order by key.

Lines and tables

The same spec draws lines or a table when you change mark. mark: "line" traces each ranked economy up to the playhead, and the x axis widens as the years go by. mark: { type: "line", window: "all" } shows all 45 years from the start.

1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024$0$5T$10T$15T$20T$25T19801982198419861988199019921994199619982000200220042006200820102012201420162018202020222024United States $28.8TChina $18.7TGermany $4.69TJapan $4.03TIndia $3.91TUnited Kingdo… $3.69TFrance $3.16TItaly $2.38TCanada $2.24TBrazil $2.19T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024$0$5T$10T$15T$20T$25T19801982198419861988199019921994199619982000200220042006200820102012201420162018202020222024United States $28.8TChina $18.7TGermany $4.69TJapan $4.03TIndia $3.91TUnited Kingdo… $3.69TFrance $3.16TItaly $2.38TCanada $2.24TBrazil $2.19T2024
1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in2010 to become thesecond-largest economyGDP in current US dollars, the 10 largest of 20economies, 1980-2024$0$5T$10T$15T$20T$25T198020002024United StatesChinaGermanyJapanIndiaUnited KingdomFranceItalyCanadaBrazil20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in2010 to become thesecond-largest economyGDP in current US dollars, the 10 largest of 20economies, 1980-2024$0$5T$10T$15T$20T$25T198020002024United StatesChinaGermanyJapanIndiaUnited KingdomFranceItalyCanadaBrazil2024
The 10 largest economies as a line race, 1980-2024. Source: World Bank, GDP (current US$).

Made with OpenChart. How to build this race chart

{
  "type": "race",
  "mark": "line",
  "data": [
    {
      "country": "Argentina",
      "year": 1980,
      "gdp": 77000000000
    },
    {
      "country": "Argentina",
      "year": 1981,
      "gdp": 79000000000
    },
    {
      "country": "Argentina",
      "year": 1982,
      "gdp": 84000000000
    },
    {
      "country": "Argentina",
      "year": 1983,
      "gdp": 104000000000
    },
    {
      "country": "Argentina",
      "year": 1984,
      "gdp": 117000000000
    },
    {
      "country": "Argentina",
      "year": 1985,
      "gdp": 88000000000
    },
    // … 886 more rows; Copy includes them
  ],
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
}
import { Race } from "@opendata-ai/openchart-react";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "line",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};

export function Example() {
  return <Race spec={spec} />;
}
<script setup lang="ts">
import { Race } from "@opendata-ai/openchart-vue";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "line",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};
</script>

<template>
  <Race :spec="spec" />
</template>
<script lang="ts">
import { Race } from "@opendata-ai/openchart-svelte";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "line",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};
</script>

<Race {spec} />
import { createRace } from "@opendata-ai/openchart-vanilla";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": "line",
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);

The lines keep the history that bars throw away. Japan’s economy peaked at $6.27 trillion in 2012 and fell to $4.03 trillion by 2024, and both points stay on screen while the race goes on.

mark: "table" lists rank, the change in rank since the previous period, name and value, and the rows slide when the order changes. The default columns also include an inline bar and each entry’s share of the total. This one lists four columns so the names fit on a phone.

1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024RankChangeNameValue1United States$28.8T2China$18.7T3Germany$4.69T4Japan$4.03T5India$3.91T6United Kingdom$3.69T7France$3.16T8Italy$2.38T9↑1Canada$2.24T10↓1Brazil$2.19T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in 2010 to become the second-largest economyGDP in current US dollars, the 10 largest of 20 economies, 1980-2024RankChangeNameValue1United States$28.8T2China$18.7T3Germany$4.69T4Japan$4.03T5India$3.91T6United Kingdom$3.69T7France$3.16T8Italy$2.38T9↑1Canada$2.24T10↓1Brazil$2.19T2024
1. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in2010 to become thesecond-largest economyGDP in current US dollars, the 10 largest of 20economies, 1980-2024Rank↕NameValue1United States$28.8T2China$18.7T3Germany$4.69T4Japan$4.03T5India$3.91T6United Kingdom$3.69T7France$3.16T8Italy$2.38T9↑1Canada$2.24T10↓1Brazil$2.19T20241. United States, $28.8T. 2. China, $18.7T. 3. Germany, $4.69T. 4. Japan, $4.03T. 5. India, $3.91T. 6. United Kingdom, $3.69T. 7. France, $3.16T. 8. Italy, $2.38T. 9. Canada, $2.24T. 10. Brazil, $2.19T.China passed Japan in2010 to become thesecond-largest economyGDP in current US dollars, the 10 largest of 20economies, 1980-2024Rank↕NameValue1United States$28.8T2China$18.7T3Germany$4.69T4Japan$4.03T5India$3.91T6United Kingdom$3.69T7France$3.16T8Italy$2.38T9↑1Canada$2.24T10↓1Brazil$2.19T2024
The 10 largest economies as a table race, 1980-2024. Source: World Bank, GDP (current US$).

Made with OpenChart. How to build this race chart

{
  "type": "race",
  "mark": {
    "type": "table",
    "columns": [
      "rank",
      "change",
      "name",
      "value"
    ]
  },
  "data": [
    {
      "country": "Argentina",
      "year": 1980,
      "gdp": 77000000000
    },
    {
      "country": "Argentina",
      "year": 1981,
      "gdp": 79000000000
    },
    {
      "country": "Argentina",
      "year": 1982,
      "gdp": 84000000000
    },
    {
      "country": "Argentina",
      "year": 1983,
      "gdp": 104000000000
    },
    {
      "country": "Argentina",
      "year": 1984,
      "gdp": 117000000000
    },
    {
      "country": "Argentina",
      "year": 1985,
      "gdp": 88000000000
    },
    // … 886 more rows; Copy includes them
  ],
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
}
import { Race } from "@opendata-ai/openchart-react";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": {
    "type": "table",
    "columns": [
      "rank",
      "change",
      "name",
      "value"
    ]
  },
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};

export function Example() {
  return <Race spec={spec} />;
}
<script setup lang="ts">
import { Race } from "@opendata-ai/openchart-vue";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": {
    "type": "table",
    "columns": [
      "rank",
      "change",
      "name",
      "value"
    ]
  },
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};
</script>

<template>
  <Race :spec="spec" />
</template>
<script lang="ts">
import { Race } from "@opendata-ai/openchart-svelte";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": {
    "type": "table",
    "columns": [
      "rank",
      "change",
      "name",
      "value"
    ]
  },
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};
</script>

<Race {spec} />
import { createRace } from "@opendata-ai/openchart-vanilla";
import type { RaceSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 892 rows; Copy includes them */];

const spec: RaceSpec = {
  data,
  "type": "race",
  "mark": {
    "type": "table",
    "columns": [
      "rank",
      "change",
      "name",
      "value"
    ]
  },
  "encoding": {
    "time": {
      "field": "year",
      "type": "quantitative",
      "format": "d"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "value": {
      "field": "gdp",
      "type": "quantitative",
      "format": "$.3~s"
    }
  },
  "chrome": {
    "title": "China passed Japan in 2010 to become the second-largest economy",
    "subtitle": "GDP in current US dollars, the 10 largest of 20 economies, 1980-2024"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);

Playback and reduced motion

race.autoplay defaults to "visible", which starts the race when a quarter of it is in view. true starts it at mount, and false waits for the play button. Readers who have asked their system for reduced motion get the race paused on its last frame. They can still scrub, and pressing play steps through one year at a time instead of sliding. race.controls: false removes the control strip.

The player that createRace returns has play(), pause() and seek(index), which takes a zero-based period index; fractions land between periods. on("period", callback) fires each time the playhead crosses a period.

Screen readers get a description that names the first and last leaders, a table of the top entries by period, and a live region that announces the leaders when the reader pauses, steps or scrubs. It stays quiet during playback, so a screen reader isn’t reading a new ranking every 700 milliseconds.

For readers without JavaScript, or for an image in a newsletter, renderRaceStaticSVG(spec) from @opendata-ai/openchart-vanilla/static draws the final period as an SVG string without a browser. renderStaticSVG accepts race specs too. createRace adds the live race to its container without clearing it, so remove the static SVG before you mount the race.

In React, Vue or Svelte

Each framework package exports a Race component that takes the same spec:

import type { RaceSpec } from "@opendata-ai/openchart-core";
import { Race, type RaceHandle } from "@opendata-ai/openchart-react";
import { useRef } from "react";

export function GdpRace({ spec }: { spec: RaceSpec }) {
  const race = useRef<RaceHandle>(null);
  return <Race ref={race} spec={spec} style={{ height: 520 }} />;
}

race.current?.instance is the same player that createRace returns. Vue and Svelte import Race from @opendata-ai/openchart-vue and @opendata-ai/openchart-svelte.

Exporting a GIF for social posts

await player.export("gif") records the whole race, from the first period through a 1.5-second hold on the last, and returns a Blob. It needs the optional gifenc package (npm install gifenc) and works for bar and line races; switch a table race to bars first. The GIF is drawn at the race’s current size in pixels, so size the container to the image you want before you call it.

fps sets frames per second, 25 by default. A GIF gets at most 1,199 frames, so a race longer than about 48 seconds exports at a lower frame rate. The GIF loops when the spec sets race.loop: true and plays once otherwise. Export a chart as PNG, SVG or GIF has a short function that saves the Blob as a file.

Every race option, including the table columns and the scale floor, is on the race docs page.