Heatmap 热力图
用二维分类网格和连续色阶展示每个交叉位置的数值强弱。
何时使用
在线体验
Gallery 展示一周 24 小时访问量热力图,单元格支持标签和悬停提示。
基本用法
X() 和 Y() 指定两个分类字段,Value() 指定映射到连续色阶的数值字段。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
var heatRows = []chart.Row{
{"hour": "09:00", "day": "周一", "count": 32},
{"hour": "12:00", "day": "周一", "count": 86},
{"hour": "09:00", "day": "周二", "count": 48},
{"hour": "12:00", "day": "周二", "count": 92},
}
func page(c *ui.Context) {
chart.Heatmap(c, heatRows).X("hour").Y("day").Value("count").Show()
}常见用法
单元格标签和尺寸
Label() 在单元格足够宽高时显示格式化数值,Height() 给行列网格分配稳定高度。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func labeledHeatmap(c *ui.Context) {
rows := []chart.Row{{"hour": "09:00", "day": "周一", "count": 32}, {"hour": "12:00", "day": "周一", "count": 86}}
chart.Heatmap(c, rows).X("hour").Y("day").Value("count").Label().Format(chart.Thousands(0)).Height(340).Show()
}自定义连续色阶
Ramp() 接收从低到高的 chart.Color;使用单色连续阶梯表达量级,不要把连续值映射成彩虹色。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
"github.com/tangoui/tango/ui/theme"
)
func rampHeatmap(c *ui.Context) {
rows := []chart.Row{{"x": "A", "y": "一", "v": 10}, {"x": "B", "y": "一", "v": 80}}
chart.Heatmap(c, rows).X("x").Y("y").Value("v").Ramp(
theme.Hex("#e6f4ff"), theme.Hex("#91caff"), theme.Hex("#1677ff"),
).Show()
}交互和空值
悬停单元格会显示分类组合和值;没有数值的交叉位置保持空白,加载阶段可用 Loading(true),静态导出可用 NoTooltip()。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func heatmapState(c *ui.Context) {
rows := []chart.Row{{"x": "A", "y": "一", "v": 10}, {"x": "B", "y": "一", "v": 20}}
chart.Heatmap(c, rows).X("x").Y("y").Value("v").A11yLabel("按分类统计的热力图").Loading(false).Show()
}主题与样式
Heatmap 默认使用当前 chart style 的 Sequential 单色连续色阶和数值图例。可用 Ramp() 只为单图替换色阶,Colors() 不替代连续色阶;Ant Design、Element Plus 和 shadcn 的背景、文字和悬停边框会随主题适配。
注意事项
X()/Y()的分类顺序来自数据首次出现的顺序;需要固定星期或小时顺序时,先按期望顺序构造数据。- 缺失交叉位置不是零值,不要用零填充来掩盖“没有数据”和“数值为零”的差异。
- 连续色阶必须同时配数值图例,颜色深浅之外还应提供标签或表格;大量网格可先聚合,避免标签拥挤。
- 即时模式下替换外部数据切片来更新图表;用
Key()区分不同指标热力图,RTL 需要反转绘图区时再用Mirror()。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
chart.Heatmap
func Heatmap(c *ui.Context, data []Row) *HeatmapWHeatmap starts a heatmap: X and Y are category fields, Value the numeric field mapped onto the sequential palette.
chart.Heatmap(c, rows).X("hour").Y("weekday").Value("count").Show()
返回 *HeatmapW,可链式调用的方法见 HeatmapW 方法。
HeatmapW 方法
chart.HeatmapW:HeatmapW builds a heatmap over two category fields.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[HeatmapW]) | A11yLabel(s string) *HeatmapW |
Animate | Animate enables (default) or disables entry and update transitions. (来自嵌入的 common[HeatmapW]) | Animate(on bool) *HeatmapW |
Colors | Colors overrides the palette. (来自嵌入的 common[HeatmapW]) | Colors(cs ...theme.Color) *HeatmapW |
Format | Format sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[HeatmapW]) | Format(f Formatter) *HeatmapW |
Height | Height sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[HeatmapW]) | Height(h float32) *HeatmapW |
Key | Key gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[HeatmapW]) | Key(k any) *HeatmapW |
Label | Label shows data labels on the marks. (来自嵌入的 common[HeatmapW]) | Label() *HeatmapW |
Layout | Layout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[HeatmapW]) | Layout(o ...layout.Option) *HeatmapW |
Legend | Legend places (or with LegendNone hides) the legend. (来自嵌入的 common[HeatmapW]) | Legend(p LegendPos) *HeatmapW |
Loading | Loading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[HeatmapW]) | Loading(on bool) *HeatmapW |
Mirror | Mirror draws the plot right to left: the x axis runs from the right, the value axis sits on the right, bars, lines, areas and points are mirrored (so categories read in reverse order), and pie, donut, radar and gauge start at the mirrored angle and sweep counter-clockwise. (来自嵌入的 common[HeatmapW]) | Mirror() *HeatmapW |
NoTooltip | NoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[HeatmapW]) | NoTooltip() *HeatmapW |
Ramp | Ramp overrides the sequential palette (low → high). | Ramp(cs ...Color) *HeatmapW |
Show | Show builds the chart. | Show() View |
Style | Style tweaks the resolved chart style for this chart only. (来自嵌入的 common[HeatmapW]) | Style(fn func(*Style)) *HeatmapW |
Value | Value sets the numeric field mapped to color. | Value(field string) *HeatmapW |
X | X sets the column category field. | X(field string) *HeatmapW |
Y | Y sets the row category field (first category on top). | Y(field string) *HeatmapW |