Theming and tokens
ThemeConfig colors, fonts, spacing and rules for every chart, and the CSS tokens a page can set instead.
A theme sets a chart’s colors, type and spacing, and leaves its data and encoding alone. This chart has no theme field, so it draws in the library default: Inter, a transparent background and the six-hue palette.
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2017",
"source": "Coal",
"share": 30.7
},
{
"year": "2017",
"source": "Gas",
"share": 33
},
{
"year": "2017",
"source": "Nuclear",
"share": 20.5
},
// … 18 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
}
};
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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A custom theme
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2017",
"source": "Coal",
"share": 30.7
},
{
"year": "2017",
"source": "Gas",
"share": 33
},
{
"year": "2017",
"source": "Nuclear",
"share": 20.5
},
// … 18 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"colors": {
"categorical": [
{
"light": "#8c2d19",
"dark": "#e07a5f"
},
{
"light": "#1f5c99",
"dark": "#7fb0e0"
},
{
"light": "#7a7267",
"dark": "#a8a095"
}
],
"background": {
"light": "#f7f3ea",
"dark": "#1c1a17"
}
},
"fonts": {
"family": "\"Source Serif 4\", Georgia, serif"
},
"spacing": {
"padding": 28
}
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"colors": {
"categorical": [
{
"light": "#8c2d19",
"dark": "#e07a5f"
},
{
"light": "#1f5c99",
"dark": "#7fb0e0"
},
{
"light": "#7a7267",
"dark": "#a8a095"
}
],
"background": {
"light": "#f7f3ea",
"dark": "#1c1a17"
}
},
"fonts": {
"family": "\"Source Serif 4\", Georgia, serif"
},
"spacing": {
"padding": 28
}
}
};
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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"colors": {
"categorical": [
{
"light": "#8c2d19",
"dark": "#e07a5f"
},
{
"light": "#1f5c99",
"dark": "#7fb0e0"
},
{
"light": "#7a7267",
"dark": "#a8a095"
}
],
"background": {
"light": "#f7f3ea",
"dark": "#1c1a17"
}
},
"fonts": {
"family": "\"Source Serif 4\", Georgia, serif"
},
"spacing": {
"padding": 28
}
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"colors": {
"categorical": [
{
"light": "#8c2d19",
"dark": "#e07a5f"
},
{
"light": "#1f5c99",
"dark": "#7fb0e0"
},
{
"light": "#7a7267",
"dark": "#a8a095"
}
],
"background": {
"light": "#f7f3ea",
"dark": "#1c1a17"
}
},
"fonts": {
"family": "\"Source Serif 4\", Georgia, serif"
},
"spacing": {
"padding": 28
}
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal's share of US electricity fell by almost half from 2016 to 2023 as gas rose to 44%",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"colors": {
"categorical": [
{
"light": "#8c2d19",
"dark": "#e07a5f"
},
{
"light": "#1f5c99",
"dark": "#7fb0e0"
},
{
"light": "#7a7267",
"dark": "#a8a095"
}
],
"background": {
"light": "#f7f3ea",
"dark": "#1c1a17"
}
},
"fonts": {
"family": "\"Source Serif 4\", Georgia, serif"
},
"spacing": {
"padding": 28
}
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
The theme field takes a ThemeConfig. This one sets a serif family, a paper background, three series colors and wider padding. Each color is a { light, dark } pair, so the chart has its own dark version; switch the site theme in the header to see it. Fields you leave out keep their default values, because the library deep-merges a theme onto its defaults.
colors.categorical replaces the palette as a whole. Series take its colors in order, so coal gets the first color here.
One theme for every chart
To style every chart on a site, pass the theme once where the charts mount instead of writing it into each spec:
import { createChart } from "@opendata-ai/openchart-vanilla";
import { houseTheme } from "./house-theme";
createChart(container, spec, { theme: houseTheme });In React, Vue and Svelte, wrap the page in <VizThemeProvider theme={houseTheme}>. Layers merge in a fixed order: the spec’s own theme, then the provider, then the component prop or mount option, with the later layer winning for any field both set. Arrays and { light, dark } pairs replace whole; objects merge field by field.
Series strategy
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2017",
"source": "Coal",
"share": 30.7
},
{
"year": "2017",
"source": "Gas",
"share": 33
},
{
"year": "2017",
"source": "Nuclear",
"share": 20.5
},
// … 18 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal fell from 31% to 16% of US electricity between 2016 and 2023",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"seriesStrategy": "accent-neutral"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal fell from 31% to 16% of US electricity between 2016 and 2023",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"seriesStrategy": "accent-neutral"
}
};
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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal fell from 31% to 16% of US electricity between 2016 and 2023",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"seriesStrategy": "accent-neutral"
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal fell from 31% to 16% of US electricity between 2016 and 2023",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"seriesStrategy": "accent-neutral"
}
};
</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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 8
}
},
"y": {
"field": "share",
"type": "quantitative",
"scale": {
"domain": [
0,
50
]
},
"axis": {
"title": "Share of generation (%)",
"format": ".0f",
"grid": true
}
},
"color": {
"field": "source",
"type": "nominal",
"title": "Source"
}
},
"endpointLabels": {
"format": ".0f"
},
"chrome": {
"title": "Coal fell from 31% to 16% of US electricity between 2016 and 2023",
"subtitle": "Share of generation from the six main sources, coal, gas and nuclear shown, %, 2016–2023"
},
"theme": {
"seriesStrategy": "accent-neutral"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
seriesStrategy: "accent-neutral" gives the first series the accent color and the others greys, for up to four series. Five or more get the full palette again. It suits a chart about one series, here coal, without hand-picking colors for the rest. The default, "palette", always uses the full palette.
CSS tokens
A page stylesheet can restyle charts without a theme. Every chart’s root element reads CSS custom properties, and the library defines their defaults in a cascade layer, so ordinary page CSS overrides them:
.oc-root {
/* Series not under the pointer fade to 15% instead of 30%. */
--oc-hover-dim: 0.15;
/* Square tooltips. */
--oc-border-radius: 0;
}A token only applies where the chart’s theme leaves that value unset. If a spec or mount option sets borderRadius, the theme wins, because the library writes authored values inline on the chart. Theming tokens lists every token and its dark-mode value.
Fields that matter
| Field | What it does |
|---|---|
theme.colors.categorical |
The series palette, in order. Takes colors or { light, dark } pairs. |
theme.colors.background, text, axis, gridline |
Surface and ink colors. The default background is transparent. |
theme.fonts.family, mono |
The text family and the family for tabular numbers. |
theme.fonts.sizes |
Pixel sizes for title, subtitle, body, small and axisTick. |
theme.spacing |
padding around the chart, chromeGap between title and subtitle, chromeToChart above the plot. |
theme.chrome |
Per-element color, fontSize, fontWeight and lineHeight for title, subtitle, source and the rest. |
theme.rule |
A short colored bar above the title. null removes one a preset draws. |
theme.seriesStrategy |
"palette" or "accent-neutral". |
Related
- Presets are four finished themes you can start from.
- Dark mode covers light and dark color pairs.
- Line shows the chart type on this page in more detail.
- Theme reference lists every
ThemeConfigfield and default.