Change Highcharts tooltip formatter from chart Obj

2019-04-21 21:08发布

问题:

I have found that I can change series with setData, and I know I can modify Max values with .setExtremes , but I cannot figure out how to set the tooltip formatter from the chart object. How do I update that field ? If i have a chart object , how do I update its tooltip formatter property ? and How about the plotOptions tooltip formatter?

What I have tried :

chart1.tooltip.formatter = function() {return ''+this.series.name +'example: '+ this.y      +'example';};

But nothing changed in my tooltips when i added that after the chart definition (for testing). Also, I noted that this

console.log (chart1.tooltip.formatter);

returns undefined, but I don't know why.

Fiddle so you can try it out.

http://jsfiddle.net/pCuUW/5/ Thanks in advance.

回答1:

You can use chart.tooltip.options.formatter instead, like

chart.tooltip.options.formatter = function() {
    var xyArr=[];
    $.each(this.points,function(){
        xyArr.push('Serie: ' + this.series.name + ', ' +'X: ' + this.x + ', Y: ' +this.y);
    });
    return xyArr.join('<br/>');
}

Changing tooltip formatter dynamically | Highchart & Highstock @ jsFiddle

UPDATE In new (5.0.0+) versions of highcharts, this can also be done using the chart.update() method

  chart.update({
    tooltip: {
      formatter: function() {
        var xyArr = [];
        $.each(this.points, function() {
          xyArr.push('Serie: ' + this.series.name + ', ' + 'X: ' + this.x + ', Y: ' + this.y);
        });
        return xyArr.join('<br/>');
      }
    }
  });

Changing tooltip formatter dynamically with chart.update | Highchart & Highstock @ jsFiddle