Heatmap
Color a grid of two categories by a number to read patterns across rows and columns, then change the color scheme, pin the color domain and order the rows.
A heatmap puts one cell on a grid of two categorical fields and colors it by a number. It suits a source-by-year or feature-by-feature matrix, where the reader looks for the darkest rows and columns.
In OpenChart the heatmap is the rect mark. Use a scatter chart when both fields are numbers. For a daily series laid out as a year grid, use the calendar, and for color inside table rows, see column types.
A basic heatmap
Set mark to "rect", put one category on x and the other on y, and a quantitative field on color. scale.scheme names the color ramp and format is a d3-format string for the value in tooltips and on the color legend.
{
"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);
Another color scheme
scale.scheme takes a named scheme. The sequential names are blue, green, orange, purple and teal, and common Vega-Lite names such as blues are accepted as aliases. An unknown name fails validation and lists the supported ones.
{
"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": "orange"
},
"format": ".1f"
}
},
"chrome": {
"title": "The same matrix in another sequential scheme",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "orange"
},
"format": ".1f"
}
},
"chrome": {
"title": "The same matrix in another sequential scheme",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "orange"
},
"format": ".1f"
}
},
"chrome": {
"title": "The same matrix in another sequential scheme",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "orange"
},
"format": ".1f"
}
},
"chrome": {
"title": "The same matrix in another sequential scheme",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "orange"
},
"format": ".1f"
}
},
"chrome": {
"title": "The same matrix in another sequential scheme",
"subtitle": "Share of US electricity generation by source (%), 2016-2023"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A fixed color domain
scale.domain pins the color range to [min, max]. A fixed 0 to 100 domain colors each cell against the full percentage range, so small shares stay pale.
{
"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",
"domain": [
0,
100
]
},
"format": ".1f"
}
},
"chrome": {
"title": "A 0 to 100 domain shows how small the renewables are",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues",
"domain": [
0,
100
]
},
"format": ".1f"
}
},
"chrome": {
"title": "A 0 to 100 domain shows how small the renewables are",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues",
"domain": [
0,
100
]
},
"format": ".1f"
}
},
"chrome": {
"title": "A 0 to 100 domain shows how small the renewables are",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues",
"domain": [
0,
100
]
},
"format": ".1f"
}
},
"chrome": {
"title": "A 0 to 100 domain shows how small the renewables are",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal"
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues",
"domain": [
0,
100
]
},
"format": ".1f"
}
},
"chrome": {
"title": "A 0 to 100 domain shows how small the renewables are",
"subtitle": "Share of US electricity generation by source (%), 2016-2023"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Ordering rows
An array on the sort field of a nominal channel sets the order by hand. The array lists the rows from top to bottom, so this order puts Gas, the largest source, at the top.
{
"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",
"sort": [
"Gas",
"Coal",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Rows set by hand, with the largest source on top",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal",
"sort": [
"Gas",
"Coal",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Rows set by hand, with the largest source on top",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal",
"sort": [
"Gas",
"Coal",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Rows set by hand, with the largest source on top",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal",
"sort": [
"Gas",
"Coal",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Rows set by hand, with the largest source on top",
"subtitle": "Share of US electricity generation by source (%), 2016-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": "rect",
"encoding": {
"x": {
"field": "year",
"type": "nominal"
},
"y": {
"field": "source",
"type": "nominal",
"sort": [
"Gas",
"Coal",
"Nuclear",
"Hydro",
"Wind",
"Solar"
]
},
"color": {
"field": "share",
"type": "quantitative",
"scale": {
"scheme": "blues"
},
"format": ".1f"
}
},
"chrome": {
"title": "Rows set by hand, with the largest source on top",
"subtitle": "Share of US electricity generation by source (%), 2016-2023"
}
};
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 |
|---|---|
mark |
"rect" draws one cell per row. |
encoding.x, encoding.y |
The two nominal fields that form the grid. |
encoding.y.sort |
An array of names that sets the row order by hand. |
encoding.color |
The quantitative field that sets each cell’s color. |
encoding.color.scale.scheme |
The named color scheme, such as "blues" or "orange". |
encoding.color.scale.domain |
Pins the color range to [min, max]. |
encoding.color.format |
A d3-format string for the value shown in tooltips and on the color legend. |
Related
- Scatter: relate two numbers.
- Bubble: add a third number as size.
- Calendar: a daily value as a year grid.
- Column types: heatmap cells inside a table.
- Stacked 100%: the share of each part as stacked bars.
- Encoding reference: every channel with its scale and axis settings.