Skip to content

Heatmap ​

A grid of cells colored by value: rows and columns of labels around a matrix of numbers, with a continuous color scale and its gradient legend. Suits a scores table (locations by models), a parameter sweep or counts by day and hour. Shares the title, legend strip, tooltip, menu and CSV wiring with LineChart and BarChart.

Data ​

rows and columns are the labels; data is one array per row, with one entry per column (data[rowIndex][columnIndex]), so a typed-array column from a model output is one row of the matrix. A missing or non-finite entry leaves its cell empty (see missing values).

Long-format records, one per cell, pivot into that shape with pivotCells:

ts
import { pivotCells } from "@cfasim-ui/charts";

const scores = [
  { location: "Alabama", model: "Ensemble", wis: 1.52 },
  { location: "Alabama", model: "Baseline", wis: 2.21 },
  // …
];
// { rows: ["Alabama", …], columns: ["Ensemble", "Baseline"], data: [[1.52, 2.21], …] }
const matrix = pivotCells(scores, {
  row: "location",
  column: "model",
  value: "wis",
});

Labels keep their first-seen order unless rows / columns are given, which also drop records outside them. Accessor functions work in place of field names (row: (r) => r.location.toUpperCase()).

Color scale ​

colorScale maps values to fills (see ColorScale): range lists two or more colors mixed in Oklab (default viridis), domain fixes the values at the ends (default the data's extent), label titles the legend and format its end labels (defaulting to valueFormat). Hex and rgb() colors are mixed in JS; var() colors go through CSS color-mix() and so follow the theme.

A three-value domain with three colors makes a diverging scale with the middle color at the middle value. The legend then labels the middle value too, under the middle of the ramp, so readers can see where the neutral point sits:

Correlation-110R₀latentinfectiousreportingdispersionR₀latentinfectiousreportingdispersion1.000.42-0.180.05-0.610.421.00-0.330.12-0.20-0.18-0.331.000.080.270.050.120.081.00–-0.61-0.200.27–1.00
vue
<Heatmap
  :rows="params"
  :columns="params"
  :data="correlation"
  :color-scale="{
    domain: [-1, 0, 1],
    range: ['#b2182b', '#f7f7f7', '#2166ac'],
    label: 'Correlation',
  }"
  :cell-labels="{ format: '%.2f' }"
  :cell-gap="2"
  :cell-radius="3"
  missing-color="var(--color-bg-subtle)"
  missing-label="–"
  :cell-height="36"
  :width="420"
  tooltip-trigger="hover"
/>

Pass colorScale: { legend: false } to drop the legend; legendPosition: "bottom" moves it under the plot.

Cell labels ​

cellLabels prints each value on its cell in black or white, whichever reads better on the fill. A label that does not fit its cell is left out, so narrow columns or short rows stay clean. Pass an object to set format, fontSize, fontWeight or a fixed color.

valueFormat formats the labels, the tooltip and the legend ends alike: a preset such as "percent:1", a printf string such as "%.2f", or a function.

Axes ​

Column labels sit under the plot by default (columnAxis: "bottom"), above it with "top", or not at all with "none"; rowAxis: "none" drops the row labels. Row labels take the width of the widest one or of rowHeader, whichever is wider (capped at 40% of the chart, or rowLabelWidth). Column labels turn diagonal or vertical on their own when they would collide (columnLabelAngle: "auto"); pass a number of degrees between -90 and 90 to fix the angle. xLabel and yLabel title the axes and rowHeader heads the row labels like a table's first column.

With an explicit height the rows share the plot. Here 24 columns turn the hour labels vertical:

Clinic visits by hourVisits456MonTueWedThuFriSatSun00:0001:0002:0003:0004:0005:0006:0007:0008:0009:0010:0011:0012:0013:0014:0015:0016:0017:0018:0019:0020:0021:0022:0023:00DayHour
vue
<Heatmap
  :rows="days"
  :columns="hours"
  :data="visits"
  :height="260"
  x-label="Hour"
  y-label="Day"
  :color-scale="{ label: 'Visits' }"
  title="Clinic visits by hour"
  tooltip-trigger="hover"
/>

