Highcharts 的 tooltip 怎么自定义显示格式?

照南酱~ 阅读 12

我在用 Highcharts 做一个折线图,想在 tooltip 里同时显示日期和数值,但默认只显示数值。试过用 tooltip.formatter,但不知道怎么拿到原始数据里的日期字段。

我的数据是这种格式:

series: [{
    data: [
        { x: Date.UTC(2023, 0, 1), y: 29.9 },
        { x: Date.UTC(2023, 0, 2), y: 71.5 }
    ]
}]

怎么在 tooltip 里把日期格式化成 “2023-01-01: 29.9” 这样的字符串?

我来解答 赞 2 收藏
二维码
手机扫码查看
1 条解答
Des.慧玲
最简单的办法是用 tooltip.formatter 拿到 point.x 和 point.y,然后格式化日期。代码如下:
pre class="pure-highlightjs line-numbers">tooltip: {
formatter: function() {
return Highcharts.dateFormat('%Y-%m-%d', this.x) + ': ' + this.y;
}
}
点赞
2026-03-23 11:04