Skip to content
OpenChart

Bubble playback

Play a bubble chart year by year, Gapminder style, with fixed axes, named countries, trails and a large period label behind the bubbles.

A bubble playback moves every bubble from one period to the next, so readers watch countries travel across a scatter over decades. Use it when the path is the story, such as China’s life expectancy rising decades before its income did. To compare two moments side by side, use small multiples instead, since a playback shows one period at a time.

$10$100$1,000$10,000$100,000GDP per capita (current US$)1020304050607080Life expectancy (years)Asia & PacificAfricaAmericasEuropeChina and India got healthier long before theygot richGDP per capita and life expectancy, every other year from 1960 to 2022. Circle size =population.$10$100$1,000$10,000$100,000GDP per capita (current US$)1020304050607080Life expectancy (years)Asia & PacificAfricaAmericasEuropeChina and India got healthier long before theygot richGDP per capita and life expectancy, every other year from 1960 to 2022. Circle size =population.
$100$10,000GDP per capita (current US$)1020304050607080Life expectancy (years)Asia & PacificAfricaAmericasEuropeChina and India gothealthier long before theygot richGDP per capita and life expectancy, every other yearfrom 1960 to 2022. Circle size = population.$100$10,000GDP per capita (current US$)1020304050607080Life expectancy (years)Asia & PacificAfricaAmericasEuropeChina and India gothealthier long before theygot richGDP per capita and life expectancy, every other yearfrom 1960 to 2022. Circle size = population.
China's life expectancy rose from 33 to 64 years by 1980, while its GDP per capita was still under $200. Source: World Bank, World Development Indicators.
{
  "mark": "point",
  "data": [
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2000,
      "gdp": 175,
      "life": 55,
      "pop": 20.1
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2002,
      "gdp": 179,
      "life": 56.2,
      "pop": 21.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2004,
      "gdp": 222,
      "life": 57.8,
      "pop": 23.6
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2006,
      "gdp": 274,
      "life": 58.6,
      "pop": 25.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2008,
      "gdp": 382,
      "life": 59.7,
      "pop": 26.5
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2010,
      "gdp": 561,
      "life": 60.7,
      "pop": 28.3
    },
    // … 1,844 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India",
      "United States"
    ]
  },
  "chrome": {
    "title": "China and India got healthier long before they got rich",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India",
      "United States"
    ]
  },
  "chrome": {
    "title": "China and India got healthier long before they got rich",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India",
      "United States"
    ]
  },
  "chrome": {
    "title": "China and India got healthier long before they got rich",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};
</script>

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India",
      "United States"
    ]
  },
  "chrome": {
    "title": "China and India got healthier long before they got rich",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India",
      "United States"
    ]
  },
  "chrome": {
    "title": "China and India got healthier long before they got rich",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};

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

Add time and key to a bubble chart

A bubble playback is a bubble chart with two more channels. encoding.time names the field whose values are the periods, and encoding.key names the field that identifies each bubble from one period to the next. Supply one row per key and period.

gapminder.json
{
  "mark": "point",
  "data": [
    { "country": "China", "region": "Asia & Pacific", "year": 1960, "gdp": 90, "life": 33.4, "pop": 667.1 }
  ],
  "encoding": {
    "x": { "field": "gdp", "type": "quantitative", "scale": { "type": "log" } },
    "y": { "field": "life", "type": "quantitative" },
    "size": { "field": "pop", "type": "quantitative" },
    "color": { "field": "region", "type": "nominal" },
    "key": { "field": "country", "type": "nominal" },
    "time": { "field": "year", "type": "temporal" }
  },
  "playback": { "duration": 20000, "labels": ["China", "India", "United States"] }
}

Without key the spec fails validation, because nothing else ties China in 1960 to China in 1962. Two rows with the same country and year fail too, with a message asking you to aggregate them. Use mark: "point". A circle mark is a dot plot with a category on y, so it rejects the quantitative life expectancy axis.

The playback block sets the timing and controls, and the playback page lists every option with its default.

Axes that hold still

The axes, the size scale and the region colors are computed once over every period and stay fixed for the run, so a bubble moves only when its country’s numbers change. The x axis here runs from $10 to $100,000 in every frame, enough for Myanmar’s $12 in 1966 and the United States’ $76,657 in 2022.