Tilted labels that would run past the chart's edge get extra side padding, so the plot gives up a little width to keep them whole. A label is only shortened with … when that would take more than a fifth of the width, or when the labels would take more than 40% of an explicit height.

WIS0.512.46AlabamaColoradoDelawareHawaiiEnsemble forecastBaseline persistenceRenewal equationTrend extrapolationMechanistic SEIRGradient boosting
vue
<Heatmap
  :rows="locations"
  :columns="modelNames"
  :data="modelScores"
  column-axis="top"
  :column-label-angle="-45"
  :color-scale="{ label: 'WIS' }"
/>

Wide tables ​

The chart fits its container by default, so with many columns or a narrow screen the cell labels that no longer fit drop out. minCellWidth keeps every column at least that many pixels wide instead: when they would be narrower, the chart draws wider than its container and the plot scrolls sideways, with the row labels and rowHeader pinned at the left. The title and legend scroll with the plot. The pinned strip is painted with --heatmap-pinned-bg (default the theme background), so set it when the chart sits on another color. In a grid or flex item, set min-width: 0 on the item, as for any wide content, or the drawn width widens the track instead of scrolling.

WIS0.512.46AlabamaColoradoDelawareHawaiiLocationEnsemble forecastBaseline persistenceRenewal equationTrend extrapolationMechanistic SEIRGradient boosting1.502.462.020.810.621.722.342.251.060.521.422.442.411.340.511.132.292.301.640.580.872.082.441.42
vue
<Heatmap
  :rows="locations"
  :columns="modelNames"
  :data="modelScores"
  :min-cell-width="120"
  column-axis="top"
  row-header="Location"
  cell-labels
  value-format="%.2f"
  :color-scale="{ label: 'WIS' }"
  tooltip-trigger="hover"
  interactive
/>

Missing values ​

