# heatmap

A table drawn as a heatmap: a matrix of rows and columns, or a calendar of days like a contributions chart.

Each value becomes a rounded tile, or a dot sized by the value, colored on an even scale: one hue from light to dark for amounts, blue and red around a gray when values fall on both sides of zero. Empty cells and zeros stay faint. Row and column labels (weekdays and months in a calendar) and a small legend are drawn in the text color. When a third of it is in view the cells sweep in from the top left corner. The pointer over a cell rings it, dims the rest except its row and column, and shows its value as written.

Module `data`, category Maps and data. Import `@dioramajs/data/heatmap`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/data/src/heatmap.ts)

How full a climbing gym is by weekday and hour, as a heatmap of tiles that sweeps in on scroll.

```html title="index.html"
<div class="hm-frame">
  <p class="hm-title" id="hm-busy">When the climbing gym is busy</p>
  <table class="hm-table" aria-labelledby="hm-busy" data-dio="heatmap">
    <thead>
      <tr><th></th><th scope="col">7:00</th><th scope="col">9:00</th><th scope="col">11:00</th><th scope="col">13:00</th><th scope="col">15:00</th><th scope="col">17:00</th><th scope="col">19:00</th><th scope="col">21:00</th></tr>
    </thead>
    <tbody>
      <tr><th scope="row">Mon</th><td>12%</td><td>18%</td><td>22%</td><td>31%</td><td>28%</td><td>64%</td><td>81%</td><td>46%</td></tr>
      <tr><th scope="row">Tue</th><td>15%</td><td>16%</td><td>20%</td><td>29%</td><td>30%</td><td>70%</td><td>88%</td><td>52%</td></tr>
      <tr><th scope="row">Wed</th><td>11%</td><td>17%</td><td>24%</td><td>33%</td><td>27%</td><td>61%</td><td>79%</td><td>44%</td></tr>
      <tr><th scope="row">Thu</th><td>14%</td><td>19%</td><td>21%</td><td>30%</td><td>32%</td><td>73%</td><td>92%</td><td>58%</td></tr>
      <tr><th scope="row">Fri</th><td>10%</td><td>14%</td><td>19%</td><td>26%</td><td>35%</td><td>55%</td><td>48%</td><td>30%</td></tr>
      <tr><th scope="row">Sat</th><td>8%</td><td>42%</td><td>76%</td><td>85%</td><td>80%</td><td>62%</td><td>40%</td><td>18%</td></tr>
      <tr><th scope="row">Sun</th><td>6%</td><td>38%</td><td>71%</td><td>78%</td><td>69%</td><td>50%</td><td>27%</td><td>9%</td></tr>
    </tbody>
  </table>
</div>
```

## Usage

Put it on: a `<table>`: for the grid, a header row naming the columns and the first cell of each row naming the row; for the calendar, rows of a date and a value (or a `<dl>` of dates and values), sized with CSS.

`start()` registers `heatmap`, so the attribute alone puts it on an element.

```html
<table class="busy" data-dio="heatmap"><caption>Bookings per hour</caption><thead><tr><th></th><th>9</th><th>12</th><th>15</th><th>18</th></tr></thead><tbody><tr><th>Mon</th><td>4</td><td>12</td><td>7</td><td>3</td></tr><tr><th>Tue</th><td>5</td><td>14</td><td>9</td><td>2</td></tr><tr><th>Sat</th><td>18</td><td>26</td><td>21</td><td>9</td></tr></tbody></table>
```

- `data-dio-value`: Exact value of a cell whose text is not a plain number, such as "busy".

- `data-dio-date`: The date of a calendar row whose cell text is not a date and holds no `<time>`.

In page transitions: Fades with the page.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `mode` (rebuilds) | `data-dio-mode` | `'grid' \| 'calendar'` | `'grid'` | grid draws the table as a matrix, for occupancy by weekday and hour, usage by feature and plan or availability; calendar lays dated values out a column per week and a row per weekday, for activity over a year. |
| `style` (rebuilds) | `data-dio-style` | `'tile' \| 'dot'` | `'tile'` | tile fills each cell with its color, for reading patterns; dot draws a circle whose area follows the value, for sparse data where the size of a few big values matters. |
| `motion` (rebuilds) | `data-dio-motion` | `'sweep' \| 'fade'` | `'sweep'` | Entrance: sweep grows the cells in from the top left corner; fade fades them all in at once. |
| `colors` (rebuilds) | `data-dio-colors` | `string` | `''` | Colors of the scale from the lowest value to the highest, separated by spaces; empty uses a colorblind-safe blue, or blue and red around 0. |
| `steps` | `data-dio-steps` | `number` 0 to 9 | `5` | Number of color bins the scale snaps to, as the legend shows them; 0 colors each value exactly. |
| `max` (rebuilds) | `data-dio-max` | `number \| 'auto'` 1 to 1000000 | `'auto'` | Value at the top of the scale, for heatmaps compared side by side; auto takes the largest value. |
| `gap` | `data-dio-gap` | `number` 0 to 12 px | `2` | Gap between cells; 0 lets them touch. |
| `radius` | `data-dio-radius` | `number` 0 to 12 px | `2` | Corner radius of a tile. |
| `labels` | `data-dio-labels` | `boolean` | `true` | Draws the row and column labels, or weekdays and months. |
| `legend` | `data-dio-legend` | `boolean` | `true` | Draws the color scale with its lowest and highest values under the cells. |
| `dim` | `data-dio-dim` | `number` 0 to 1 | `0.35` | How much the hovered cell dims the others, less in its own row and column; 0 keeps them. |
| `duration` | `data-dio-duration` | `number` 0 to 3 s | `0.5` | Time one cell takes to come in; 0 shows the heatmap at once. |
| `stagger` | `data-dio-stagger` | `number` 0 to 0.1 s | `0.02` | Delay from one diagonal of cells to the next in the sweep; 0 brings them in together. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `tile`, `dot`
  - `tile` fills each cell with its color, for reading patterns.
  - `dot` draws a circle whose area follows the value, for sparse data where the size of a few big values matters.
