customize highcharts tooltip to show datetime

前端 未结 5 1444
生来不讨喜
生来不讨喜 2020-12-01 09:19

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 th

相关标签:
5条回答
  • 2020-12-01 09:55

    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.';
                    }
                }
    
    0 讨论(0)
  • 2020-12-01 10:02

    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/>'
    
    0 讨论(0)
  • 2020-12-01 10:03

    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

    0 讨论(0)
  • 2020-12-01 10:05

    I do this:

    headerFormat: '{point.x:%e %b %H:%M}'
    

    E.g.

    "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
            },
    
    0 讨论(0)
  • 2020-12-01 10:09

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

    sample:
    tooltip: {
               xDateFormat: '%Y-%m-%d'
             },
    
    0 讨论(0)
提交回复
热议问题