Heatmap
A calendar heatmap of daily values: each column is a week, each row a weekday, and darker colors represent larger values. Use it for daily commits, logins, inspections, or alerts.
Import
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum, HeatmapCell } from '@nscode/handy-ui'
Basic usage
data is an array of { date, value }. Without an explicit range, the calendar shows the 52 weeks up to today and extends backward to the start of that week (Monday), producing 53 columns. Positions after today are blank.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
const data: HeatmapDatum[] = [
{ date: '2026-11-15', value: 2 },
{ date: '2026-11-16', value: 5 },
{ date: '2026-11-17', value: 3 }
]
</script>
<template>
<HuiHeatmap :data="data" />
</template>
Data and dates
dateaccepts'YYYY-MM-DD'(also'YYYY/MM/DD'or a time-bearing string such as'2026-11-17 08:30'), a millisecond timestamp, or aDate.- Dates use the local time zone.
'2026-11-17'means the 17th and does not shift to the 16th in western time zones due to UTC interpretation. Timestamps between midnight and 08:00 in Beijing also remain on their local day instead of being truncated to the previous UTC date. ISO timestamps with a time zone, such as'2026-11-16T16:30:00Z', are converted to local time before extracting the date. - Multiple entries on one day are summed, so raw detail records can be passed directly.
nullvalues are excluded from the sum. A day with onlynullentries displays No data. - Unrecognized dates, such as
'abc','2026-02-30', or a year/month-only'2026-11', are ignored with a console warning indicating the count. Data outside the displayed range is ignored and does not affect the maximum.
Hover over November 17, 10, 3, and 18 to compare:
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
const data: HeatmapDatum[] = [
// All three formats represent 2026-11-17, whose total is 6
{ date: '2026-11-17', value: 1 },
{ date: new Date(2026, 10, 17, 9, 30), value: 2 },
{ date: new Date(2026, 10, 17, 21, 0).getTime(), value: 3 },
// Slash-separated and time-bearing formats are accepted
{ date: '2026/11/10', value: 2 },
{ date: '2026-11-03 08:30', value: 4 },
// This day contains only null and displays No data
{ date: '2026-11-18', value: null }
]
</script>
<template>
<HuiHeatmap :data="data" start="2026-10-01" end="2026-11-30" />
</template>
A full year
year displays January 1 through December 31 of that year. When supplied, start and end have no effect.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: every day of 2025, generated with a fixed seed; about 30% of days have no data
let seed = 11
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const data: HeatmapDatum[] = Array.from({ length: 365 }, (_, i) => ({
date: new Date(2025, 0, 1 + i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" :year="2025" />
</template>
Date ranges
start and end accept the same formats as date. With only end, the range begins 52 weeks earlier. If start is later than end, they are swapped with a warning. Positions before start in the first week and after end in the last week remain blank.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: each day from 2026-03-04 to 2026-06-30, generated with a fixed seed
let seed = 5
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const data: HeatmapDatum[] = Array.from({ length: 119 }, (_, i) => ({
date: new Date(2026, 2, 4 + i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" start="2026-03-04" end="2026-06-30" />
</template>
Color levels
There are five color levels:
| Level | Condition | Default color (primary) |
|---|---|---|
| 0 | No data, null, zero, or negative | Fill color --hui-fill-color |
| 1 | 0 < value ≤ max × 1/4 | Type color and background mixed 3 : 7 |
| 2 | max × 1/4 < value ≤ max × 2/4 | Type color and background mixed 5 : 5 |
| 3 | max × 2/4 < value ≤ max × 3/4 | Type color and background mixed 7 : 3 |
| 4 | value > max × 3/4 (including values above max) | Primary color --hui-color-primary |
maxdefaults to the largest daily total in the displayed range. Set it manually if a few unusually large values make the other cells too pale.- Colors are mixed from theme variables and adapt to palette and dark-theme changes. In dark themes, level 0 is a dark gray slightly brighter than the background, while levels 1–4 become progressively brighter.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" :max="10" />
</template>
These heatmaps use identical data: today is 100, and all other values are at most 12. The first omits max, so 100 sets the scale and every other positive value falls into level 1. The second uses :max="12" to spread the levels. Today still uses the strongest level 4 color because it exceeds max:
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 140 days ending today, generated with a fixed seed; today has an unusually large value
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 140 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: i === 0 ? 100 : random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 139)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<span class="caption">Without max (scale: 100)</span>
<HuiHeatmap :data="data" :start="start" />
<span class="caption">:max="12"</span>
<HuiHeatmap :data="data" :start="start" :max="12" />
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Color types
type selects a semantic theme color: primary (default), success, warning, danger, info, or help. Choose success for a green heatmap.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" type="success" />
</template>
Compare all six types over the last 20 weeks:
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum, HeatmapType } from '@nscode/handy-ui'
// Demo data: the preceding 140 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 140 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 139)
const types: HeatmapType[] = ['primary', 'success', 'warning', 'danger', 'info', 'help']
</script>
<template>
<div class="grid">
<HuiSpace v-for="type in types" :key="type" direction="vertical" align="start">
<span class="caption">{{ type }}</span>
<HuiHeatmap :data="data" :start="start" :type="type" size="small" />
</HuiSpace>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
gap: var(--hui-spacing-4);
}
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Custom colors
colors provides five colors in order from level 0 to 4. Missing entries use the colors from type; a count other than five produces a console warning. Fixed colors do not adapt to dark themes. Switch palettes yourself or use CSS variables such as 'var(--my-level-1)'.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
const colors = ['#ebedf0', '#9be9a8', '#40c463', '#30a14e', '#216e39']
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" :colors="colors" />
</template>
Starting the week on Sunday
first-day-of-week defaults to 1 (Monday in the first row). Use 0 or 7 for Sunday, or 2–6 for another weekday. Weekday labels always appear on the Monday, Wednesday, and Friday rows. This follows HuiCalendar: 0 and 7 both mean Sunday, and out-of-range values wrap modulo 7.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" :first-day-of-week="0" />
</template>
The same last-12-week dataset starts on Monday, Sunday, and Wednesday below. Compare the weekday label positions:
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 84 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 84 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 83)
</script>
<template>
<HuiSpace wrap size="large" align="start">
<HuiSpace direction="vertical" align="start">
<span class="caption">1: Monday (default)</span>
<HuiHeatmap :data="data" :start="start" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">0: Sunday</span>
<HuiHeatmap :data="data" :start="start" :first-day-of-week="0" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">3: Wednesday</span>
<HuiHeatmap :data="data" :start="start" :first-day-of-week="3" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Sizes
size accepts large, default, and small:
| Size | Cell | Cell gap | Label and legend font size |
|---|---|---|---|
| large | 14px | 4px | 14px |
| default | 11px | 3px | 12px |
| small | 9px | 2px | 12px |
When omitted, size follows HuiConfigProvider, falling back to default. A full year with 53 columns is approximately 980 / 770 / 610px wide respectively.
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiSpace direction="vertical" fill>
<span class="caption">large</span>
<HuiHeatmap :data="data" size="large" />
<span class="caption">default</span>
<HuiHeatmap :data="data" />
<span class="caption">small</span>
<HuiHeatmap :data="data" size="small" />
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Cell spacing
x-gap and y-gap set horizontal and vertical spacing. Numbers are pixels; CSS lengths such as '0.25em' are also accepted. Omitted values follow the size table above. Either prop can be set independently.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" :x-gap="6" :y-gap="6" />
</template>
Compare several settings over the last 12 weeks:
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 84 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 84 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 83)
</script>
<template>
<HuiSpace wrap size="large" align="start">
<HuiSpace direction="vertical" align="start">
<span class="caption">Omitted (3px)</span>
<HuiHeatmap :data="data" :start="start" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">:x-gap="0" :y-gap="0"</span>
<HuiHeatmap :data="data" :start="start" :x-gap="0" :y-gap="0" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">:x-gap="8"</span>
<HuiHeatmap :data="data" :start="start" :x-gap="8" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">y-gap="0.5em"</span>
<HuiHeatmap :data="data" :start="start" y-gap="0.5em" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Tooltips
Hovering or focusing a cell displays its date and value, such as “2026-11-17: 3”. A missing value displays “2026-11-17: No data”.
unitchanges the unit, such asunit="hours"; an empty string displays only the value.- The
tooltipslot customizes content and receives{ date, value, level }. :tooltip="false"hides tooltips.
All cells share one tooltip that follows the active cell, so even 371 cells use only one tooltip. It ignores pointer events to avoid blocking cells. Links and buttons inside it are therefore not clickable; use cellClick for interactions.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
</script>
<template>
<HuiHeatmap :data="data" unit="hours" />
<HuiHeatmap :data="data" style="margin-top: 16px">
<template #tooltip="{ date, value }">
<div>{{ date }}</div>
<div>{{ value === null ? 'No commits' : `${value} commits` }}</div>
</template>
</HuiHeatmap>
</template>
Compare unit="", a tooltip slot that includes level, and disabled tooltips over the last 12 weeks:
<script setup lang="ts">
import { HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 84 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 84 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 83)
</script>
<template>
<HuiSpace wrap size="large" align="start">
<HuiSpace direction="vertical" align="start">
<span class="caption">unit="": values only</span>
<HuiHeatmap :data="data" :start="start" unit="" />
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">Show level in the slot</span>
<HuiHeatmap :data="data" :start="start" type="success">
<template #tooltip="{ date, value, level }">
{{ date }}:{{ value ?? 'No data' }}(level {{ level }})
</template>
</HuiHeatmap>
</HuiSpace>
<HuiSpace direction="vertical" align="start">
<span class="caption">:tooltip="false"</span>
<HuiHeatmap :data="data" :start="start" :tooltip="false" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Clicking cells
Clicking a cell, or pressing Enter/Space while it is focused, emits cellClick with { date, value, level } and the native event. date is 'YYYY-MM-DD', and missing values are null. Blank positions outside the range and labels do not emit it; neither do cells while loading.
<script setup lang="ts">
import { HuiHeatmap, HuiMessage } from '@nscode/handy-ui'
import type { HeatmapCell, HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const onCellClick = (cell: HeatmapCell) => {
HuiMessage(`${cell.date}:${cell.value ?? 'No data'}(${cell.level} level)`)
}
</script>
<template>
<HuiHeatmap :data="data" @cell-click="onCellClick" />
</template>
Labels, legend, and footer
show-week-labels,show-month-labels, andshow-legendcontrol the weekday labels on the left, month labels above, and Less … More legend at the bottom right. All are enabled by default.- Month labels are omitted when a month occupies only one or two columns (fewer than three weeks at a range boundary), preventing crowded labels.
less-textandmore-textoverride the legend labels, such asless-text="Less" more-text="More".- The
footerslot appears at the bottom left, opposite the legend.
<script setup lang="ts">
import { HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const total = data.reduce((sum, item) => sum + (item.value ?? 0), 0)
</script>
<template>
<HuiHeatmap :data="data" less-text="Less" more-text="More">
<template #footer>Past year total: {{ total }} contributions</template>
</HuiHeatmap>
</template>
Use the checkboxes to toggle these three sections over the last 20 weeks:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiHeatmap, HuiSpace } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 140 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 140 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 139)
const showWeekLabels = ref(true)
const showMonthLabels = ref(true)
const showLegend = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiCheckbox v-model="showWeekLabels">Weekday labels (show-week-labels)</HuiCheckbox>
<HuiCheckbox v-model="showMonthLabels">Month labels (show-month-labels)</HuiCheckbox>
<HuiCheckbox v-model="showLegend">Legend (show-legend)</HuiCheckbox>
</HuiSpace>
<HuiHeatmap
:data="data"
:start="start"
:show-week-labels="showWeekLabels"
:show-month-labels="showMonthLabels"
:show-legend="showLegend"
/>
</HuiSpace>
</template>
Loading
When loading is true, cells become gray and pulse together (unless reduced motion is enabled). Tooltips and click events are disabled. The range is independent of data, so cell positions and heatmap dimensions remain stable before and after loading. Toggle loading with the checkbox:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiHeatmap } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
// Demo data: the preceding 371 days ending today, generated with a fixed seed; about 30% of days have no data
let seed = 7
const random = () => {
seed = (seed * 16807) % 2147483647
return seed / 2147483647
}
const today = new Date()
const data: HeatmapDatum[] = Array.from({ length: 371 }, (_, i) => ({
date: new Date(today.getFullYear(), today.getMonth(), today.getDate() - i),
value: random() < 0.3 ? null : Math.round(random() * 12)
}))
const loading = ref(true)
</script>
<template>
<HuiCheckbox v-model="loading">Loading</HuiCheckbox>
<HuiHeatmap :data="data" :loading="loading" style="margin-top: 12px" />
</template>
Keyboard interaction
Cells use roving tabindex: the heatmap occupies one Tab stop. Initial focus goes to today, or the first date if today is outside the range; subsequent entry returns to the last focused cell.
- Up / Down: previous / next day (Down on Sunday moves to Monday of the next week).
- Left / Right: the same weekday in the previous / next week.
- Home / End: the first / last cell in the current weekday row.
- Ctrl + Home / End (⌘ + Home / End on Mac): the first / last date in the range.
- Enter / Space: emit
cellClick. - Esc: dismiss the tooltip. When a tooltip is open, the default action is prevented so an enclosing dialog is not also closed.
Movement stops at range boundaries. Focusing a cell shows its tooltip.
Ancestor variables and explicit props
Public variables are read-only inputs. Component root, size, and type classes set private defaults only. Override public variables on the component or any ancestor. Nested heatmaps inherit public configuration while each instance resets its own private defaults and prop overrides.
| Variable | Default | Description |
|---|---|---|
--hui-heatmap-cell-size | large/default/small: 14/11/9px | Cell side length |
--hui-heatmap-font-size | large/default/small: 14/12/12px | Label and legend font size |
--hui-heatmap-x-gap, --hui-heatmap-y-gap | large/default/small: 4/3/2px | Horizontal and vertical spacing |
--hui-heatmap-color-0 | var(--hui-fill-color) | Color for missing data or level 0 |
--hui-heatmap-color-1 through --hui-heatmap-color-4 | Type color mixed at 30%/50%/70%/100% | Level 1–4 colors |
Explicit colors, x-gap, and y-gap take precedence over public variables, including inline ones. They override only when supplied; zero spacing is valid. Removing props or emptying a color entry restores public configuration, then size/type defaults if no public value exists. Do not use private variables as application override APIs. Compare these paths with the button below:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiHeatmap, HuiButton } from '@nscode/handy-ui'
import type { HeatmapDatum } from '@nscode/handy-ui'
const variableProps = ref(true)
const variableData: HeatmapDatum[] = [
{ date: '2024-01-01', value: 1 },
{ date: '2024-01-02', value: 2 },
{ date: '2024-01-03', value: 3 },
{ date: '2024-01-04', value: 4 }
]
const variableColors = [
'var(--hui-fill-color)',
'var(--hui-color-success-light-7)',
'var(--hui-color-success-light-5)',
'var(--hui-color-success-light-3)',
'var(--hui-color-success)'
]
</script>
<template>
<div
class="heatmap-variables-demo"
style="--hui-heatmap-cell-size: 20px; --hui-heatmap-font-size: 14px; --hui-heatmap-x-gap: 8px; --hui-heatmap-y-gap: 6px; --hui-heatmap-color-0: var(--hui-fill-color); --hui-heatmap-color-1: var(--hui-color-danger-light-7); --hui-heatmap-color-2: var(--hui-color-danger-light-5); --hui-heatmap-color-3: var(--hui-color-danger-light-3); --hui-heatmap-color-4: var(--hui-color-danger)"
>
<HuiButton class="heatmap-variable-toggle" @click="variableProps = !variableProps">{{
variableProps ? 'Remove explicit props' : 'Restore explicit props'
}}</HuiButton>
<p>Small preset: inherits cell size, font size, spacing, and colors from the ancestor.</p>
<HuiHeatmap
class="heatmap-inherited"
:data="variableData"
start="2024-01-01"
end="2024-01-14"
:max="4"
size="small"
:tooltip="false"
/>
<p>
Large preset:
{{
variableProps
? 'Explicit zero spacing and green colors take precedence'
: 'Props removed; ancestor configuration restored'
}}.
</p>
<HuiHeatmap
class="heatmap-props"
:data="variableData"
start="2024-01-01"
end="2024-01-14"
:max="4"
size="large"
:tooltip="false"
:x-gap="variableProps ? 0 : undefined"
:y-gap="variableProps ? 2 : undefined"
:colors="variableProps ? variableColors : undefined"
/>
</div>
</template>
Props
Weekday and month labels, legends, tooltips, and accessible text follow the current language. Explicit less-text, more-text, and unit props take precedence, including empty strings. Chinese defaults to a count unit; English defaults to no unit. Data and date formats remain unchanged.
| Prop | Type | Default | Description |
|---|---|---|---|
| data | HeatmapDatum[] | [] | Data as { date, value }[]; values on the same day are summed |
| year | number | - | Display January 1–December 31 of a year; overrides start and end |
| start | string | number | Date | - | First date; defaults to 52 weeks before end, extended backward to the first day of the week |
| end | string | number | Date | Today | Last date |
| first-day-of-week | number | 1 | First weekday: 1 is Monday, 0 or 7 is Sunday |
| max | number | - | Maximum scale value for level 4; defaults to the largest displayed daily total. Larger values still use level 4 |
| type | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help' | 'primary' | Semantic theme color type |
| colors | string[] | - | Five custom colors from level 0 to 4; missing levels use type colors |
| show-week-labels | boolean | true | Show weekday labels on the left |
| show-month-labels | boolean | true | Show month labels above |
| show-legend | boolean | true | Show the Less … More legend at the bottom right |
| less-text | string | Locale-dependent (English: Less) | Left legend label |
| more-text | string | Locale-dependent (English: More) | Right legend label |
| tooltip | boolean | true | Show tooltips on hover/focus |
| unit | string | Locale-dependent (English: empty) | Unit after values in tooltips and accessible names; empty hides it |
| loading | boolean | false | Loading state |
| size | 'large' | 'default' | 'small' | - | Size; follows HuiConfigProvider when omitted, falling back to 'default' |
| x-gap | number | string | - | Horizontal cell spacing; numbers are pixels. Defaults to 4 / 3 / 2px by size |
| y-gap | number | string | - | Vertical cell spacing; same format as x-gap |
HeatmapDatum
| Prop | Type | Default | Description |
|---|---|---|---|
| date | string | number | Date | Date interpreted in the local time zone | |
| value | number | null | Numeric value or null for no data; same-day entries are summed |
HeatmapCell
Arguments for the cellClick event and tooltip slot.
| Prop | Type | Default | Description |
|---|---|---|---|
| date | string | Local date formatted as 'YYYY-MM-DD' | |
| value | number | null | Daily total, or null when there is no data | |
| level | 0 | 1 | 2 | 3 | 4 | Color level |
Events
Event declarations and script listeners use camelCase cellClick; templates may use @cell-click. Arguments and the native event remain unchanged.
| Event | Parameters | Description |
|---|---|---|
| cellClick | (cell: HeatmapCell, event: MouseEvent | KeyboardEvent) | Click or press Enter/Space on a focused cell; not emitted for out-of-range positions or while loading |
Slots
| Slot | Scope | Description |
|---|---|---|
| tooltip | { date, value, level } | Custom tooltip content, defaulting to a localized date and value |
| footer | - | Bottom-left content opposite the legend, such as an annual total |
Methods
No public methods are exposed.
Types
import type {
HeatmapProps,
HeatmapEmits,
HeatmapSlots,
HeatmapSize,
HeatmapType,
HeatmapDateInput,
HeatmapDatum,
HeatmapCell,
HeatmapLevel
} from '@nscode/handy-ui'
type HeatmapSize = 'large' | 'default' | 'small'
type HeatmapType = 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help'
type HeatmapDateInput = string | number | Date
type HeatmapLevel = 0 | 1 | 2 | 3 | 4
interface HeatmapDatum {
date: HeatmapDateInput
value: number | null
}
interface HeatmapCell {
date: string
value: number | null
level: HeatmapLevel
}
interface HeatmapProps {
data?: HeatmapDatum[]
year?: number
start?: HeatmapDateInput
end?: HeatmapDateInput
firstDayOfWeek?: number
max?: number
type?: HeatmapType
colors?: string[]
showWeekLabels?: boolean
showMonthLabels?: boolean
showLegend?: boolean
lessText?: string
moreText?: string
tooltip?: boolean
unit?: string
loading?: boolean
size?: HeatmapSize
xGap?: number | string
yGap?: number | string
}
interface HeatmapSlots {
tooltip?: (props: HeatmapCell) => unknown
footer?: () => unknown
}
interface HeatmapEmits {
cellClick: [cell: HeatmapCell, event: MouseEvent | KeyboardEvent]
}