Annotations
Text callouts with connectors, reference lines and shaded ranges, placed in data coordinates to point at the story.
An annotation is a note placed in data coordinates, so it stays on its point at every width. A text annotation with a connector points at one mark.
{
"mark": {
"type": "point",
"fill": "#0e7490",
"size": 6,
"trendline": false
},
"data": [
{
"country": "India",
"gdpPerCapita": 9818,
"lifeExpectancy": 68.3
},
{
"country": "China",
"gdpPerCapita": 23846,
"lifeExpectancy": 76.1
},
{
"country": "United States",
"gdpPerCapita": 75489,
"lifeExpectancy": 79.2
},
{
"country": "Indonesia",
"gdpPerCapita": 14470,
"lifeExpectancy": 69
},
{
"country": "Brazil",
"gdpPerCapita": 19652,
"lifeExpectancy": 75.3
},
{
"country": "Mexico",
"gdpPerCapita": 22040,
"lifeExpectancy": 76.9
},
{
"country": "Japan",
"gdpPerCapita": 46107,
"lifeExpectancy": 83.6
},
{
"country": "Germany",
"gdpPerCapita": 63676,
"lifeExpectancy": 80.8
},
{
"country": "United Kingdom",
"gdpPerCapita": 53139,
"lifeExpectancy": 81.4
},
{
"country": "France",
"gdpPerCapita": 54799,
"lifeExpectancy": 82.4
},
{
"country": "South Korea",
"gdpPerCapita": 55071,
"lifeExpectancy": 81.9
},
{
"country": "Canada",
"gdpPerCapita": 58321,
"lifeExpectancy": 82.2
}
],
"encoding": {
"x": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person (PPP, constant international $)",
"format": "$,.0f"
}
},
"y": {
"field": "lifeExpectancy",
"type": "quantitative",
"axis": {
"title": "Life expectancy at birth (years)"
}
},
"tooltip": {
"field": "country",
"type": "nominal",
"title": "Country"
}
},
"annotations": [
{
"type": "text",
"x": 75489,
"y": 79.2,
"text": "**United States**\n$75,489 per person, 79.2 years",
"anchor": "bottom",
"offset": {
"dx": -40,
"dy": 40
},
"connector": "curve"
}
],
"chrome": {
"title": "The US has the highest income of these 12 countries, but six of them live longer",
"subtitle": "GDP per person against life expectancy at birth in 12 large economies"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "point",
"fill": "#0e7490",
"size": 6,
"trendline": false
},
"encoding": {
"x": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person (PPP, constant international $)",
"format": "$,.0f"
}
},
"y": {
"field": "lifeExpectancy",
"type": "quantitative",
"axis": {
"title": "Life expectancy at birth (years)"
}
},
"tooltip": {
"field": "country",
"type": "nominal",
"title": "Country"
}
},
"annotations": [
{
"type": "text",
"x": 75489,
"y": 79.2,
"text": "**United States**\n$75,489 per person, 79.2 years",
"anchor": "bottom",
"offset": {
"dx": -40,
"dy": 40
},
"connector": "curve"
}
],
"chrome": {
"title": "The US has the highest income of these 12 countries, but six of them live longer",
"subtitle": "GDP per person against life expectancy at birth in 12 large economies"
}
};
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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "point",
"fill": "#0e7490",
"size": 6,
"trendline": false
},
"encoding": {
"x": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person (PPP, constant international $)",
"format": "$,.0f"
}
},
"y": {
"field": "lifeExpectancy",
"type": "quantitative",
"axis": {
"title": "Life expectancy at birth (years)"
}
},
"tooltip": {
"field": "country",
"type": "nominal",
"title": "Country"
}
},
"annotations": [
{
"type": "text",
"x": 75489,
"y": 79.2,
"text": "**United States**\n$75,489 per person, 79.2 years",
"anchor": "bottom",
"offset": {
"dx": -40,
"dy": 40
},
"connector": "curve"
}
],
"chrome": {
"title": "The US has the highest income of these 12 countries, but six of them live longer",
"subtitle": "GDP per person against life expectancy at birth in 12 large economies"
}
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "point",
"fill": "#0e7490",
"size": 6,
"trendline": false
},
"encoding": {
"x": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person (PPP, constant international $)",
"format": "$,.0f"
}
},
"y": {
"field": "lifeExpectancy",
"type": "quantitative",
"axis": {
"title": "Life expectancy at birth (years)"
}
},
"tooltip": {
"field": "country",
"type": "nominal",
"title": "Country"
}
},
"annotations": [
{
"type": "text",
"x": 75489,
"y": 79.2,
"text": "**United States**\n$75,489 per person, 79.2 years",
"anchor": "bottom",
"offset": {
"dx": -40,
"dy": 40
},
"connector": "curve"
}
],
"chrome": {
"title": "The US has the highest income of these 12 countries, but six of them live longer",
"subtitle": "GDP per person against life expectancy at birth in 12 large economies"
}
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "point",
"fill": "#0e7490",
"size": 6,
"trendline": false
},
"encoding": {
"x": {
"field": "gdpPerCapita",
"type": "quantitative",
"axis": {
"title": "GDP per person (PPP, constant international $)",
"format": "$,.0f"
}
},
"y": {
"field": "lifeExpectancy",
"type": "quantitative",
"axis": {
"title": "Life expectancy at birth (years)"
}
},
"tooltip": {
"field": "country",
"type": "nominal",
"title": "Country"
}
},
"annotations": [
{
"type": "text",
"x": 75489,
"y": 79.2,
"text": "**United States**\n$75,489 per person, 79.2 years",
"anchor": "bottom",
"offset": {
"dx": -40,
"dy": 40
},
"connector": "curve"
}
],
"chrome": {
"title": "The US has the highest income of these 12 countries, but six of them live longer",
"subtitle": "GDP per person against life expectancy at birth in 12 large economies"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Text callout
The callout sits at the US point, x: 75489 and y: 79.2. offset moves the label 40 pixels left and down from it, anchor: "bottom" puts the label below the point (anchor sets which side of the point the label sits on), and connector: "curve" draws a curved leader with a ring on the point. \n breaks the text into two lines and **United States** sets the name in bold.
Reference line
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"month": "Jan",
"high": 9.3
},
{
"month": "Feb",
"high": 9.2
},
{
"month": "Mar",
"high": 10.8
},
{
"month": "Apr",
"high": 19
},
{
"month": "May",
"high": 22.1
},
{
"month": "Jun",
"high": 25.4
},
{
"month": "Jul",
"high": 30.2
},
{
"month": "Aug",
"high": 27.6
},
{
"month": "Sep",
"high": 24.3
},
{
"month": "Oct",
"high": 19.3
},
{
"month": "Nov",
"high": 11.8
},
{
"month": "Dec",
"high": 9.8
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C"
}
}
},
"labels": false,
"annotations": [
{
"type": "refline",
"y": 18.233333333333338,
"label": "Average of the monthly highs, 18.2°C",
"style": "dashed",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Seven months of 2023 averaged highs above 18.2°C, the average of Central Park's 12 monthly highs",
"subtitle": "Average daily high by month, Central Park, New York, 2023 (°C)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C"
}
}
},
"labels": false,
"annotations": [
{
"type": "refline",
"y": 18.233333333333338,
"label": "Average of the monthly highs, 18.2°C",
"style": "dashed",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Seven months of 2023 averaged highs above 18.2°C, the average of Central Park's 12 monthly highs",
"subtitle": "Average daily high by month, Central Park, New York, 2023 (°C)"
}
};
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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C"
}
}
},
"labels": false,
"annotations": [
{
"type": "refline",
"y": 18.233333333333338,
"label": "Average of the monthly highs, 18.2°C",
"style": "dashed",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Seven months of 2023 averaged highs above 18.2°C, the average of Central Park's 12 monthly highs",
"subtitle": "Average daily high by month, Central Park, New York, 2023 (°C)"
}
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C"
}
}
},
"labels": false,
"annotations": [
{
"type": "refline",
"y": 18.233333333333338,
"label": "Average of the monthly highs, 18.2°C",
"style": "dashed",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Seven months of 2023 averaged highs above 18.2°C, the average of Central Park's 12 monthly highs",
"subtitle": "Average daily high by month, Central Park, New York, 2023 (°C)"
}
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "high",
"type": "quantitative",
"axis": {
"title": "Average daily high",
"format": ".0f",
"labelSuffix": "°C"
}
}
},
"labels": false,
"annotations": [
{
"type": "refline",
"y": 18.233333333333338,
"label": "Average of the monthly highs, 18.2°C",
"style": "dashed",
"strokeWidth": 1.5
}
],
"chrome": {
"title": "Seven months of 2023 averaged highs above 18.2°C, the average of Central Park's 12 monthly highs",
"subtitle": "Average daily high by month, Central Park, New York, 2023 (°C)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A refline draws a line across the plot at one value. Set y for a horizontal line or x for a vertical one. This one marks the average of the 12 monthly highs, 18.2°C. The demo module computes it from the rows, so the line and its label always match the data. style: "dashed" keeps it visually separate from the bars.
Shaded range
{
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2
},
"data": [
{
"date": "2019-01-01",
"rate": 1.6
},
{
"date": "2019-04-01",
"rate": 2
},
{
"date": "2019-07-01",
"rate": 1.8
},
{
"date": "2019-10-01",
"rate": 1.8
},
{
"date": "2020-01-01",
"rate": 2.5
},
{
"date": "2020-04-01",
"rate": 0.3
},
// … 18 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"scale": {
"domain": [
0,
10
]
},
"axis": {
"title": "Change from a year earlier",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"annotations": [
{
"type": "range",
"x1": "2021-07-01",
"x2": "2023-01-01",
"label": "Above 5%",
"opacity": 0.12
}
],
"chrome": {
"title": "US inflation stayed above 5% for seven straight quarters, from mid-2021 to early 2023",
"subtitle": "Consumer price index, % change from a year earlier, quarterly, 2019–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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"scale": {
"domain": [
0,
10
]
},
"axis": {
"title": "Change from a year earlier",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"annotations": [
{
"type": "range",
"x1": "2021-07-01",
"x2": "2023-01-01",
"label": "Above 5%",
"opacity": 0.12
}
],
"chrome": {
"title": "US inflation stayed above 5% for seven straight quarters, from mid-2021 to early 2023",
"subtitle": "Consumer price index, % change from a year earlier, quarterly, 2019–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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"scale": {
"domain": [
0,
10
]
},
"axis": {
"title": "Change from a year earlier",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"annotations": [
{
"type": "range",
"x1": "2021-07-01",
"x2": "2023-01-01",
"label": "Above 5%",
"opacity": 0.12
}
],
"chrome": {
"title": "US inflation stayed above 5% for seven straight quarters, from mid-2021 to early 2023",
"subtitle": "Consumer price index, % change from a year earlier, quarterly, 2019–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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"scale": {
"domain": [
0,
10
]
},
"axis": {
"title": "Change from a year earlier",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"annotations": [
{
"type": "range",
"x1": "2021-07-01",
"x2": "2023-01-01",
"label": "Above 5%",
"opacity": 0.12
}
],
"chrome": {
"title": "US inflation stayed above 5% for seven straight quarters, from mid-2021 to early 2023",
"subtitle": "Consumer price index, % change from a year earlier, quarterly, 2019–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 = [/* 24 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"scale": {
"domain": [
0,
10
]
},
"axis": {
"title": "Change from a year earlier",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"annotations": [
{
"type": "range",
"x1": "2021-07-01",
"x2": "2023-01-01",
"label": "Above 5%",
"opacity": 0.12
}
],
"chrome": {
"title": "US inflation stayed above 5% for seven straight quarters, from mid-2021 to early 2023",
"subtitle": "Consumer price index, % change from a year earlier, quarterly, 2019–2024"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A range shades a stretch of the axis between x1 and x2, or y1 and y2, with an optional label. Here it covers the seven quarters when inflation ran above 5%, from the third quarter of 2021 to the first quarter of 2023. opacity keeps the fill light enough that the line stays easy to read through it, and a y domain of 0 to 10 leaves room for the label above the peak.
Highlighted series
{
"mark": {
"type": "line",
"strokeWidth": 2
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2016",
"source": "Hydro",
"share": 6.6
},
{
"year": "2016",
"source": "Wind",
"share": 5.7
},
{
"year": "2016",
"source": "Solar",
"share": 1.4
},
// … 42 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"highlight": "Coal"
}
},
"endpointLabels": {
"content": "label"
},
"seriesStyles": {
"Coal": {
"strokeWidth": 3
}
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 19.6,
"text": "Coal fell below **20%** in 2020",
"anchor": "bottom",
"offset": {
"dx": 0,
"dy": 28
},
"connector": true
}
],
"chrome": {
"title": "Coal's share of generation from the six main sources nearly halved between 2016 and 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%). Coal went from 31.2% in 2016 to 16.2% in 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 = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"highlight": "Coal"
}
},
"endpointLabels": {
"content": "label"
},
"seriesStyles": {
"Coal": {
"strokeWidth": 3
}
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 19.6,
"text": "Coal fell below **20%** in 2020",
"anchor": "bottom",
"offset": {
"dx": 0,
"dy": 28
},
"connector": true
}
],
"chrome": {
"title": "Coal's share of generation from the six main sources nearly halved between 2016 and 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%). Coal went from 31.2% in 2016 to 16.2% in 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 = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"highlight": "Coal"
}
},
"endpointLabels": {
"content": "label"
},
"seriesStyles": {
"Coal": {
"strokeWidth": 3
}
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 19.6,
"text": "Coal fell below **20%** in 2020",
"anchor": "bottom",
"offset": {
"dx": 0,
"dy": 28
},
"connector": true
}
],
"chrome": {
"title": "Coal's share of generation from the six main sources nearly halved between 2016 and 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%). Coal went from 31.2% in 2016 to 16.2% in 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 = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"highlight": "Coal"
}
},
"endpointLabels": {
"content": "label"
},
"seriesStyles": {
"Coal": {
"strokeWidth": 3
}
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 19.6,
"text": "Coal fell below **20%** in 2020",
"anchor": "bottom",
"offset": {
"dx": 0,
"dy": 28
},
"connector": true
}
],
"chrome": {
"title": "Coal's share of generation from the six main sources nearly halved between 2016 and 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%). Coal went from 31.2% in 2016 to 16.2% in 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 = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "share",
"type": "quantitative",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"highlight": "Coal"
}
},
"endpointLabels": {
"content": "label"
},
"seriesStyles": {
"Coal": {
"strokeWidth": 3
}
},
"annotations": [
{
"type": "text",
"x": "2020",
"y": 19.6,
"text": "Coal fell below **20%** in 2020",
"anchor": "bottom",
"offset": {
"dx": 0,
"dy": 28
},
"connector": true
}
],
"chrome": {
"title": "Coal's share of generation from the six main sources nearly halved between 2016 and 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%). Coal went from 31.2% in 2016 to 16.2% in 2023."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
highlight: "Coal" on the color channel keeps coal in its color and greys out the other five series. seriesStyles makes the coal line thicker, and one text annotation names the year it first fell below 20%. Keep a chart to three annotations or fewer; one that needs more usually holds two findings and reads better as two charts.
Fields that matter
| Field | What it does |
|---|---|
type |
"text", "refline" or "range". |
x, y |
Data coordinates of a text note or reference line. |
x1, x2, y1, y2 |
The ends of a range. |
text, label |
The note text. **bold** spans and \n line breaks work in text. |
connector |
true, "curve", "drop-line" or { type, arrow }. |
anchor, offset |
Which side of the point the label sits on, and the label’s pixel offset. |
style |
"solid", "dashed" or "dotted" for a reference line. |
encoding.color.highlight |
One or more series to keep in color while the rest turn grey. |
Related
- Line combines callouts, reference lines and ranges on time series.
- Scatter labels outliers with callouts.
- Annotations reference lists every annotation field.