Labels and legends
Value labels, their density and format, and when a legend earns its place over labelling marks directly.
labels prints each mark’s value on the chart. Here they print one decimal place and stand in for the y axis, which the spec turns off.
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"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",
"axis": false
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "US inflation was above 3% for 13 straight quarters, from mid-2021 to mid-2024",
"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": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "US inflation was above 3% for 13 straight quarters, from mid-2021 to mid-2024",
"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": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "US inflation was above 3% for 13 straight quarters, from mid-2021 to mid-2024",
"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": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "US inflation was above 3% for 13 straight quarters, from mid-2021 to mid-2024",
"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": "bar",
"fill": "#0e7490"
},
"encoding": {
"x": {
"field": "date",
"type": "temporal",
"axis": {
"format": "%Y",
"tickCount": 6
}
},
"y": {
"field": "rate",
"type": "quantitative",
"axis": false
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "US inflation was above 3% for 13 straight quarters, from mid-2021 to mid-2024",
"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);
Value labels
format: ".1f" matches the one decimal place in the source data, and axis: false drops the y axis because the labels already carry the numbers. density decides how many labels show. The default, "auto", hides any label that would collide with a neighbour; all 24 fit here, so none is dropped. "all" prints every label even when they collide, "endpoints" only the first and last, and "none" none. prefix and suffix add text around each value.
Legend position
{
"mark": "bar",
"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": "ordinal"
},
"y": {
"field": "share",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"labels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made up 43.5% of generation from the six main sources in 2023, more than coal and nuclear combined",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
}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": "bar",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "share",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"labels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made up 43.5% of generation from the six main sources in 2023, more than coal and nuclear combined",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
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": "bar",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "share",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"labels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made up 43.5% of generation from the six main sources in 2023, more than coal and nuclear combined",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
</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": "bar",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "share",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"labels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made up 43.5% of generation from the six main sources in 2023, more than coal and nuclear combined",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
</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": "bar",
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "share",
"type": "quantitative",
"stack": "zero",
"axis": {
"title": "Share of generation",
"format": ".0f",
"labelSuffix": "%"
}
},
"color": {
"field": "source",
"type": "nominal",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"labels": false,
"legend": {
"position": "top"
},
"chrome": {
"title": "Gas made up 43.5% of generation from the six main sources in 2023, more than coal and nuclear combined",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A nominal field on color gets a legend. Without a setting, the engine picks the position from the chart’s width; legend: { position: "top" } fixes it above the plot. The other positions are "right", "bottom", "top-left", "bottom-right" and "inline". scale.domain on the color channel sets the order of the legend and which color each source gets. The stack follows the order of the rows, so this spec lists the sources in the domain in the same order the data does.
Labels in place of a legend
{
"mark": "area",
"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",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"legend": {
"show": false
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Gas's share of generation from the six main sources grew from 34.8% in 2016 to 43.5% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
}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": "area",
"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",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"legend": {
"show": false
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Gas's share of generation from the six main sources grew from 34.8% in 2016 to 43.5% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
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": "area",
"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",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"legend": {
"show": false
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Gas's share of generation from the six main sources grew from 34.8% in 2016 to 43.5% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
</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": "area",
"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",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"legend": {
"show": false
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Gas's share of generation from the six main sources grew from 34.8% in 2016 to 43.5% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
</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": "area",
"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",
"scale": {
"domain": [
"Coal",
"Gas",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
}
}
},
"legend": {
"show": false
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Gas's share of generation from the six main sources grew from 34.8% in 2016 to 43.5% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A legend makes the reader match colors back to the plot. Here each band is named at its right end instead. Stacked areas show these names only when endpointLabels is set, and content: "label" prints the name without the value. legend: { show: false } removes the legend the names replace.
Fields that matter
| Field | What it does |
|---|---|
labels |
true, false or a config object for value labels. |
labels.density |
"auto", "all", "endpoints" or "none". |
labels.format |
d3-format string for the value. |
labels.prefix, labels.suffix |
Text before and after each value. |
labels.placement |
"inside", "outside" or "adaptive" for bar labels. |
legend.position |
Where the legend goes. Omit it to let the width decide. |
legend.show |
false hides the legend. |
endpointLabels |
Names each line or band at its right end. Stacked areas need it set explicitly. |
Related
- Stacked area shows more stacked bands over time.
- Bar shows value label placement on bars.
- Endpoint labels names lines at their ends.
- Chrome, labels and legends reference lists every label and legend field.