GDP per capita grows by orders of magnitude, so x uses scale: { type: "log" } and equal steps right mean equal multiples of income. Between two periods a bubble moves along a geometric path on a log axis, so its motion matches the axis. A log axis can’t show zero or negative values, so playback treats them as missing and warns with the count.

A bubble chart without time draws a trend line by default. A time spec draws none, and an authored trendline is an error.

Name a few countries

playback.labels lists keys whose name stays beside their bubble for the whole run. A label is never hidden, so it never flickers on and off between frames. When it would cover another label, it moves to the other side of its bubble, or above or below it. Name the handful of countries the title is about rather than every one.

The large faint year behind the bubbles is the period label. It uses the time channel’s format; set playback.period.format to change it, or playback.period: false to hide it.

Trails

China's path ran up first, then right. This chart is drawn in the browser.

Each trail has a dot for every period the country has reached so far. Source: World Bank, World Development Indicators.
{
  "mark": "point",
  "data": [
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2000,
      "gdp": 175,
      "life": 55,
      "pop": 20.1
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2002,
      "gdp": 179,
      "life": 56.2,
      "pop": 21.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2004,
      "gdp": 222,
      "life": 57.8,
      "pop": 23.6
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2006,
      "gdp": 274,
      "life": 58.6,
      "pop": 25.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2008,
      "gdp": 382,
      "life": 59.7,
      "pop": 26.5
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2010,
      "gdp": 561,
      "life": 60.7,
      "pop": 28.3
    },
    // … 1,844 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India"
    ],
    "trails": [
      "China",
      "India"
    ]
  },
  "chrome": {
    "title": "China's path ran up first, then right",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India"
    ],
    "trails": [
      "China",
      "India"
    ]
  },
  "chrome": {
    "title": "China's path ran up first, then right",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India"
    ],
    "trails": [
      "China",
      "India"
    ]
  },
  "chrome": {
    "title": "China's path ran up first, then right",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};
</script>

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India"
    ],
    "trails": [
      "China",
      "India"
    ]
  },
  "chrome": {
    "title": "China's path ran up first, then right",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "China",
      "India"
    ],
    "trails": [
      "China",
      "India"
    ]
  },
  "chrome": {
    "title": "China's path ran up first, then right",
    "subtitle": "GDP per capita and life expectancy, every other year from 1960 to 2022. Circle size = population."
  }
};

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

playback.trails draws each named key’s path so far: a thin line in the bubble’s color through every period it has reached, with a small dot at each one. China’s trail climbs steeply until about 1980 and then runs right. trails: true draws one for every key, and above 30 keys the engine warns that the chart will be crowded.

South Africa lost nine years of life expectancy from 1990 to 2004. This chart is drawn in the browser.

