日历热力图 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 } = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 } = {
'2025-06-10': 134,
'2025-06-09': 27,
'2025-05-30': 388,
'2025-05-24': 400,
'2025-05-19': 5,
'2025-05-11': 610,
'2025-06-13': 241,
'2025-06-18': 238,
'2025-06-13': 190,
'2025-06-03': 10,
'2025-06-29': 111,
'2025-06-27': 120,
'2025-07-03': 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 = {
'2025-06-10': 134,
'2025-06-09': 27,
'2025-05-30': 388,
'2025-05-24': 400,
'2025-05-19': 5,
'2025-05-11': 610,
'2025-06-13': 241,
'2025-06-18': 238,
'2025-06-13': 190,
'2025-06-03': 10,
'2025-06-29': 111,
'2025-06-27': 120,
'2025-07-03': 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 } = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 } = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 = {
'2025-04-21': 1,
'2025-04-26': 2,
'2025-05-01': 3,
'2025-05-02': 4,
'2025-05-04': 5,
'2025-05-11': 6,
'2025-05-15': 7,
'2025-05-20': 8,
'2025-05-26': 9,
'2025-05-31': 10,
'2025-06-11': 11,
'2025-06-24': 12,
'2025-06-30': 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 } |