Apexcharts 热图:悬停时显示每个系列数据的精确 x 值
我正在尝试使用 apexcharts 创建一个热图,显示一年中每周的活动频率(查看 github 贡献热图)。我的目标是显示一个工具提示,每当用户将鼠标悬停在单元格上时,该工具提示就会显示特定日期、工作日和频率值。示例:
------------
| 2022-02-20 |
| ---------- |
| Sunday: 40 |
------------
这是我到目前为止所尝试的:
var options = {
series: [],
chart: {
height: 350,
type: 'heatmap',
},
dataLabels: {
enabled: true
},
xaxis: {
labels: {
show: false
},
tooltip: {
enabled: false,
},
},
tooltip: {
shared: false,
followCursor: true,
intersect: false,
x: {
show: true,
format: 'dd MMM'
},
}
};
这是一个简化的示例系列:
var series = [
{
name: 'Sunday',
data: [{
x: '2022-02-20',
y: 40
}]
},
{
name: 'Monday',
data: [{
x: '2022-02-21',
y: 0
}]
},
{
name: 'Tuesday',
data: [{
x: '2022-02-22',
y: 5
}]
},
{
name: 'Wednesday',
data: [{
x: '2022-02-23',
y: 100
}]
},
{
name: 'Thursday',
data: [{
x: '2022-02-24',
y: 17
}]
},
{
name: 'Friday',
data: [{
x: '2022-02-25',
y: 4
}]
},
{
name: 'Saturday',
data: [{
x: '2022-02-26',
y: 90
}]
},
];
options.series = series;
这里发生的情况是,apexcharts 垂直显示 2022 年 2 月 20 日(星期日)到 2022 年 2 月 26 日(星期六)这一周的热图从下到上(似乎以相反的顺序显示)。如果用户将鼠标悬停在星期日单元格上,它会显示我在开始时提供的确切工具提示,其中包含值 2022-02-20、星期日和 40。但是,当用户将鼠标悬停在星期一至星期六的单元格上时,工具提示会显示正确的工具提示除 x 值之外的值。它仍然显示 2022-02-20。
据我了解,图表将列中的第一个 x 值视为该列的类别。这意味着无论用户将鼠标悬停在星期六、星期五还是任何其他日期,它都会显示星期日的 x 值。如何显示悬停单元格的实际 x 值?
更新
这是热图演示
自 2022 年 3 月 1 日起更新
按照 @Patryk 的建议Laszuk,我添加了
formatter: (value,options)=>{
return options.w.config.series[options.seriesIndex].data[0].x
}
tooltip.x
,但这使得所有水平对齐的单元格具有相同的 x 值。
I'm trying to create a heatmap using apexcharts that shows frequency of activites each week in a year (Check the github contributions heatmap). My goal is to display a tooltip that shows the specific date, week day, and frequency value whenever the user hovers on a cell. Example:
------------
| 2022-02-20 |
| ---------- |
| Sunday: 40 |
------------
This is what I have attempted so far:
var options = {
series: [],
chart: {
height: 350,
type: 'heatmap',
},
dataLabels: {
enabled: true
},
xaxis: {
labels: {
show: false
},
tooltip: {
enabled: false,
},
},
tooltip: {
shared: false,
followCursor: true,
intersect: false,
x: {
show: true,
format: 'dd MMM'
},
}
};
Here is a simplified sample series:
var series = [
{
name: 'Sunday',
data: [{
x: '2022-02-20',
y: 40
}]
},
{
name: 'Monday',
data: [{
x: '2022-02-21',
y: 0
}]
},
{
name: 'Tuesday',
data: [{
x: '2022-02-22',
y: 5
}]
},
{
name: 'Wednesday',
data: [{
x: '2022-02-23',
y: 100
}]
},
{
name: 'Thursday',
data: [{
x: '2022-02-24',
y: 17
}]
},
{
name: 'Friday',
data: [{
x: '2022-02-25',
y: 4
}]
},
{
name: 'Saturday',
data: [{
x: '2022-02-26',
y: 90
}]
},
];
options.series = series;
What happens here is that the apexcharts displays a heatmap for the week of 2022-02-20 (Sunday) to 2022-02-26 (Saturday) vertically from bottom to top (it seems it displays in reverse order). If the user hovers to the Sunday cell it displays the exact tooltip that I provided at the beginning with the values 2022-02-20, Sunday, and 40. However, when the user hovers on cells Monday - Saturday, the tooltip displays the correct values except for the x value. It still displays 2022-02-20.
From what I understand, the chart treats the first x value in a column as the column's category. This means regardless on whether the user hovers to Saturday, or Friday, or any other day it will display the Sunday's x value. What can I do to display the actual x value of the hovered cell?
Update
Here's a demonstration of the heatmap
Update as of 2022-03-01
As suggested by @Patryk Laszuk, I added
formatter: (value,options)=>{
return options.w.config.series[options.seriesIndex].data[0].x
}
on tooltip.x
, however this makes all the cells aligned horizontally have the same x value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
格式化程序
https://apexcharts.com/docs/options/工具提示/#xformatterUse
formatter
https://apexcharts.com/docs/options/tooltip/#xformatter