South Africa's life expectancy fell from 62.9 years in 1990 to 54.0 in 2004, and its trail turns back down the chart before it climbs again. Source: World Bank, World Development Indicators.
{
  "mark": "point",
  "data": [
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2000,
      "gdp": 175,
      "life": 55,
      "pop": 20.1
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2002,
      "gdp": 179,
      "life": 56.2,
      "pop": 21.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2004,
      "gdp": 222,
      "life": 57.8,
      "pop": 23.6
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2006,
      "gdp": 274,
      "life": 58.6,
      "pop": 25.4
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2008,
      "gdp": 382,
      "life": 59.7,
      "pop": 26.5
    },
    {
      "country": "Afghanistan",
      "region": "Asia & Pacific",
      "year": 2010,
      "gdp": 561,
      "life": 60.7,
      "pop": 28.3
    },
    // … 1,844 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "South Africa",
      "Kenya"
    ],
    "trails": [
      "South Africa",
      "Kenya"
    ]
  },
  "chrome": {
    "title": "South Africa lost nine years of life expectancy from 1990 to 2004",
    "subtitle": "The 17 African countries in the data, every other year from 1960 to 2022. Circle size = population."
  },
  "transform": [
    {
      "filter": {
        "field": "region",
        "equal": "Africa"
      }
    }
  ]
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "South Africa",
      "Kenya"
    ],
    "trails": [
      "South Africa",
      "Kenya"
    ]
  },
  "chrome": {
    "title": "South Africa lost nine years of life expectancy from 1990 to 2004",
    "subtitle": "The 17 African countries in the data, every other year from 1960 to 2022. Circle size = population."
  },
  "transform": [
    {
      "filter": {
        "field": "region",
        "equal": "Africa"
      }
    }
  ]
};

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "South Africa",
      "Kenya"
    ],
    "trails": [
      "South Africa",
      "Kenya"
    ]
  },
  "chrome": {
    "title": "South Africa lost nine years of life expectancy from 1990 to 2004",
    "subtitle": "The 17 African countries in the data, every other year from 1960 to 2022. Circle size = population."
  },
  "transform": [
    {
      "filter": {
        "field": "region",
        "equal": "Africa"
      }
    }
  ]
};
</script>

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

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "South Africa",
      "Kenya"
    ],
    "trails": [
      "South Africa",
      "Kenya"
    ]
  },
  "chrome": {
    "title": "South Africa lost nine years of life expectancy from 1990 to 2004",
    "subtitle": "The 17 African countries in the data, every other year from 1960 to 2022. Circle size = population."
  },
  "transform": [
    {
      "filter": {
        "field": "region",
        "equal": "Africa"
      }
    }
  ]
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 1,850 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "point",
  "encoding": {
    "x": {
      "field": "gdp",
      "type": "quantitative",
      "scale": {
        "type": "log"
      },
      "axis": {
        "title": "GDP per capita (current US$)",
        "format": "$,.0f"
      }
    },
    "y": {
      "field": "life",
      "type": "quantitative",
      "axis": {
        "title": "Life expectancy (years)"
      }
    },
    "size": {
      "field": "pop",
      "type": "quantitative",
      "title": "Population (millions)"
    },
    "color": {
      "field": "region",
      "type": "nominal"
    },
    "key": {
      "field": "country",
      "type": "nominal"
    },
    "time": {
      "field": "year",
      "type": "temporal"
    }
  },
  "playback": {
    "duration": 20000,
    "labels": [
      "South Africa",
      "Kenya"
    ],
    "trails": [
      "South Africa",
      "Kenya"
    ]
  },
  "chrome": {
    "title": "South Africa lost nine years of life expectancy from 1990 to 2004",
    "subtitle": "The 17 African countries in the data, every other year from 1960 to 2022. Circle size = population."
  },
  "transform": [
    {
      "filter": {
        "field": "region",
        "equal": "Africa"
      }
    }
  ]
};

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

A trail shows a key moving backwards as plainly as forwards. South Africa’s climbs until 1990, drops until 2004 and then climbs again. Kenya’s drops by about five years over the same stretch.

Countries that appear and disappear

A country without values for a period is absent from it. Russia enters in 1988 and Afghanistan in 2000, the first years the World Bank reports their GDP per capita. Each fades in over the second half of the interval before it appears, and a country that stops reporting fades out over the first half of the interval after its last value. A missing period between two reported ones is filled in by default; playback.gaps sets that.

Hover and legend

Hovering a bubble shows the values from the nearest period, titled with that period, such as 1986, rather than numbers interpolated between two years. When the nearest period was filled in for a gap, the title says “estimated”. Clicking a region in the legend hides its bubbles without rescaling the axes.

Static output

compileChart, renderStaticSVG and a page without JavaScript draw the final period, with its year as the period label. Trails are drawn up to that period. To draw another period, pass t to renderStaticSVG:

static.ts
import { renderStaticSVG } from "@opendata-ai/openchart-vanilla/static";

const svg1990 = renderStaticSVG(spec, { width: 720, height: 500, t: 15 });

t counts periods from zero, and this data has one every other year, so t: 15 is 1990. A fraction draws the frame between two periods.

Fields that matter

Field What it does
mark "point".
encoding.x, encoding.y The two quantitative fields. scale: { type: "log" } suits income.
encoding.size A quantitative field mapped to bubble area.
encoding.color A category field. Every category keeps its color for the whole run.
encoding.key Required. The field that identifies each bubble across periods.
encoding.time The period field: temporal, ordinal or quantitative.
playback.labels Keys whose name stays beside their bubble.
playback.trails true or a list of keys whose path to draw.
playback.period The large period label’s format and position, or false.