自定义highcharts工具提示以显示日期时间

Sky*_*zen 25 javascript jquery charts tooltip highcharts

我正在开发一个项目,预计会显示这个图:http://jsfiddle.net/Kc23N/

当我点击一个点(工具提示)时,我想显示一个可以理解的日期,而不是以毫秒为单位的值.

我认为需要改变这段代码:

tooltip: {
      headerFormat: '<b>{series.name}</b><br>',
      pointFormat: '{point.x} date, {point.y} Kg',                        
}
Run Code Online (Sandbox Code Playgroud)

我该怎么办?任何善意的帮助表示赞赏.

谢谢

rsc*_*cnt 54

您可以使用moment.js来获取格式化的值,但Highcharts拥有自己的日期格式化功能,这对于Highcharts来说更加惯用.它可以附加到highcharts构造函数中的tooltip选项,如下所示:

        tooltip: {
            formatter: function() {
                return  '<b>' + this.series.name +'</b><br/>' +
                    Highcharts.dateFormat('%e - %b - %Y',
                                          new Date(this.x))
                + ' date, ' + this.y + ' Kg.';
            }
        }
Run Code Online (Sandbox Code Playgroud)

您可能还希望在xAxis下添加dateTimeLabelFormats对象以及日期所需的选项.

用你的代码做了这个例子


Quy*_* Le 53

您应该在工具提示中使用xDateFormat来自定义格式日期
http://api.highcharts.com/highcharts#tooltip.xDateFormat

sample:
tooltip: {
           xDateFormat: '%Y-%m-%d'
         },
Run Code Online (Sandbox Code Playgroud)

  • 这应该有更多的赞成,效果很好! (4认同)

Wan*_*iao 17

您可以使用{value:%Y-%m-%d}模板过滤器.

举个例子:

headerFormat: '<span style="font-size: 10px">{point.key:%Y-%m-%d}</span><br/>'
Run Code Online (Sandbox Code Playgroud)


Gop*_*h.R 13

您需要使用tooltip格式化工具在工具提示中返回格式化日期.

以下是工具提示格式化程序API供您参考.

要格式化日期部分,您可以使用 Highcharts.dateFormat()

格式是PHP的strftime函数格式的子集.

您还可以参考PHP's strftime更多日期格式.

我设法格式化您的工具提示,如下所示.

  tooltip: {
                formatter: function() {
                    return  '<b>' + this.series.name +'</b><br/>' +
                        Highcharts.dateFormat('%e - %b - %Y',
                                              new Date(this.x))
                    + '  <br/>' + this.y + ' Kg.';
                }
            }
Run Code Online (Sandbox Code Playgroud)


Jus*_*tin 8

我这样做:

headerFormat: '{point.x:%e %b %H:%M}'
Run Code Online (Sandbox Code Playgroud)

例如

"tooltip": {
            outside: true,
            split: false,
            shared: true,
            useHTML: true,
            headerFormat: '{point.x:%e %b %H:%M}',
            pointFormat: '<p style="font-size:12px;margin:0px;padding-top:1px;padding-bottom:1px;color:{series.color};">{series.name} <strong>{point.y}</strong></p>',
            valueDecimals: 2,
            backgroundColor: 'black',
            borderWidth: 0,
            style: {
                width: '150px',
                padding: '0px'
            },
            shadow: false
        },
Run Code Online (Sandbox Code Playgroud)