A NaN, null or absent entry draws no cell. missingColor fills those cells instead (the diverging demo above uses the theme's subtle background), and they read as — in the tooltip and the keyboard labels. A pale missing fill is easy to mistake for a low value, so missingLabel prints text such as "–" on missing cells, in black or white against missingColor (or in the text color without one). It uses the cellLabels style, and works without cellLabels too.

Tooltip and events ​

tooltipTrigger: "hover" (or "click") shows the hovered cell's row as the title and its column and value as the row, with a swatch of the cell color. tooltipTitle rewrites the title. The tooltip slot gets the shared chart props (index, values, title, format, data from tooltipData) plus the cell itself: row, column, rowIndex, columnIndex, value and color. index is the cell's row-major position (rowIndex * columns.length + columnIndex), which is also what tooltipData is indexed by. The tooltip closes when rows or columns change (re-sorting, say), since the cell under it is then a different one; new data alone keeps it open with the new value.

vue
<Heatmap :rows :columns :data tooltip-trigger="hover">
  <template #tooltip="{ row, column, value, format }">
    <strong>{{ row }}</strong> scored {{ format(value) }} with {{ column }}
  </template>
</Heatmap>

Events: hover with { index } (or null), cell-click with the cell, row-click and column-click with { index, label } from a click on a label.

The CSV download heads the row column with rowHeader, else yLabel, else row.

Accessibility ​

The <svg> is exposed as one labeled image (role="img" with the title or ariaLabel), as on the other charts. interactive adds a real <button> over every cell and row label, laid over the svg outside it: the grid is one Tab stop, the arrow keys move between cells (Home reaches the row label, End the last column), a focused cell shows the tooltip and Enter or Space emits cell-click / row-click. Escape hides the tooltip (on every chart, for hover too) and leaves focus where it is; the next arrow key or pointer move shows it again. Pointer events pass through to the plot, so hover and click work the same with or without it.

Each cell button is named row, column: value and each row button by its label. Screen readers do not announce the tooltip, so when the tooltip slot shows more than the value, say the same in cellAriaLabel; rowAriaLabel does the same for the row buttons, for example to say what clicking does:

vue
<Heatmap
  :rows
  :columns
  :data
  interactive
  :cell-aria-label="
    (cell) =>
      `${cell.row}, ${cell.column}. Mean CRPS: ${cell.value.toFixed(3)}. ` +
      `Change vs baseline: ${change(cell)}. Open forecast detail.`
  "
  :row-aria-label="(row) => `${row.label}. Open forecast detail.`"
/>

Examples ​

Scores table ​

A model-evaluation table: column labels on top, values on the cells, keyboard access, and page-driven sorting (reorder rows and data together).

Sort locations by
Mean WIS0.823.04AlabamaColoradoDelawareHawaiiIdahoKansasMaineNevadaOregonTennesseeVirginiaWyomingLocationEnsembleBaselineRenewalTrend1.402.602.241.981.992.211.321.471.241.471.582.030.842.162.202.581.693.041.941.811.882.341.481.410.981.581.622.461.032.172.142.301.912.532.031.691.631.871.301.500.821.961.642.761.312.882.481.95
vue
<script setup>
import { computed, ref } from "vue";

const models = ["Ensemble", "Baseline", "Renewal", "Trend"];
const locations = ["Alabama", "Colorado", "Delaware" /* … */];
const wis = [[1.52, 2.21, 1.84, 2.03] /* one array per location */];

const sortBy = ref("name");
const order = computed(() => {
  const idx = locations.map((_, i) => i);
  if (sortBy.value !== "name") {
    const c = Number(sortBy.value);
    idx.sort((a, b) => wis[a][c] - wis[b][c]);
  }
  return idx;
});
const rows = computed(() => order.value.map((i) => locations[i]));
const data = computed(() => order.value.map((i) => wis[i]));
</script>

<template>
  <Heatmap
    :rows="rows"
    :columns="models"
    :data="data"
    :color-scale="{ range: ['#deebf7', '#08519c'], label: 'Mean WIS' }"
    value-format="%.2f"
    cell-labels
    column-axis="top"
    row-header="Location"
    :cell-height="32"
    :cell-gap="2"
    :cell-radius="2"
    tooltip-trigger="hover"
    interactive
    @cell-click="(cell) => openDetail(cell.row, cell.column)"
    @row-click="(row) => openDetail(row.label)"
  />
</template>

Props ​

PropTypeRequiredDefault
widthnumberNo—
heightnumberNo—
titlestringNo—
titleStyleTitleStyleNo—
rolestringNo—
ariaLabelstringNo—
axisLabelStyleLabelStyleNo—
tickLabelStyleLabelStyleNo—
legendStyleLabelStyleNo—
legendPosition"top" | "bottom"No—
xLabelstringNo—
yLabelstringNo—
xLabelAlign"left" | "center"No—
debouncenumberNo—
menuboolean | stringNotrue
menuItemsChartMenuItemKey[]No—
tooltipDataArrayLike&lt;unknown&gt;No—
tooltipTrigger"hover" | "click" | "none"No—
tooltipClamp"none" | "chart" | "window"No—
tooltipValueFormatNumberFormatNo—
tooltipTitle(label: string, index: number) =&gt; stringNo—
csvstring | (() =&gt; string)No—
filenamestringNo—
downloadLinkboolean | stringNo—
downloadButtonboolean | stringNo—
fullscreenTargetstring | HTMLElementNo—
chartPaddingChartPaddingNo—
rowsreadonly string[]Yes—
columnsreadonly string[]Yes—
datareadonly ArrayLike&lt;number&gt;[]Yes—
colorScaleColorScaleNo—
valueFormatNumberFormatNo—
cellLabelsboolean | HeatmapCellLabelsNo—
cellGapnumberNo1
cellRadiusnumberNo0
cellStrokestringNo—
cellStrokeWidthnumberNo1
missingColorstringNo—
missingLabelstringNo—
cellAriaLabel(cell: HeatmapCell) =&gt; stringNo—
rowAriaLabel(row: HeatmapAxisItem) =&gt; stringNo—
cellHeightnumberNo28
columnAxis"top" | "bottom" | "none"No"bottom"
rowAxis"left" | "none"No"left"
columnLabelAnglenumber | "auto"No"auto"
rowLabelWidthnumberNo—
minCellWidthnumberNo—
rowHeaderstringNo—
interactivebooleanNo—

Events ​

EventPayload
hoverChartHoverPayload
cellClickHeatmapCell
rowClickHeatmapAxisItem
columnClickHeatmapAxisItem