customize highcharts tooltip to show datetime

2019-01-17 15:21发布

I'm developing a project that is expected to show this graph: http://jsfiddle.net/Kc23N/

When I click a point (tooltip) I want to show a date understandable, not the value in milliseconds.

I think needs to change this piece of code:

tooltip: {
      headerFormat: '<b>{series.name}</b><br>',
      pointFormat: '{point.x} date, {point.y} Kg',                        
}

how do I do? any kind help is appreciated.

Thanks

4条回答
相关推荐>>
2楼-- · 2019-01-17 15:56

You should use xDateFormat in tooltip for customizing your format date
http://api.highcharts.com/highcharts#tooltip.xDateFormat

sample:
tooltip: {
           xDateFormat: '%Y-%m-%d'
         },
查看更多
Anthone
3楼-- · 2019-01-17 16:00

You can use moment.js to get the values formatted, but Highcharts has it's own date formatting functionality which would be more idiomatic with Highcharts. It can be attached onto the tooltip option in the highcharts constructor like so:

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

You may want to also add the dateTimeLabelFormats object with the options you need for your dates, under the xAxis.

I did this example with your code

查看更多
爷、活的狠高调
4楼-- · 2019-01-17 16:03

You can use {value:%Y-%m-%d} template filter.

For an example:

headerFormat: '<span style="font-size: 10px">{point.key:%Y-%m-%d}</span><br/>'
查看更多
5楼-- · 2019-01-17 16:19

You need to return a formatted date in the tooltip using tooltip formatter .

Here is the tooltip formatter API for your reference.

For formatting the date part, you can make use of Highcharts.dateFormat()

The format is a subset of the formats for PHP's strftime function.

You can also refer PHP's strftime for more date formats.

I managed to format your tooltip as below.

  tooltip: {
                formatter: function() {
                    return  '<b>' + this.series.name +'</b><br/>' +
                        Highcharts.dateFormat('%e - %b - %Y',
                                              new Date(this.x))
                    + '  <br/>' + this.y + ' Kg.';
                }
            }
查看更多
登录 后发表回答