日历热力图 Calendar Heatmap
我看你上周github压根没有提交记录,steam倒是天天在线
本组件提供了日历热力图最核心的数据分析和展示功能
基础用法
通过设置mapData的值传入相关数据,使用@hover会在用户悬浮日历格子时执行回调,搭配tipInfo可实现悬浮时文字提示
在@hover方法可接受一个类型为{ date: string, count: number | undefined }的对象,为该位置所对应的数据
mapData 数据格式
请确保传入的数据是{ [key: string]: number }的形式,且其中键的值应为'YYYY-MM-DD'的格式
github提交
展开查看
vue
<template>
<t-calendar-heatmap :mapData="data" @hover="showInfo" title="github提交图" :tipInfo="msg" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
const data: { [key: string]: number } = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
const msg = ref<string>('')
const showInfo = (v: { date: string; count: number | undefined }) => {
msg.value = v['count'] ? `${v['date']}共有${v['count']}次贡献` : `${v['date']}没有贡献`
}
</script>vue
<template>
<t-calendar-heatmap :mapData="data" @hover="showInfo" title="github提交图" :tipInfo="msg" />
</template>
<script setup>
import { ref } from 'vue'
const data = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
const msg = ref('')
const showInfo = v => {
msg.value = v['count'] ? `${v['date']}共有${v['count']}次贡献` : `${v['date']}没有贡献`
}
</script>自定义阈值
你可以根据使用场景自定义变色的阈值,通过设置thresholds属性来使用组件提供的四种渐变颜色
假如传入[k1, k2, k3, k4],那么
| 范围 | (-∞, k1] | (k1, k2] | (k2, k3] | (k3, k4] | (k4, +∞) |
|---|---|---|---|---|---|
| 颜色 | 默认 | 颜色1 | 颜色2 | 颜色3 | 颜色4 |
thresholds 数据格式
请确保传入的数据是长度为4的递增数组
降水量
展开查看
vue
<template>
<t-calendar-heatmap :mapData="data" :thresholds="thresholds" @hover="showInfo" title="降水量" :tipInfo="msg" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
const thresholds: number[] = [100, 200, 300, 400]
const data: { [key: string]: number } = {
'2026-03-09': 134,
'2026-03-08': 27,
'2026-02-26': 388,
'2026-02-20': 400,
'2026-02-15': 5,
'2026-02-07': 610,
'2026-03-12': 241,
'2026-03-17': 238,
'2026-03-12': 190,
'2026-03-02': 10,
'2026-03-28': 111,
'2026-03-26': 120,
'2026-04-01': 130
}
const msg = ref<string>('')
const showInfo = (v: { date: string; count: number | undefined }) => {
msg.value = v['count'] ? `${v['date']}\n降水量:${v['count']}mm` : `${v['date']}\n无降水`
}
</script>vue
<template>
<t-calendar-heatmap :mapData="data" :thresholds="thresholds" @hover="showInfo" title="降水量" :tipInfo="msg" />
</template>
<script setup>
import { ref } from 'vue'
const thresholds = [100, 200, 300, 400]
const data = {
'2026-03-09': 134,
'2026-03-08': 27,
'2026-02-26': 388,
'2026-02-20': 400,
'2026-02-15': 5,
'2026-02-07': 610,
'2026-03-12': 241,
'2026-03-17': 238,
'2026-03-12': 190,
'2026-03-02': 10,
'2026-03-28': 111,
'2026-03-26': 120,
'2026-04-01': 130
}
const msg = ref('')
const showInfo = v => {
msg.value = v['count'] ? `${v['date']}\n降水量:${v['count']}mm` : `${v['date']}\n无降水`
}
</script>触发方式
绑定@hover事件会在悬浮元素时执行回调,除此之外组件还提供在点击时触发的@pick事件
展开查看
vue
<template>
<t-calendar-heatmap :mapData="data" @pick="logInfo" />
</template>
<script setup lang="ts">
const data: { [key: string]: number } = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
const logInfo = (v: { date: string, count: number | undefined }) => {
console.log(v)
}
</script>vue
<template>
<t-calendar-heatmap :mapData="data" @pick="logInfo" />
</template>
<script setup>
const data = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
const logInfo = v => {
console.log(v)
}
</script>颜色与尺寸
设置属性theme以调整主题颜色,col以调整渲染列数
展开查看
vue
<template>
<t-calendar-heatmap :mapData="data" theme="info" :col="45" />
<t-calendar-heatmap :mapData="data" theme="wine" :col="10" />
</template>
<script setup lang="ts">
const data: { [key: string]: number } = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
</script>vue
<template>
<t-calendar-heatmap :mapData="data" theme="info" :col="45" />
<t-calendar-heatmap :mapData="data" theme="wine" :col="10" />
</template>
<script setup>
const data = {
'2026-01-18': 1,
'2026-01-23': 2,
'2026-01-28': 3,
'2026-01-29': 4,
'2026-01-31': 5,
'2026-02-07': 6,
'2026-02-11': 7,
'2026-02-16': 8,
'2026-02-22': 9,
'2026-02-27': 10,
'2026-03-10': 11,
'2026-03-23': 12,
'2026-03-29': 13
}
</script>相关属性
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| mapData | 数据信息,格式见“基本用法” | Object | {} |
| thresholds | 变色阈值,格式见“自定义阈值” | number[] | [1, 3, 5, 7] |
| title | 标题内容 | string | null | null |
| tipInfo | 显示文字提示的内容 | string | null | null |
| theme | 颜色主题 | 'tea' | 'info' | 'wine' | 'tea' |
| col | 渲染列数 | number | 40 |
相关事件
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| hover | 悬浮时触发该事件 | value: { date: string, count: number } |
| pick | 点击时触发该事件 | value: { date: string, count: number } |
