javascript - Highcharts 使用格式化程序更改工具提示日期时间

标签 javascript highcharts tooltip momentjs

我有一个如下图所示的图表。默认情况下,每个工具提示值都位于其自己的工具提示“气泡”中,日期时间位于 Y 轴底部(悬停在 X 标签顶部)。

default tooltip

问题在于,更改日期时间格式以匹配区域设置对于 Highcharts 来说不是动态的。我知道我可以让用户更改 dateTimeLabelFormats 以匹配他们的区域设置,但我希望利用 moment.js 及其内置的区域设置格式。

我只需要更改这些图表中的日期时间,无需更改其他内容。

当我尝试下面的代码时,它为我提供了我正在寻找的区域设置杠杆,但工具提示被合并到 1 个框中,并且没有与默认相同的感觉。

tooltip: {
    enabled: true,
    dateTimeLabelFormats: {
        //minute: '%e %b %H:%M',
        hour: '%e %b %H:%M'
    },
    // For locale control with moment. Combined momentjs with this answer https://stackoverflow.com/a/33939715/1177153
    formatter: function() {
        var toolTipTxt = '<b>'+ moment.unix(this.x / 1000).format("LLL") +'</b>';  
          $.each(this.points, function(i, point) {
            toolTipTxt += '<br/><span style="color:'+ point.series.color +'">  ' + point.series.name + ': ' + point.y+'</span>';
        });
        return toolTipTxt;
    },
    crosshairs: true,
    shared: true
},

moment.js locale formatting

有没有办法用格式化程序模拟默认工具提示?值和时间戳的单独“气泡”悬停在底部?

是否可以以某种方式将 xDateFormat 与 moment.js 一起使用?

最佳答案

我找到了一个有效的解决方案,直接从Highcharts API documentation for tooltip split

jsfiddle example from the API documentation拥有我需要的一切(除了 moment.js)。

我今天肯定忽略了这一点 100 次。这是对我有用的最终代码以及结果的屏幕截图。

现在,工具提示的标题将位于正确的区域设置中,而无需用户更改任何代码。

tooltip: {
    enabled: true,
    // For locale control with moment.js - https://api.highcharts.com/highcharts/tooltip.split
    formatter: function () {
        // The first returned item is the header, subsequent items are the points
        return [moment.unix( this.x / 1000).format("LLL")].concat(
            this.points.map(function (point) {
                return "<span style='color:" + point.series.color + "'>\u25CF</span> " + point.series.name + ': ' + point.y;
            })
        );
    },
    split: true,
},

enter image description here

关于javascript - Highcharts 使用格式化程序更改工具提示日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564405/

相关文章:

javascript - 在我的 Chrome 网上应用店应用程序中禁用检查元素/javascript 控制台?

javascript - 如何在 Highcharts 中添加自定义图标作为标记

javascript - 图表的宽度影响我在 Highcharts 中的第二个 x 轴标签

c# - 为什么我的工具提示不显示它的提示?

java - 如何在 Java 中获取动态 RadioButton 工具提示?

wpf - 工具提示与弹出窗口(WPF控件)

javascript - 当元素在手机上可见时,Ionic 2 触发动画

javascript - 如何使用 Jquery 动态弹出数据

javascript - jQuery:尝试刷新或关闭浏览器时提示用户

javascript - 在 HIGHCHARTS 中无法设置仪表中的最大点