【问题标题】:Highcharts changing tooltip datetime with formatterHighcharts 使用格式化程序更改工具提示日期时间
【发布时间】:2019-02-07 08:55:29
【问题描述】:

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

问题在于,使用 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
},

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

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

【问题讨论】:

    标签: javascript highcharts tooltip momentjs


    【解决方案1】:

    我从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,
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      相关资源
      最近更新 更多