Race
Play a ranking over time as bars, lines or a table, and let readers pause on any period.
A race plays the top entries in a ranking one period at a time. Use it when the order of events is the story, such as which economies passed Japan and when. Use a bump chart to compare rank across several periods, or small-multiples line charts to compare how values changed for each series. Readers can scrub a race, but they can’t compare two moments that never share the screen.
Bar race
Set type: "race" and mark: "bar". Each row supplies one country’s GDP for one year. The race ranks the countries each year and moves the bars between ranks as the values change. encoding.value.format uses $.3~s to show values such as $4.03T.
{
"type": "race",
"mark": "bar",
"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",
"subtitle": "Annual GDP in current US dollars"
}
}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": "bar",
"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",
"subtitle": "Annual GDP in current US dollars"
}
};
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": "bar",
"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",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "bar",
"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",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "bar",
"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",
"subtitle": "Annual GDP in current US dollars"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);
Line race
Set mark: "line" to trace the ranked series up to the playhead. The x axis grows with the playhead by default. Use mark: { type: "line", window: "all" } to show the full time axis while the lines draw in.
{
"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": "US GDP grew tenfold",
"subtitle": "Annual GDP in current US dollars"
}
}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": "US GDP grew tenfold",
"subtitle": "Annual GDP in current US dollars"
}
};
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": "US GDP grew tenfold",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "US GDP grew tenfold",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "US GDP grew tenfold",
"subtitle": "Annual GDP in current US dollars"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);
Table race
Set mark: "table" when readers need the rank and value in columns. The default columns include an inline bar and each entry’s share of the total. Below 520px of table space, the bar hides and the Change heading becomes an arrow to leave more room for names. Use columns to pick which columns to show.
{
"type": "race",
"mark": "table",
"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 was No. 2 in 2024",
"subtitle": "Annual GDP in current US dollars"
}
}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": "table",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "China was No. 2 in 2024",
"subtitle": "Annual GDP in current US dollars"
}
};
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": "table",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "China was No. 2 in 2024",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "table",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "China was No. 2 in 2024",
"subtitle": "Annual GDP in current US dollars"
}
};
</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": "table",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "China was No. 2 in 2024",
"subtitle": "Annual GDP in current US dollars"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);
Loop the top five
race.top: 5 limits the ranking to five slots. race.loop: true restarts the race after the final hold, which lasts 1,500 milliseconds by default.
{
"type": "race",
"mark": "bar",
"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": "The US led every year",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"top": 5,
"loop": true
}
}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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "The US led every year",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"top": 5,
"loop": true
}
};
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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "The US led every year",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"top": 5,
"loop": true
}
};
</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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "The US led every year",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"top": 5,
"loop": true
}
};
</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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "The US led every year",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"top": 5,
"loop": true
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);
Fixed scale
The default race.rescale: "frame" fits the bar scale to each frame. Set "fixed" to hold that scale across the whole run. Early bars can look short when the values grow substantially over time. Line races fit the y axis to the history through the playhead.
{
"type": "race",
"mark": "bar",
"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": "US GDP reached $28.8T",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"rescale": "fixed"
}
}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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "US GDP reached $28.8T",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"rescale": "fixed"
}
};
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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "US GDP reached $28.8T",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"rescale": "fixed"
}
};
</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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "US GDP reached $28.8T",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"rescale": "fixed"
}
};
</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": "bar",
"encoding": {
"time": {
"field": "year",
"type": "quantitative",
"format": "d"
},
"key": {
"field": "country",
"type": "nominal"
},
"value": {
"field": "gdp",
"type": "quantitative",
"format": "$.3~s"
}
},
"chrome": {
"title": "US GDP reached $28.8T",
"subtitle": "Annual GDP in current US dollars"
},
"race": {
"rescale": "fixed"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createRace(container, spec);
Playback and controls
A race plays with the same clock, controls and keyboard as every other playback chart, so the playback page covers when it starts, the player, the keyboard and reduced motion. The timing fields (duration, step, holdEnd, loop, autoplay, controls, gaps and period) go in either the race block or a top-level playback block, with the same defaults. Setting one field in both blocks is an error.
Runtime API
In vanilla JavaScript, createRace(container, spec) returns the player. Call play() or pause() to control it, and seek(t) to move to a zero-based period index. Fractional indices land between periods. update({ ...spec, mark: "line" }) swaps the mark at the current playhead, as in the first example. on("period", callback) listens for period crossings and seeks and returns an unsubscribe function. Call destroy() when you remove the player.
Use setTheme({ theme, darkMode }) to replace the mount-level theme and color scheme during playback. Omit theme to use the spec’s theme, or darkMode to use light mode.
To render the final frame as a standalone SVG, use renderRaceStaticSVG(spec, options) from @opendata-ai/openchart-vanilla/static.
Install the optional gifenc package to export GIFs. await player.export("gif") returns a Blob of the full run for bar and line marks. To export a race shown as a table, switch it to bar or line first.
Fields that matter
| Field | What it does |
|---|---|
type |
"race". |
mark |
"bar", "line" or "table". Use an object for options such as a line’s window or a table’s columns. |
encoding.time |
The period field. Quantitative and temporal fields sort by value. Ordinal fields use the order in the data unless you set sort. |
encoding.key |
The field that identifies the same entry across periods. Supply one row per key and period. |
encoding.value |
The numeric field to rank and draw. Set format here for the value labels. |
race.top |
Number of ranked slots. Defaults to 10. |
race.swap |
Milliseconds a bar takes to move to its new rank. Defaults to 400. |
race.rescale |
For bars and inline table bars, "frame" fits each frame, "fixed" uses one scale, and { min } sets a floor on the frame maximum. |
race.summary |
"sum" shows the total of the top entries. Defaults to false. |
playback |
Timing and controls, shared with other playback charts. See Playback for each field and its default. |
Related
- Slope and bump for comparing ranks at a glance.
- Line for comparing values across periods.
- Playback for timing, controls and the keyboard.
- Motion for chart entrances and data updates.
- Export for other export formats.