- `motion`: `sweep`, `fade`
  - `sweep` grows the cells in from the top left corner.
  - `fade` fades them all in at once.
- `mode`: `grid`, `calendar`
  - `grid` draws the table as a matrix, for occupancy by weekday and hour, usage by feature and plan or availability.
  - `calendar` lays dated values out a column per week and a row per weekday, for activity over a year.

## Inputs and frames

Reads [`delta`](/docs/api/inputs#input-delta), [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`width`](/docs/api/inputs#input-width), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while the cells come in and for a few frames as a hovered cell fades in or out; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. The heatmap stands complete from the first frame; hover and the tooltip still work.

Runs on every device. A tap on a cell shows its value, a tap elsewhere hides it. Nothing else changes.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { heatmap: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 6.8 KB the first time a page uses it. GPU: Two draw calls: all cells as one instanced call and the labels texture. Memory: One canvas texture the size of the box at up to 2x pixel ratio, plus about 60 bytes per cell.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A year of activity

The `calendar` mode reads rows of a date and a value and lays them out like a contributions chart: a column per week, a row per weekday, month names on top. The week starts on Monday or Sunday as the page’s `lang` says. Days without a row stay empty.

A year of volunteer shifts as a calendar heatmap, busier on summer weekends.

```html title="index.html"
<div class="hm-frame">
  <p class="hm-title" id="hm-year">Volunteer shifts covered in 2026</p>
  <table class="hm-table" aria-labelledby="hm-year" data-dio="heatmap" data-dio-mode="calendar">
    <thead><tr><th scope="col">Day</th><th scope="col">Shifts</th></tr></thead>
    <tbody></tbody>
  </table>
</div>
```

```ts title="script.ts"
// A year of rows, one per day: a real page renders them on the server from its own records.
export default function mount(example: HTMLElement): void {
  const body = example.querySelector('tbody')
  if (!body) return
  let seed = 11
  const random = () => {
    seed = (seed * 16807) % 2147483647
    return seed / 2147483647
  }
  const rows: string[] = []
  for (let d = 0; d < 365; d++) {
    const date = new Date(Date.UTC(2026, 0, 1 + d))
    const iso = date.toISOString().slice(0, 10)
    const weekend = date.getUTCDay() === 0 || date.getUTCDay() === 6
    const summer = Math.sin((d / 365) * Math.PI) ** 2
    const shifts =
      random() < 0.12
        ? 0
        : Math.round((weekend ? 6 : 2) * summer + random() * 3)
    rows.push(
      `<tr><td><time datetime="${iso}">${iso}</time></td><td>${shifts}</td></tr>`,
    )
  }
  body.innerHTML = rows.join('')
}
```

A date can be a `<time datetime>` in the cell, the cell’s text in `2026-03-14` form, or `data-dio-date` on the cell. A `<dl>` of `<dt>` dates and `<dd>` values reads the same way.

### Change on both sides of zero

When values fall below and above zero, the scale turns into red for falls and blue for rises around a gray, with zero in the middle. `steps="0"` colors each value exactly instead of in bins.

```html
<table data-dio="heatmap" data-dio-steps="0">
  <thead>
    <tr>
      <th>Region</th>
      <th>Q1</th>
      <th>Q2</th>
    </tr>
  </thead>
  <tbody>
    <tr><th>North</th><td>+12%</td><td>-4%</td></tr>
    <tr><th>South</th><td>-9%</td><td>+3%</td></tr>
  </tbody>
</table>
```

### Several heatmaps on one scale

Each heatmap takes its top from its own largest value. Give heatmaps that sit side by side the same `max` so their colors compare; `colors` sets the scale from the lowest value to the highest.

## Notes

The table is the source of truth. It stays in the page for screen readers and search engines, hidden only while the heatmap is drawn, and it shows as is without WebGL. A table grows to fit its rows whatever height it is given, so a long one needs `display: block` with a height and `overflow: auto`: the heatmap takes that box, and the plain table scrolls inside it.

Numbers are read the way people write them, as in [`chart`](/docs/effects/chart): separators, a decimal comma, currency, `%` and suffixes. Zeros and empty cells stay faint, so gaps read as gaps. The default colors are one blue from light to dark that tells values apart with every common color vision deficiency; on a dark page the scale runs from dark to light. The legend under the cells names the lowest and the highest value.

It draws only while the cells come in and for a few frames as a hovered cell fades in or out. Under reduced motion the heatmap stands complete from the first frame. Style the tooltip with `::part(tooltip)`.

## Related

- [chart](/docs/effects/chart.md): A plain HTML table or definition list drawn as an animated chart that grows in on scroll.
- [globe](/docs/effects/globe.md): An interactive globe of land dots with markers and arcs for a list of places.
- [graph](/docs/effects/graph.md): A list or a table of links drawn as a network graph: partners, integrations, teams, knowledge maps.
- [map](/docs/effects/map.md): A flat map of the world or a region with markers for a list of places and arcs between them.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [bend](/docs/effects/bend.md): Scroll speed bends the image like paper and splits its color channels.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [clouds](/docs/effects/clouds.md): A sky of soft layered clouds for the time of day, daylight, a sunset or a moonlit night, drifting in the wind and parting on scroll.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
