如何在自定义工具提示格式化程序中自动格式化日期?

时间:2013-10-17 23:18:23

标签: highcharts

Highcharts在x轴和工具提示中自动格式化日期非常出色。不幸的是我需要一个自定义函数来格式化我的y值,/plotOptions/line/tooltip/pointFormat只接受格式字符串而不是函数,所以我必须设置/tooltip/formatter。如何保持格式化日期(例如10月13日或10月10日),如x轴所示?我似乎无法从那里访问point.key,只有原始millis值。

http://jsfiddle.net/9Fke4/

4 个答案:

答案 0 :(得分:1)

您可以使用dateFormat()

    tooltip: {
        formatter: function() {
           return '<b>' + Highcharts.dateFormat('%b\' %d',this.x) + ':</b> ' + this.y;
        }
    },

http://jsfiddle.net/9Fke4/1/

答案 1 :(得分:1)

FWIW,这在Github

的Highcharts问题中得到了解答
formatter: function() {
                var tooltip = this.series.chart.tooltip,
                    item = this.point.getLabelConfig(),
                    header = this.series.chart.tooltip.tooltipFooterHeaderFormatter(item);

                return header + this.y;
            }

相应的小提琴:

http://jsfiddle.net/9Fke4/12/

如果您使用的是共享系列:

tooltip: {
        formatter: function (tooltip) {
          var header,
              s = [];

          $.each(this.points, function(i, point) {
            if(header == null) {
              var config = point.point.getLabelConfig();
              header = tooltip.tooltipFooterHeaderFormatter(config);
            }
            s.push(point.y);
          });

          return header + s.reverse().join('<br>');
        },
        shared: true
      }

答案 2 :(得分:0)

使用

将原始毫秒转换为日期对象
var currentDate = new Date (tome in milliseconds)

在你定义的tootip / formatter函数中。

这将使您能够很好地控制日期。您可以使用currentDate.getMonth(),getDate(),getDay()等方法从该日期获取所需的信息。

使用上述信息构建一个字符串并返回。

我希望这会对你有所帮助。

答案 3 :(得分:0)

我最终得到的解决方案是预先格式化y值并将它们存储为系列数据的一部分;然后可以从工具提示headerFormatpointFormat引用预先格式化的值,其中它们可以与{point.key}一起使用,其中包含自动格式化日期(并且不可用)提供自定义工具提示时formatter):

http://jsfiddle.net/uG3sv/