Layers
Draw several marks on one plot with a layer array, such as bars with a reference rule, labelled points, or two series on independent axes.
A layered chart draws more than one mark in the same plot. Replace the single mark, data and encoding with a layer array, where each entry is a chart spec of its own. Put data or encoding at the top level to share them across layers, and keep the chrome there too. Use it for a reference line over bars, labels over points, or a second series that needs its own axis.
When the series should not share a plot at all, split them into panels with facets. For a reference line or note that needs no data of its own, the annotations on a single chart, shown on the column page, are simpler.
Bars with a rule
The first layer is a bar chart and the second is a rule mark drawn from a one-row dataset, here the yearly average. Layers share their x and y scales by default, so the one-row rule lands at its value on the bars’ axis. A scale.domain on either layer pins the shared axis. The rule takes its color from the color scale, so a single-entry range sets it.
{
"chrome": {
"title": "Employers added an average of 216,000 jobs a month in 2024",
"subtitle": "Monthly US nonfarm payroll additions, 2024, with the yearly average drawn as a second layer"
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#0e7490",
"opacity": 0.85
},
"data": [
{
"month": "Jan",
"jobs": 353
},
{
"month": "Feb",
"jobs": 275
},
{
"month": "Mar",
"jobs": 303
},
{
"month": "Apr",
"jobs": 175
},
{
"month": "May",
"jobs": 272
},
{
"month": "Jun",
"jobs": 206
},
{
"month": "Jul",
"jobs": 114
},
{
"month": "Aug",
"jobs": 142
},
{
"month": "Sep",
"jobs": 254
},
{
"month": "Oct",
"jobs": 12
},
{
"month": "Nov",
"jobs": 227
},
{
"month": "Dec",
"jobs": 256
}
],
"encoding": {
"x": {
"field": "month",
"type": "nominal"
},
"y": {
"field": "jobs",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
},
"axis": {
"title": "Jobs added (thousands)"
}
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "rule"
},
"data": [
{
"avg": 216,
"label": "avg: 216K"
}
],
"encoding": {
"y": {
"field": "avg",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
}
},
"color": {
"field": "label",
"type": "nominal",
"scale": {
"range": [
"#334155"
]
}
}
}
}
]
}import { Chart } from "@opendata-ai/openchart-react";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Employers added an average of 216,000 jobs a month in 2024",
"subtitle": "Monthly US nonfarm payroll additions, 2024, with the yearly average drawn as a second layer"
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#0e7490",
"opacity": 0.85
},
"data": [
{
"month": "Jan",
"jobs": 353
},
{
"month": "Feb",
"jobs": 275
},
{
"month": "Mar",
"jobs": 303
},
{
"month": "Apr",
"jobs": 175
},
{
"month": "May",
"jobs": 272
},
{
"month": "Jun",
"jobs": 206
},
{
"month": "Jul",
"jobs": 114
},
{
"month": "Aug",
"jobs": 142
},
{
"month": "Sep",
"jobs": 254
},
{
"month": "Oct",
"jobs": 12
},
{
"month": "Nov",
"jobs": 227
},
{
"month": "Dec",
"jobs": 256
}
],
"encoding": {
"x": {
"field": "month",
"type": "nominal"
},
"y": {
"field": "jobs",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
},
"axis": {
"title": "Jobs added (thousands)"
}
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "rule"
},
"data": [
{
"avg": 216,
"label": "avg: 216K"
}
],
"encoding": {
"y": {
"field": "avg",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
}
},
"color": {
"field": "label",
"type": "nominal",
"scale": {
"range": [
"#334155"
]
}
}
}
}
]
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Employers added an average of 216,000 jobs a month in 2024",
"subtitle": "Monthly US nonfarm payroll additions, 2024, with the yearly average drawn as a second layer"
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#0e7490",
"opacity": 0.85
},
"data": [
{
"month": "Jan",
"jobs": 353
},
{
"month": "Feb",
"jobs": 275
},
{
"month": "Mar",
"jobs": 303
},
{
"month": "Apr",
"jobs": 175
},
{
"month": "May",
"jobs": 272
},
{
"month": "Jun",
"jobs": 206
},
{
"month": "Jul",
"jobs": 114
},
{
"month": "Aug",
"jobs": 142
},
{
"month": "Sep",
"jobs": 254
},
{
"month": "Oct",
"jobs": 12
},
{
"month": "Nov",
"jobs": 227
},
{
"month": "Dec",
"jobs": 256
}
],
"encoding": {
"x": {
"field": "month",
"type": "nominal"
},
"y": {
"field": "jobs",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
},
"axis": {
"title": "Jobs added (thousands)"
}
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "rule"
},
"data": [
{
"avg": 216,
"label": "avg: 216K"
}
],
"encoding": {
"y": {
"field": "avg",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
}
},
"color": {
"field": "label",
"type": "nominal",
"scale": {
"range": [
"#334155"
]
}
}
}
}
]
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Employers added an average of 216,000 jobs a month in 2024",
"subtitle": "Monthly US nonfarm payroll additions, 2024, with the yearly average drawn as a second layer"
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#0e7490",
"opacity": 0.85
},
"data": [
{
"month": "Jan",
"jobs": 353
},
{
"month": "Feb",
"jobs": 275
},
{
"month": "Mar",
"jobs": 303
},
{
"month": "Apr",
"jobs": 175
},
{
"month": "May",
"jobs": 272
},
{
"month": "Jun",
"jobs": 206
},
{
"month": "Jul",
"jobs": 114
},
{
"month": "Aug",
"jobs": 142
},
{
"month": "Sep",
"jobs": 254
},
{
"month": "Oct",
"jobs": 12
},
{
"month": "Nov",
"jobs": 227
},
{
"month": "Dec",
"jobs": 256
}
],
"encoding": {
"x": {
"field": "month",
"type": "nominal"
},
"y": {
"field": "jobs",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
},
"axis": {
"title": "Jobs added (thousands)"
}
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "rule"
},
"data": [
{
"avg": 216,
"label": "avg: 216K"
}
],
"encoding": {
"y": {
"field": "avg",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
}
},
"color": {
"field": "label",
"type": "nominal",
"scale": {
"range": [
"#334155"
]
}
}
}
}
]
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Employers added an average of 216,000 jobs a month in 2024",
"subtitle": "Monthly US nonfarm payroll additions, 2024, with the yearly average drawn as a second layer"
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#0e7490",
"opacity": 0.85
},
"data": [
{
"month": "Jan",
"jobs": 353
},
{
"month": "Feb",
"jobs": 275
},
{
"month": "Mar",
"jobs": 303
},
{
"month": "Apr",
"jobs": 175
},
{
"month": "May",
"jobs": 272
},
{
"month": "Jun",
"jobs": 206
},
{
"month": "Jul",
"jobs": 114
},
{
"month": "Aug",
"jobs": 142
},
{
"month": "Sep",
"jobs": 254
},
{
"month": "Oct",
"jobs": 12
},
{
"month": "Nov",
"jobs": 227
},
{
"month": "Dec",
"jobs": 256
}
],
"encoding": {
"x": {
"field": "month",
"type": "nominal"
},
"y": {
"field": "jobs",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
},
"axis": {
"title": "Jobs added (thousands)"
}
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "rule"
},
"data": [
{
"avg": 216,
"label": "avg: 216K"
}
],
"encoding": {
"y": {
"field": "avg",
"type": "quantitative",
"scale": {
"domain": [
0,
400
]
}
},
"color": {
"field": "label",
"type": "nominal",
"scale": {
"range": [
"#334155"
]
}
}
}
}
]
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Direct labels on points
A point layer draws every state and a text layer labels only five of them. The text layer reads its words from a field with the text channel, and dy on the mark offsets each label in pixels, so it stays on the point layer’s scales. Both layers share a pinned domain with headroom, so the top label does not overhang the plot.
{
"chrome": {
"title": "California has the largest economy and population of any state",
"subtitle": "The ten largest US state economies by GDP and population, 2023"
},
"layer": [
{
"mark": {
"type": "point",
"fill": "#0e7490",
"opacity": 0.85,
"trendline": false
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
},
{
"state": "Pennsylvania",
"label": "PA",
"gdp": 1,
"pop": 12.9
},
{
"state": "Ohio",
"label": "OH",
"gdp": 0.9,
"pop": 11.8
},
{
"state": "Georgia",
"label": "GA",
"gdp": 0.8,
"pop": 11
},
{
"state": "New Jersey",
"label": "NJ",
"gdp": 0.8,
"pop": 9.3
},
{
"state": "Washington",
"label": "WA",
"gdp": 0.8,
"pop": 7.8
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
},
"axis": {
"title": "GDP ($ trillions)"
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
},
"axis": {
"title": "Population (millions)"
}
}
}
},
{
"mark": {
"type": "text",
"dy": -14
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
}
},
"text": {
"field": "label",
"type": "nominal"
},
"size": {
"field": "gdp",
"type": "quantitative",
"scale": {
"range": [
11,
22
]
}
}
}
}
]
}import { Chart } from "@opendata-ai/openchart-react";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "California has the largest economy and population of any state",
"subtitle": "The ten largest US state economies by GDP and population, 2023"
},
"layer": [
{
"mark": {
"type": "point",
"fill": "#0e7490",
"opacity": 0.85,
"trendline": false
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
},
{
"state": "Pennsylvania",
"label": "PA",
"gdp": 1,
"pop": 12.9
},
{
"state": "Ohio",
"label": "OH",
"gdp": 0.9,
"pop": 11.8
},
{
"state": "Georgia",
"label": "GA",
"gdp": 0.8,
"pop": 11
},
{
"state": "New Jersey",
"label": "NJ",
"gdp": 0.8,
"pop": 9.3
},
{
"state": "Washington",
"label": "WA",
"gdp": 0.8,
"pop": 7.8
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
},
"axis": {
"title": "GDP ($ trillions)"
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
},
"axis": {
"title": "Population (millions)"
}
}
}
},
{
"mark": {
"type": "text",
"dy": -14
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
}
},
"text": {
"field": "label",
"type": "nominal"
},
"size": {
"field": "gdp",
"type": "quantitative",
"scale": {
"range": [
11,
22
]
}
}
}
}
]
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "California has the largest economy and population of any state",
"subtitle": "The ten largest US state economies by GDP and population, 2023"
},
"layer": [
{
"mark": {
"type": "point",
"fill": "#0e7490",
"opacity": 0.85,
"trendline": false
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
},
{
"state": "Pennsylvania",
"label": "PA",
"gdp": 1,
"pop": 12.9
},
{
"state": "Ohio",
"label": "OH",
"gdp": 0.9,
"pop": 11.8
},
{
"state": "Georgia",
"label": "GA",
"gdp": 0.8,
"pop": 11
},
{
"state": "New Jersey",
"label": "NJ",
"gdp": 0.8,
"pop": 9.3
},
{
"state": "Washington",
"label": "WA",
"gdp": 0.8,
"pop": 7.8
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
},
"axis": {
"title": "GDP ($ trillions)"
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
},
"axis": {
"title": "Population (millions)"
}
}
}
},
{
"mark": {
"type": "text",
"dy": -14
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
}
},
"text": {
"field": "label",
"type": "nominal"
},
"size": {
"field": "gdp",
"type": "quantitative",
"scale": {
"range": [
11,
22
]
}
}
}
}
]
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "California has the largest economy and population of any state",
"subtitle": "The ten largest US state economies by GDP and population, 2023"
},
"layer": [
{
"mark": {
"type": "point",
"fill": "#0e7490",
"opacity": 0.85,
"trendline": false
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
},
{
"state": "Pennsylvania",
"label": "PA",
"gdp": 1,
"pop": 12.9
},
{
"state": "Ohio",
"label": "OH",
"gdp": 0.9,
"pop": 11.8
},
{
"state": "Georgia",
"label": "GA",
"gdp": 0.8,
"pop": 11
},
{
"state": "New Jersey",
"label": "NJ",
"gdp": 0.8,
"pop": 9.3
},
{
"state": "Washington",
"label": "WA",
"gdp": 0.8,
"pop": 7.8
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
},
"axis": {
"title": "GDP ($ trillions)"
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
},
"axis": {
"title": "Population (millions)"
}
}
}
},
{
"mark": {
"type": "text",
"dy": -14
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
}
},
"text": {
"field": "label",
"type": "nominal"
},
"size": {
"field": "gdp",
"type": "quantitative",
"scale": {
"range": [
11,
22
]
}
}
}
}
]
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "California has the largest economy and population of any state",
"subtitle": "The ten largest US state economies by GDP and population, 2023"
},
"layer": [
{
"mark": {
"type": "point",
"fill": "#0e7490",
"opacity": 0.85,
"trendline": false
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
},
{
"state": "Pennsylvania",
"label": "PA",
"gdp": 1,
"pop": 12.9
},
{
"state": "Ohio",
"label": "OH",
"gdp": 0.9,
"pop": 11.8
},
{
"state": "Georgia",
"label": "GA",
"gdp": 0.8,
"pop": 11
},
{
"state": "New Jersey",
"label": "NJ",
"gdp": 0.8,
"pop": 9.3
},
{
"state": "Washington",
"label": "WA",
"gdp": 0.8,
"pop": 7.8
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
},
"axis": {
"title": "GDP ($ trillions)"
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
},
"axis": {
"title": "Population (millions)"
}
}
}
},
{
"mark": {
"type": "text",
"dy": -14
},
"data": [
{
"state": "California",
"label": "CA",
"gdp": 3.9,
"pop": 39
},
{
"state": "Texas",
"label": "TX",
"gdp": 2.6,
"pop": 30.5
},
{
"state": "New York",
"label": "NY",
"gdp": 2.1,
"pop": 19.6
},
{
"state": "Florida",
"label": "FL",
"gdp": 1.6,
"pop": 22.6
},
{
"state": "Illinois",
"label": "IL",
"gdp": 1.1,
"pop": 12.5
}
],
"encoding": {
"x": {
"field": "gdp",
"type": "quantitative",
"scale": {
"domain": [
0.6,
4.25
]
}
},
"y": {
"field": "pop",
"type": "quantitative",
"scale": {
"domain": [
5,
43
]
}
},
"text": {
"field": "label",
"type": "nominal"
},
"size": {
"field": "gdp",
"type": "quantitative",
"scale": {
"range": [
11,
22
]
}
}
}
}
]
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Two series on independent axes
resolve: { scale: { y: "independent" } } gives each layer its own y axis, so series in different units can share one x axis. Color each axis to match its series with axis.labelColor. Here net revenue is in dollars and enrollment is a headcount, and a condition on the bar color turns the deficit years red.
{
"chrome": {
"title": "Net revenue fell into deficit as enrollment declined",
"subtitle": "Net revenue (bars) and undergraduate enrollment (line) on independent y-axes, 2014-2024"
},
"resolve": {
"scale": {
"y": "independent"
}
},
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.85
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "revenue",
"type": "quantitative",
"axis": {
"title": "Net revenue ($)",
"format": "~s",
"labelColor": "#0e7490",
"values": [
-40000000,
-20000000,
0,
20000000,
40000000,
60000000,
80000000
]
}
},
"color": {
"condition": {
"test": {
"field": "revenue",
"gte": 0
},
"value": "#0e7490"
},
"value": "#d64045"
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "line",
"stroke": "#e07b39",
"strokeWidth": 2.5,
"point": true,
"interpolate": "monotone"
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "enrollment",
"type": "quantitative",
"axis": {
"title": "Enrollment",
"format": "~s",
"labelColor": "#e07b39"
},
"scale": {
"domain": [
46000,
66000
]
}
}
},
"labels": {
"density": "none"
}
}
]
}import { Chart } from "@opendata-ai/openchart-react";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Net revenue fell into deficit as enrollment declined",
"subtitle": "Net revenue (bars) and undergraduate enrollment (line) on independent y-axes, 2014-2024"
},
"resolve": {
"scale": {
"y": "independent"
}
},
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.85
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "revenue",
"type": "quantitative",
"axis": {
"title": "Net revenue ($)",
"format": "~s",
"labelColor": "#0e7490",
"values": [
-40000000,
-20000000,
0,
20000000,
40000000,
60000000,
80000000
]
}
},
"color": {
"condition": {
"test": {
"field": "revenue",
"gte": 0
},
"value": "#0e7490"
},
"value": "#d64045"
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "line",
"stroke": "#e07b39",
"strokeWidth": 2.5,
"point": true,
"interpolate": "monotone"
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "enrollment",
"type": "quantitative",
"axis": {
"title": "Enrollment",
"format": "~s",
"labelColor": "#e07b39"
},
"scale": {
"domain": [
46000,
66000
]
}
}
},
"labels": {
"density": "none"
}
}
]
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Net revenue fell into deficit as enrollment declined",
"subtitle": "Net revenue (bars) and undergraduate enrollment (line) on independent y-axes, 2014-2024"
},
"resolve": {
"scale": {
"y": "independent"
}
},
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.85
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "revenue",
"type": "quantitative",
"axis": {
"title": "Net revenue ($)",
"format": "~s",
"labelColor": "#0e7490",
"values": [
-40000000,
-20000000,
0,
20000000,
40000000,
60000000,
80000000
]
}
},
"color": {
"condition": {
"test": {
"field": "revenue",
"gte": 0
},
"value": "#0e7490"
},
"value": "#d64045"
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "line",
"stroke": "#e07b39",
"strokeWidth": 2.5,
"point": true,
"interpolate": "monotone"
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "enrollment",
"type": "quantitative",
"axis": {
"title": "Enrollment",
"format": "~s",
"labelColor": "#e07b39"
},
"scale": {
"domain": [
46000,
66000
]
}
}
},
"labels": {
"density": "none"
}
}
]
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Net revenue fell into deficit as enrollment declined",
"subtitle": "Net revenue (bars) and undergraduate enrollment (line) on independent y-axes, 2014-2024"
},
"resolve": {
"scale": {
"y": "independent"
}
},
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.85
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "revenue",
"type": "quantitative",
"axis": {
"title": "Net revenue ($)",
"format": "~s",
"labelColor": "#0e7490",
"values": [
-40000000,
-20000000,
0,
20000000,
40000000,
60000000,
80000000
]
}
},
"color": {
"condition": {
"test": {
"field": "revenue",
"gte": 0
},
"value": "#0e7490"
},
"value": "#d64045"
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "line",
"stroke": "#e07b39",
"strokeWidth": 2.5,
"point": true,
"interpolate": "monotone"
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "enrollment",
"type": "quantitative",
"axis": {
"title": "Enrollment",
"format": "~s",
"labelColor": "#e07b39"
},
"scale": {
"domain": [
46000,
66000
]
}
}
},
"labels": {
"density": "none"
}
}
]
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { LayerSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const spec: LayerSpec = {
"chrome": {
"title": "Net revenue fell into deficit as enrollment declined",
"subtitle": "Net revenue (bars) and undergraduate enrollment (line) on independent y-axes, 2014-2024"
},
"resolve": {
"scale": {
"y": "independent"
}
},
"layer": [
{
"mark": {
"type": "bar",
"opacity": 0.85
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "revenue",
"type": "quantitative",
"axis": {
"title": "Net revenue ($)",
"format": "~s",
"labelColor": "#0e7490",
"values": [
-40000000,
-20000000,
0,
20000000,
40000000,
60000000,
80000000
]
}
},
"color": {
"condition": {
"test": {
"field": "revenue",
"gte": 0
},
"value": "#0e7490"
},
"value": "#d64045"
}
},
"labels": {
"density": "none"
}
},
{
"mark": {
"type": "line",
"stroke": "#e07b39",
"strokeWidth": 2.5,
"point": true,
"interpolate": "monotone"
},
"data": [
{
"year": "2014",
"revenue": 68000000,
"enrollment": 61200
},
{
"year": "2015",
"revenue": 72000000,
"enrollment": 62400
},
{
"year": "2016",
"revenue": 65000000,
"enrollment": 61500
},
{
"year": "2017",
"revenue": 58000000,
"enrollment": 60300
},
{
"year": "2018",
"revenue": 51000000,
"enrollment": 58900
},
{
"year": "2019",
"revenue": 42000000,
"enrollment": 57100
},
{
"year": "2020",
"revenue": 18000000,
"enrollment": 55400
},
{
"year": "2021",
"revenue": 5000000,
"enrollment": 54200
},
{
"year": "2022",
"revenue": -8000000,
"enrollment": 52800
},
{
"year": "2023",
"revenue": -21000000,
"enrollment": 51600
},
{
"year": "2024",
"revenue": -35000000,
"enrollment": 50300
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "enrollment",
"type": "quantitative",
"axis": {
"title": "Enrollment",
"format": "~s",
"labelColor": "#e07b39"
},
"scale": {
"domain": [
46000,
66000
]
}
}
},
"labels": {
"density": "none"
}
}
]
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fields that matter
| Field | What it does |
|---|---|
layer |
An array of chart specs drawn on one plot, each with its own mark, and its own data and encoding unless shared at the top level. |
resolve.scale.y |
"independent" gives each layer its own y scale and axis. |
encoding.y.scale.domain |
Pins the shared axis domain, for example to add headroom for labels. |
mark: { type: "rule" } |
A reference line drawn from data, colored by the color scale. |
mark: { type: "text", dy } |
Labels from the text channel, offset in pixels by dy. |
encoding.y.axis.labelColor |
Colors an axis to match its layer. |
chrome |
Title and subtitle, set once at the top level. |
Related
- Facets and small multiples for one chart per category.
- Column for annotations on a single chart.
- Line for series that share one axis.
- LayerSpec reference lists the layer fields and what a layer inherits.