如何在Highchart中自定义工具提示?

Not*_*nez 5 javascript jquery highcharts

我希望我的工具提示根据x轴显示时间范围。
下图显示了我想要的。因此,如果有人知道我该怎么做,请让我知道或建议我。谢谢 :)

在此处输入图片说明

这是我的代码

Highcharts.chart('container', {
    chart: {
        type: 'areaspline'
    },
    title: {
        text: '<b>Power consumption</b>'
    },
    xAxis: {
      categories: [
                    '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00',
                    '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00',
                    '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00',
                    '21:00', '22:00', '23:00'
                  ]
      },
    yAxis: {
        title: {
            text: 'Power (watt)'
        }
    },
    tooltip: {
      borderColor: '#2c3e50',
      shared: true
    },
    plotOptions: {
        areaspline: {
            marker: {
                enabled: true,
                symbol: 'circle',
                radius: 3,
                states: {
                    hover: {
                        enabled: true
                    }
                }
            }
        },
        series: {
          fillOpacity: 0.5
        }
    },
    series: [{
        name: 'Today ',
        data: [3, 4, 3, 5, 4, 10, 12, 3, 4, 3, 5, 4, 10, 12, 3, 4, 3, 5, 4, 10, 12, 3, 4, 3],
        color: '#2ecc71',
        zIndex: 1
    }, {
        name: 'Yesterday ',
        data: [1, 3, 4, 3, 3, 5, 4, 1, 3, 4, 3, 3, 5, 4, 1, 3, 4, 3, 3, 5, 4, 1, 3, 4],
        color: '#bdc3c7',
        zIndex: 0
    }],
    
    exporting: {
      buttons: {
        contextButtons: {
          enabled: false,
          menuItems: null
         }
      },
      
    enabled: false
    },

    credits: {
      enabled: false
    }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
Run Code Online (Sandbox Code Playgroud)

mor*_*ree 7

您可以使用tooltip.formatter构建自己的工具提示。根据您的情况,您需要编辑工具提示的标题。

formatter: function (tooltip) {     
  const header = `<span style="color: blue;">${this.x} - ${this.x.replace(/:00/, ':59')}</span><br/>`

  return header + (tooltip.bodyFormatter(this.points).join(''))
}
Run Code Online (Sandbox Code Playgroud)

示例:http//jsfiddle.net/cynysyhb/

  • 最好使用`$ {this.x.replace(/:00 /,':59')}`而不是`$ {this.x.replace(/ 00 /,'59')}`,它将代替分钟值只要 (2认同)