Responsive charts on mobile
On a phone, OpenChart thins the axis ticks on its own. The title length and which callout stays are still your call.
OpenChart3 min read
Made with OpenChart. How to build this line chart
Major league batters have piled up more strikeouts than hits in every season since 2018. That chart is easy to read in a 720px article column. On a 360px phone, the same drawing scaled down to fit has tick labels too small to read, so OpenChart lays the chart out again for the width its container has.
Made with OpenChart. How to build this line chart
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"data": [
{
"year": "1960",
"measure": "Strikeouts",
"count": 12815
},
{
"year": "1960",
"measure": "Hits",
"count": 21434
},
{
"year": "1961",
"measure": "Strikeouts",
"count": 14947
},
{
"year": "1961",
"measure": "Hits",
"count": 25066
},
{
"year": "1962",
"measure": "Strikeouts",
"count": 17567
},
{
"year": "1962",
"measure": "Hits",
"count": 28521
},
// … 126 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
</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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
</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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Here is the same spec in a 360px container.
Made with OpenChart. How to build this line chart
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"data": [
{
"year": "1960",
"measure": "Strikeouts",
"count": 12815
},
{
"year": "1960",
"measure": "Hits",
"count": 21434
},
{
"year": "1961",
"measure": "Strikeouts",
"count": 14947
},
{
"year": "1961",
"measure": "Hits",
"count": 25066
},
{
"year": "1962",
"measure": "Strikeouts",
"count": 17567
},
{
"year": "1962",
"measure": "Hits",
"count": 28521
},
// … 126 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
</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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
</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 = [/* 132 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "count",
"type": "quantitative",
"axis": {
"title": "Per season",
"format": ",.0f",
"grid": true
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"Strikeouts",
"Hits"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": true,
"annotations": [
{
"type": "text",
"x": "2020",
"y": 14439,
"text": "2020: 60-game season",
"anchor": "right",
"offset": {
"dx": -14,
"dy": 18
},
"connector": true
},
{
"type": "text",
"x": "2018",
"y": 41207,
"text": "2018: strikeouts pass hits",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -28
},
"connector": true,
"background": true,
"priority": 1
}
],
"chromeLayout": "subtract",
"chrome": {
"title": "Strikeouts have outnumbered hits every season since 2018",
"subtitle": "Hits and strikeouts by all MLB batters, per season, 1960-2025"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Breakpoints and the compact layout
The library sorts a chart into one of three layouts by container width: full above 700px, medium from 400 to 700px, and compact below 400px. Compact drops value labels, uses the fewest axis ticks and keeps at most one callout inline. Any other callout becomes a numbered marker, with its text in a note under the plot.
In the two batting charts above, the year ticks go from four (1960, 1980, 2000, 2020) to three (1960, 2000, 2020), and the 2020 callout turns into marker 1 with its note under the plot. Notes are numbered by their place in the spec’s annotations list, which is why the 2020 callout is listed first.
Height counts too. A chart 350px tall or shorter drops the subtitle and keeps its title, and one under 100px tall drops the title as well.
The breakpoints follow the width of the container, not the screen. A chart in a 300px sidebar on a desktop monitor gets the compact layout, so test one width inside each band (360, 560 and your full column) instead of one phone.
Setting up a chart for phones
Write a second, shorter title and subtitle for compact width and put it in overrides. The title of the last chart here takes two lines at 720px and four at 360px, which pushes the plot down, and the layout won’t shorten a headline for you.
Pick the callout that carries the point and give it priority: 1. Compact keeps the callout with the lowest priority number inline and numbers the rest, falling back to spec order when no callout has a priority. Callouts you write for phones in overrides are not held to the one-callout limit, so keep that list short.
Check that every line still has its name at its end. Three series fit at 360px. With more, responsive: { labelSummary: true } moves names and last values into a keyed list under the plot when they cannot fit, and a top legend, as the direct labels post suggests for phones, also gives the lines the full width.
Set the height for the narrow shape. Height comes from the container, not the spec, and 360px wide by 440px tall is a different shape from 720 by 440. A time series usually reads better wider than tall, so check how steep the lines look before you keep the same height.
The chart below sets a short compact title, a number-only callout and four fixed year ticks in overrides. Narrow your browser window below 400px, or open this page on a phone, to see the swap.
Made with OpenChart. How to build this line chart
{
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"data": [
{
"year": "1995",
"partner": "China",
"share": 5.7
},
{
"year": "1995",
"partner": "Mexico",
"share": 8.5
},
{
"year": "1995",
"partner": "Canada",
"share": 20.1
},
{
"year": "1996",
"partner": "China",
"share": 5.9
},
{
"year": "1996",
"partner": "Mexico",
"share": 9.6
},
{
"year": "1996",
"partner": "Canada",
"share": 20.6
},
// … 84 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of US goods imports",
"format": ".0f",
"labelSuffix": "%",
"grid": true
}
},
"color": {
"field": "partner",
"type": "nominal",
"scale": {
"domain": [
"China",
"Mexico",
"Canada"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#b54d00",
"dark": "#f07a2b"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": {
"format": ".1f"
},
"responsive": {
"preserveEnd": true
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "China peaked at 20.4% in 2020",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -30
},
"connector": true
}
],
"overrides": {
"compact": {
"encoding": {
"x": {
"axis": {
"values": [
"1995",
"2005",
"2015",
"2024"
]
}
}
},
"chrome": {
"title": "China's share of US imports peaked in 2020",
"subtitle": "Share of US goods imports, 1995-2024"
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "20.4%",
"anchor": "top",
"offset": {
"dx": -36,
"dy": -30
},
"connector": true
}
]
}
},
"chromeLayout": "subtract",
"chrome": {
"title": "China's share of US goods imports peaked at 20.4% in 2020; Mexico passed it in 2023",
"subtitle": "Share of the value of US goods imports from China, Mexico and Canada, 1995-2024"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 90 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of US goods imports",
"format": ".0f",
"labelSuffix": "%",
"grid": true
}
},
"color": {
"field": "partner",
"type": "nominal",
"scale": {
"domain": [
"China",
"Mexico",
"Canada"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#b54d00",
"dark": "#f07a2b"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": {
"format": ".1f"
},
"responsive": {
"preserveEnd": true
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "China peaked at 20.4% in 2020",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -30
},
"connector": true
}
],
"overrides": {
"compact": {
"encoding": {
"x": {
"axis": {
"values": [
"1995",
"2005",
"2015",
"2024"
]
}
}
},
"chrome": {
"title": "China's share of US imports peaked in 2020",
"subtitle": "Share of US goods imports, 1995-2024"
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "20.4%",
"anchor": "top",
"offset": {
"dx": -36,
"dy": -30
},
"connector": true
}
]
}
},
"chromeLayout": "subtract",
"chrome": {
"title": "China's share of US goods imports peaked at 20.4% in 2020; Mexico passed it in 2023",
"subtitle": "Share of the value of US goods imports from China, Mexico and Canada, 1995-2024"
}
};
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 = [/* 90 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of US goods imports",
"format": ".0f",
"labelSuffix": "%",
"grid": true
}
},
"color": {
"field": "partner",
"type": "nominal",
"scale": {
"domain": [
"China",
"Mexico",
"Canada"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#b54d00",
"dark": "#f07a2b"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": {
"format": ".1f"
},
"responsive": {
"preserveEnd": true
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "China peaked at 20.4% in 2020",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -30
},
"connector": true
}
],
"overrides": {
"compact": {
"encoding": {
"x": {
"axis": {
"values": [
"1995",
"2005",
"2015",
"2024"
]
}
}
},
"chrome": {
"title": "China's share of US imports peaked in 2020",
"subtitle": "Share of US goods imports, 1995-2024"
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "20.4%",
"anchor": "top",
"offset": {
"dx": -36,
"dy": -30
},
"connector": true
}
]
}
},
"chromeLayout": "subtract",
"chrome": {
"title": "China's share of US goods imports peaked at 20.4% in 2020; Mexico passed it in 2023",
"subtitle": "Share of the value of US goods imports from China, Mexico and Canada, 1995-2024"
}
};
</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 = [/* 90 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of US goods imports",
"format": ".0f",
"labelSuffix": "%",
"grid": true
}
},
"color": {
"field": "partner",
"type": "nominal",
"scale": {
"domain": [
"China",
"Mexico",
"Canada"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#b54d00",
"dark": "#f07a2b"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": {
"format": ".1f"
},
"responsive": {
"preserveEnd": true
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "China peaked at 20.4% in 2020",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -30
},
"connector": true
}
],
"overrides": {
"compact": {
"encoding": {
"x": {
"axis": {
"values": [
"1995",
"2005",
"2015",
"2024"
]
}
}
},
"chrome": {
"title": "China's share of US imports peaked in 2020",
"subtitle": "Share of US goods imports, 1995-2024"
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "20.4%",
"anchor": "top",
"offset": {
"dx": -36,
"dy": -30
},
"connector": true
}
]
}
},
"chromeLayout": "subtract",
"chrome": {
"title": "China's share of US goods imports peaked at 20.4% in 2020; Mexico passed it in 2023",
"subtitle": "Share of the value of US goods imports from China, Mexico and Canada, 1995-2024"
}
};
</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 = [/* 90 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2.5,
"interpolate": "linear"
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of US goods imports",
"format": ".0f",
"labelSuffix": "%",
"grid": true
}
},
"color": {
"field": "partner",
"type": "nominal",
"scale": {
"domain": [
"China",
"Mexico",
"Canada"
]
}
}
},
"theme": {
"colors": {
"categorical": [
{
"light": "#0e7490",
"dark": "#22a3c4"
},
{
"light": "#b54d00",
"dark": "#f07a2b"
},
{
"light": "#66707a",
"dark": "#9aa0a6"
}
]
}
},
"legend": {
"show": false
},
"endpointLabels": {
"format": ".1f"
},
"responsive": {
"preserveEnd": true
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "China peaked at 20.4% in 2020",
"anchor": "top",
"offset": {
"dx": -40,
"dy": -30
},
"connector": true
}
],
"overrides": {
"compact": {
"encoding": {
"x": {
"axis": {
"values": [
"1995",
"2005",
"2015",
"2024"
]
}
}
},
"chrome": {
"title": "China's share of US imports peaked in 2020",
"subtitle": "Share of US goods imports, 1995-2024"
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 20.4,
"text": "20.4%",
"anchor": "top",
"offset": {
"dx": -36,
"dy": -30
},
"connector": true
}
]
}
},
"chromeLayout": "subtract",
"chrome": {
"title": "China's share of US goods imports peaked at 20.4% in 2020; Mexico passed it in 2023",
"subtitle": "Share of the value of US goods imports from China, Mexico and Canada, 1995-2024"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
The spec
overrides.compact is a patch laid over the spec when the container is under 400px. Here it changes the title and subtitle in chrome, which merge field by field into the base, swaps annotations for a single short callout, and sets the x axis values so the phone shows 1995, 2005, 2015 and 2024. It can also change labels and the legend. medium and full work the same way.
responsive: { preserveEnd: true } keeps the last tick on the time axis when ticks are thinned, so the final year stays on the axis at medium width. endpointLabels names each line at its right end, which is why the chart needs no legend.
theme.colors.categorical gives each series a light and a dark color, both readable as end-label text on their page. chromeLayout: "subtract" holds the figure at its fixed height, so a long title or a note for a demoted callout takes room from the plot instead of making the figure taller.
Every breakpoint and responsive option is listed in the responsive docs.