Skip to content

Heatmap for React

A React heatmap chart that colors a category-by-category grid from light to dark, with a tooltip on every cell. Pass data as readonly ChartRow[]: one object per row.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/heatmap.json

Sessions by hour

Use the arrow keys to move between cells; Home and End jump along a row.

  • Heatmap
  • HeatmapTable
  • ChartTip
  • MissingValue
  • ChartDockHint
  • +12 more
  • +9 more

Activity by hour

24 columns stay readable on a phone: columns shrink to 12px, then the grid scrolls sideways inside the card while the day labels stay put. Focus the grid and use the arrow keys; Home and End jump along a row.

Sessions by hour

Local time, per weekday

Files to copy

12 folders · 26 files · 4 packages
From theme/src/
  • components/charts/{heatmap.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-tip.tsx, chart-utils.ts}
  • components/charts/chart-parts/
  • components/charts/heatmap/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/heatmap.jsonRegistry item: heatmap. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Cohort retention, classed

Five classes instead of a continuous ramp, with values printed inside the cells that fit them: black or white, whichever contrasts more with that cell. Months that haven't happened yet are missing, so they're hatched.

Cohort retention

Share of each signup cohort still active

Heatmap

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneOne object per row, e.g. { day: "Mon", h0: 12, h1: 4, ... }.
index (required)stringNoneKey of the row label in each object.
columns (required)readonly HeatmapColumn[]NoneThe columns, in order: the key of each value and its label.
colorChartColor1Hue of the sequential ramp: a palette slot, 1–8.
domain[number, number]0 (or the minimum, if negative) to the maximumThe [min, max] the color scale spans; values outside it take the end colors.
stepsnumber0Classes on the scale (e.g. 5); 0 = continuous.
showValuesbooleanfalsePrint each value inside its cell when it fits.
cellHeightnumberfollows the cell width (24–40)Sets the row height in px.
minCellWidthnumber12Narrowest a column may get before the grid scrolls sideways inside its own container.
format((n: number) => string)formatCompact in localeFormats a cell value for the tooltips, legend and data table: takes the number, returns the text shown.
localestring"en"Locale of the default number format.
legendbooleantrueShow the scale legend under the grid.
legendLabelReactNodeNoneSets the caption for the legend, e.g. "Sessions".
onCellClick((cell: HeatmapCell) => void)NoneCalled with { row, column, columnLabel, value } on click, Enter or Space: row is the row's index value, column the column's key, columnLabel its display label.
emptyStateReactNodeNoneShown when there is nothing to plot.
labelsPartial<HeatmapLabels>defaultHeatmapLabelsEvery other string, for translation: labels={{ instructions: "…" }}; omitted keys keep the English of defaultHeatmapLabels. All 4 keys of HeatmapLabels are listed in the key table under the props table.
aria-label (required)stringNoneAccessible name of the chart; describe what it shows.
HeatmapLabels keys and English defaults (4)
Pass any key in labels; the rest keep the defaults of defaultHeatmapLabels.
KeyEnglish default
instructionsUse the arrow keys to move between cells; Home and End jump along a row.
noDataNo data
roleDescriptionheatmap
hintTap or hover a cell for its value

HeatmapTable

PropTypeDefaultDescription
data (required)readonly ChartRow[]NoneOne object per row, e.g. { day: "Mon", h0: 12, h1: 4, ... }.
format((n: number) => string)formatCompact in localeFormats a cell value for the tooltips, legend and data table: takes the number, returns the text shown.
localestring"en"Locale of the default number format.
index (required)stringNoneKey of the row label in each object.
columns (required)readonly HeatmapColumn[]NoneThe columns, in order: the key of each value and its label.
indexLabelstringNoneSets the header of the row-label column.
captionstringNoneVisually hidden caption that names the table.
noDataLabelstringNoneText for a cell that has no value.
emptyStateReactNodeNoneShown when there are no rows.

ChartTip props

PropTypeDefaultDescription
title (required)stringNoneSets the heading line of the tooltip.
rows (required)TooltipRow[]NoneOne line per series: a swatch, a label and the formatted value.
anchor (required)ChartTipAnchorNoneThe mark being read, in px relative to the chart box.
bounds (required){ w: number; h: number; }NoneSize of the chart box the tooltip must stay inside.
dockedbooleanfalseThe one-line readout pinned to the top of a narrow chart instead (see ChartTooltip).
orderreadonly ChartTipSide[]NoneSides to try, in order, when placing the tooltip next to the anchor: "right", "left", "below" or "above".
minLeftnumberNoneThe tooltip never starts further left than this many px, so it does not cover the axis.

ChartValueTable props

PropTypeDefaultDescription
columns (required)ChartValueTableColumn[]NoneThe column headers; the first column holds the row headers. Numbers align to the end unless a column says otherwise.
rows (required)ReactNode[][]NoneOne array of cells per row; the first cell is the row header.
captionstringNoneVisually hidden caption naming the table (also names its scroll region).
emptyStateReactNode"No data"Shown when there are no rows.

