Field types and scales
Nominal, ordinal, quantitative and temporal fields, and the scale settings for domain, zero, log and color ramps.
A channel’s type picks its scale. These two charts plot the same 14 rows, one with type: "ordinal" on x and one with type: "temporal".
{
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"data": [
{
"year": "1900",
"anomaly": -0.08
},
{
"year": "1910",
"anomaly": -0.43
},
{
"year": "1920",
"anomaly": -0.27
},
{
"year": "1930",
"anomaly": -0.16
},
{
"year": "1940",
"anomaly": 0.12
},
{
"year": "1950",
"anomaly": -0.17
},
// … 8 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "On an ordinal axis, the five years from 2020 to 2025 get as much width as a decade",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "On an ordinal axis, the five years from 2020 to 2025 get as much width as a decade",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "On an ordinal axis, the five years from 2020 to 2025 get as much width as a decade",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "On an ordinal axis, the five years from 2020 to 2025 get as much width as a decade",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "On an ordinal axis, the five years from 2020 to 2025 get as much width as a decade",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 2025"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
{
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"data": [
{
"year": "1900",
"anomaly": -0.08
},
{
"year": "1910",
"anomaly": -0.43
},
{
"year": "1920",
"anomaly": -0.27
},
{
"year": "1930",
"anomaly": -0.16
},
{
"year": "1940",
"anomaly": 0.12
},
{
"year": "1950",
"anomaly": -0.17
},
// … 8 more rows; Copy includes them
],
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "Global temperature rose 1.16°C from 1970 to 2025",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "Global temperature rose 1.16°C from 1970 to 2025",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "Global temperature rose 1.16°C from 1970 to 2025",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "Global temperature rose 1.16°C from 1970 to 2025",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 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 = [/* 14 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"stroke": "#0e7490",
"strokeWidth": 2,
"point": true
},
"encoding": {
"x": {
"field": "year",
"type": "temporal",
"axis": {
"format": "%Y"
}
},
"y": {
"field": "anomaly",
"type": "quantitative",
"axis": {
"title": "Difference from the 1951–1980 average",
"format": "+.1f",
"labelSuffix": "°C"
}
}
},
"chrome": {
"title": "Global temperature rose 1.16°C from 1970 to 2025",
"subtitle": "Global surface temperature against the 1951–1980 average (°C), one reading per decade from 1900, then 2025"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Ordinal or temporal
An ordinal axis treats each year as a category and gives every one the same width, so the five years from 2020 to 2025 look as long as each decade before them. A temporal axis places each reading by its date, and the last segment shrinks to half a step. Use temporal whenever the values are dates; use ordinal for labels that are not dates, such as month names without a year or fiscal quarters.
Zero and domain
{
"mark": {
"type": "point",
"trendline": false,
"fill": "#0e7490",
"size": 6
},
"data": [
{
"country": "India",
"lifeExpectancy": 68.3
},
{
"country": "China",
"lifeExpectancy": 76.1
},
{
"country": "United States",
"lifeExpectancy": 79.2
},
{
"country": "Indonesia",
"lifeExpectancy": 69
},
{
"country": "Brazil",
"lifeExpectancy": 75.3
},
{
"country": "Mexico",
"lifeExpectancy": 76.9
},
{
"country": "Japan",
"lifeExpectancy": 83.6
},
{
"country": "Germany",
"lifeExpectancy": 80.8
},
{
"country": "United Kingdom",
"lifeExpectancy": 81.4
},
{
"country": "France",
"lifeExpectancy": 82.4
},
{
"country": "South Korea",
"lifeExpectancy": 81.9
},
{
"country": "Canada",
"lifeExpectancy": 82.2
}
],
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x",
"axis": {
"values": [
"India",
"China",
"United States",
"Indonesia",
"Brazil",
"Mexico",
"Japan",
"Germany",
"United Kingdom",
"France",
"South Korea",
"Canada"
]
}
},
"x": {
"field": "lifeExpectancy",
"type": "quantitative",
"scale": {
"zero": false,
"domain": [
65,
85
]
},
"axis": {
"title": "Life expectancy at birth (years)",
"grid": true
}
}
},
"chrome": {
"title": "Life expectancy runs from 68.3 years in India to 83.6 in Japan",
"subtitle": "Life expectancy at birth in 12 large economies (years). The axis starts at 65."
}
}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",
"trendline": false,
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x",
"axis": {
"values": [
"India",
"China",
"United States",
"Indonesia",
"Brazil",
"Mexico",
"Japan",
"Germany",
"United Kingdom",
"France",
"South Korea",
"Canada"
]
}
},
"x": {
"field": "lifeExpectancy",
"type": "quantitative",
"scale": {
"zero": false,
"domain": [
65,
85
]
},
"axis": {
"title": "Life expectancy at birth (years)",
"grid": true
}
}
},
"chrome": {
"title": "Life expectancy runs from 68.3 years in India to 83.6 in Japan",
"subtitle": "Life expectancy at birth in 12 large economies (years). The axis starts at 65."
}
};
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",
"trendline": false,
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x",
"axis": {
"values": [
"India",
"China",
"United States",
"Indonesia",
"Brazil",
"Mexico",
"Japan",
"Germany",
"United Kingdom",
"France",
"South Korea",
"Canada"
]
}
},
"x": {
"field": "lifeExpectancy",
"type": "quantitative",
"scale": {
"zero": false,
"domain": [
65,
85
]
},
"axis": {
"title": "Life expectancy at birth (years)",
"grid": true
}
}
},
"chrome": {
"title": "Life expectancy runs from 68.3 years in India to 83.6 in Japan",
"subtitle": "Life expectancy at birth in 12 large economies (years). The axis starts at 65."
}
};
</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",
"trendline": false,
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x",
"axis": {
"values": [
"India",
"China",
"United States",
"Indonesia",
"Brazil",
"Mexico",
"Japan",
"Germany",
"United Kingdom",
"France",
"South Korea",
"Canada"
]
}
},
"x": {
"field": "lifeExpectancy",
"type": "quantitative",
"scale": {
"zero": false,
"domain": [
65,
85
]
},
"axis": {
"title": "Life expectancy at birth (years)",
"grid": true
}
}
},
"chrome": {
"title": "Life expectancy runs from 68.3 years in India to 83.6 in Japan",
"subtitle": "Life expectancy at birth in 12 large economies (years). The axis starts at 65."
}
};
</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",
"trendline": false,
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x",
"axis": {
"values": [
"India",
"China",
"United States",
"Indonesia",
"Brazil",
"Mexico",
"Japan",
"Germany",
"United Kingdom",
"France",
"South Korea",
"Canada"
]
}
},
"x": {
"field": "lifeExpectancy",
"type": "quantitative",
"scale": {
"zero": false,
"domain": [
65,
85
]
},
"axis": {
"title": "Life expectancy at birth (years)",
"grid": true
}
}
},
"chrome": {
"title": "Life expectancy runs from 68.3 years in India to 83.6 in Japan",
"subtitle": "Life expectancy at birth in 12 large economies (years). The axis starts at 65."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Whether a quantitative scale includes zero depends on the mark. Bars, areas and circles include it, because their length or position is read from the baseline. Line, point, beeswarm, range and text marks fit the data instead.
This dot plot uses mark: { type: "point", trendline: false }, which draws no stems, so an axis from 65 is fair and scale: { zero: false, domain: [65, 85] } fixes the range to the span that matters. Without trendline: false, a point mark fits a line through the dots. circle and lollipop draw a stem from zero to each dot, so keep zero for them, as on any bar or area chart.
Log scale
{
"mark": {
"type": "circle",
"fill": "#0e7490",
"size": 6
},
"data": [
{
"country": "India",
"population": 1438.069596
},
{
"country": "China",
"population": 1410.71
},
{
"country": "United States",
"population": 336.806231
},
{
"country": "Indonesia",
"population": 281.190067
},
{
"country": "Brazil",
"population": 211.140729
},
{
"country": "Mexico",
"population": 129.739759
},
{
"country": "Japan",
"population": 124.51665
},
{
"country": "Germany",
"population": 83.287273
},
{
"country": "United Kingdom",
"population": 68.492
},
{
"country": "France",
"population": 68.372286
},
{
"country": "South Korea",
"population": 51.712619
},
{
"country": "Canada",
"population": 40.083484
}
],
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "population",
"type": "quantitative",
"scale": {
"type": "log",
"domain": [
30,
1500
],
"nice": false
},
"axis": {
"title": "Population (millions, log scale)",
"format": ",.0f",
"values": [
30,
100,
300,
1000
],
"grid": true
}
}
},
"chrome": {
"title": "India has about 36 times as many people as Canada",
"subtitle": "Population of 12 large economies, 2023 (millions). Equal distances on the axis are equal ratios."
}
}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": "circle",
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "population",
"type": "quantitative",
"scale": {
"type": "log",
"domain": [
30,
1500
],
"nice": false
},
"axis": {
"title": "Population (millions, log scale)",
"format": ",.0f",
"values": [
30,
100,
300,
1000
],
"grid": true
}
}
},
"chrome": {
"title": "India has about 36 times as many people as Canada",
"subtitle": "Population of 12 large economies, 2023 (millions). Equal distances on the axis are equal ratios."
}
};
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": "circle",
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "population",
"type": "quantitative",
"scale": {
"type": "log",
"domain": [
30,
1500
],
"nice": false
},
"axis": {
"title": "Population (millions, log scale)",
"format": ",.0f",
"values": [
30,
100,
300,
1000
],
"grid": true
}
}
},
"chrome": {
"title": "India has about 36 times as many people as Canada",
"subtitle": "Population of 12 large economies, 2023 (millions). Equal distances on the axis are equal ratios."
}
};
</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": "circle",
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "population",
"type": "quantitative",
"scale": {
"type": "log",
"domain": [
30,
1500
],
"nice": false
},
"axis": {
"title": "Population (millions, log scale)",
"format": ",.0f",
"values": [
30,
100,
300,
1000
],
"grid": true
}
}
},
"chrome": {
"title": "India has about 36 times as many people as Canada",
"subtitle": "Population of 12 large economies, 2023 (millions). Equal distances on the axis are equal ratios."
}
};
</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": "circle",
"fill": "#0e7490",
"size": 6
},
"encoding": {
"y": {
"field": "country",
"type": "nominal",
"sort": "-x"
},
"x": {
"field": "population",
"type": "quantitative",
"scale": {
"type": "log",
"domain": [
30,
1500
],
"nice": false
},
"axis": {
"title": "Population (millions, log scale)",
"format": ",.0f",
"values": [
30,
100,
300,
1000
],
"grid": true
}
}
},
"chrome": {
"title": "India has about 36 times as many people as Canada",
"subtitle": "Population of 12 large economies, 2023 (millions). Equal distances on the axis are equal ratios."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
scale: { type: "log" } spaces values by ratio, so the gap from 30 to 300 million equals the gap from 100 million to 1 billion. On a linear axis, India and China would sit far to the right and the other ten countries would crowd into the left quarter. axis.values picks round ticks, and the axis title says “log scale” so no reader takes the spacing as linear.
Sequential color
{
"mark": "rect",
"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": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Gas rose as coal's share halved",
"subtitle": "Share of US electricity generation by source (%), 2016-2023. Darker cells = larger share."
}
}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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Gas rose as coal's share halved",
"subtitle": "Share of US electricity generation by source (%), 2016-2023. Darker cells = larger share."
}
};
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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Gas rose as coal's share halved",
"subtitle": "Share of US electricity generation by source (%), 2016-2023. Darker cells = larger share."
}
};
</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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Gas rose as coal's share halved",
"subtitle": "Share of US electricity generation by source (%), 2016-2023. Darker cells = larger share."
}
};
</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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Gas rose as coal's share halved",
"subtitle": "Share of US electricity generation by source (%), 2016-2023. Darker cells = larger share."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
On rect heatmaps and point marks, a quantitative field on color gets a sequential scale. Bars do not shade by a quantitative color field yet, so give them a condition instead, as the encoding page does. scheme names the ramp: blue, green, orange, purple or teal, with Vega-Lite names such as blues accepted. Add domain to fix the ends, for example [0, 100] for a share, so two heatmaps of the same measure use the same colors.
Fields that matter
| Field | What it does |
|---|---|
type |
quantitative, temporal, nominal or ordinal. Picks the default scale. |
scale.type |
Overrides the scale, for example "log", "sqrt" or "symlog". |
scale.zero |
Whether a quantitative domain includes zero. On by default for bar, area and circle marks; off for line, point, beeswarm, range and text. |
scale.domain |
Fixed [min, max] for continuous scales, or the category list for categorical ones. |
scale.nice |
Rounds the domain out to clean tick values. Default true. |
scale.scheme |
A named color ramp for the color channel. |
scale.range |
Explicit output values, such as exact colors. Wins over scheme. |
Related
- Heatmap uses sequential color on a grid.
- Line shows a log scale on a growth series.
- Axes covers tick formats and gridlines.
- Encoding reference lists every scale option.