MissingValue props

PropTypeDefaultDescription
labelstringlabels.noDataText shown and announced for this cell, e.g. "Not applicable".
labelsPartial<Pick<HeatmapLabels, "noData">>defaultHeatmapLabelsOverrides for the built-in English string (noData). Merged over defaultHeatmapLabels. Every other string, for translation: labels={{ noData: "…" }}; omitted keys keep the English of defaultHeatmapLabels. Its only key is listed in the key table under the props table.

SequentialLegend props

PropTypeDefaultDescription
min (required)numberNoneLowest and highest value of the scale.
max (required)numberNoneThe highest value of the scale, at the right end of the ramp.
colorChartColor1The chart color slot (1 to 8) the ramp fades toward.
stepsnumber0Number of classes; 0 draws a continuous ramp.
format((n: number) => string)formatCompactFormats the min and max shown at the ends of the ramp.
labelReactNodeNoneShows a caption before the ramp, e.g. "Sessions".
lowLabelReactNodeNoneText at the low / high end instead of the formatted min / max (e.g. "Less" / "More").
highLabelReactNodeNoneText at the high end instead of the formatted max (for example "More").
markernumber | nullnullPosition (0–1) of the value being read, drawn as a marker on the ramp.
swatchesreadonly string[]NoneExplicit swatch colours (e.g. a calendar's levels) instead of steps of color.
floornumber0.08Share of the hue in the lightest step (nearly the surface). A map raises it so data reads against its no-data land.

Keyboard

KeysAction
TabFocus the grid.
↑/↓/←/→Move between cells.
Home/EndFirst / last cell of the row.
Ctrl + Home/Ctrl + EndTop-left / bottom-right corner.
Page Up/Page DownTop / bottom of the column.
Enter/SpaceActivate the cell being read, when onCellClick is set.
EscapeClear the reading.

Accessibility

  • The grid is a focusable figure named by its aria-label and described by the reading hint (“Use the arrow keys to move between cells; Home and End jump along a row.”).
  • Missing values are hatched, never painted as zero, and every cell's value is in the tooltip and the table view.
  • On narrow screens the grid scrolls sideways inside its own focusable container while the row labels stay put.
  • For a non-visual reading, give ChartCard's table prop a HeatmapTable; the Chart / Table tabs then expose each value as plain table cells.
  • Reduced motion: the cells appear at their final colors and data changes jump instead of morphing.

All exports on this page

  • ChartDockHint — Muted hint in the band a narrow chart reserves for its docked readout, shown while nothing is being read, so the band is never just empty space.
  • ChartTip — The glass tooltip of these charts.
  • ChartValueTable — A plain table in the ChartTable style for charts whose data isn't one row per category.
  • Heatmap — A category × category grid coloured on a single-hue sequential scale.
  • HeatmapTable — Every heatmap value as a table: rows × columns, missing cells read "No data".
  • MissingValue — A table cell without a value: a dash, read as label ("No data", "Not applicable"...).
  • SequentialLegend — The key to a sequential colour scale: a ramp (or classed swatches) between its end values.
  • chartNumberFormat — The default number formatters of the charts, both in one locale (default "en", like formatCompact).
  • defaultHeatmapLabels — The built-in English strings of Heatmap; spread it into your own labels and change what you need.
  • formatPercent — 0.412 → "41.2%" (in "en", matching formatCompact); non-finite ratios read "–".
  • hueMix — A palette hue mixed into a base colour in OKLab.
  • placeChartTip — Where a tooltip of size goes next to the anchor mark inside bounds (all in px, relative to the chart box).
  • scalePosition — Position of value on a [min, max] scale, optionally snapped to steps classes (0–1).
  • sequentialColor — One step of a single-hue sequential ramp: t = 0 sits just off the chart surface, 1 is the full palette hue.

When to use Heatmap

  • Use it as a matrix heatmap for a category-by-category grid, such as traffic by weekday and time of day, error counts by service and hour, or feature usage by plan, where color encodes the value. Use it for a correlation heatmap by putting the variables on both axes and the coefficient in each cell.
  • Use onCellClick to make cells drill into detail: it receives the row, the column key, its label and the value, so a click can filter a table below the grid.
  • Use it when some cells have no data: missing values are hatched, never painted as zero, so a gap in a metric is not mistaken for a real low reading.
  • Skip it when the axes are calendar days over a year, where Calendar heatmap lays them out as weeks, or when the rows are retention cohorts, where Cohort retention adds the average row.
  • Pair it with ChartCard and HeatmapTable for a text view, a Select to switch the metric, and the legend with legendLabel so the color scale is named.

Frequently asked questions

Guides that cover it

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .