Highcharts:动态更改工具提示格式化程序?

Ric*_*ard 5 javascript highcharts

我想根据全局值格式化可重用的Highcharts工具提示.(我使用相同的图表在货币和数值之间切换:如果我在图表上显示货币数据,我想将工具提示格式化为货币.)

但是,this在Highcharts工具提示函数似乎只引用本地数据点,我似乎无法传递值.

如何传入值或获取全局值?这是我现在的代码,它失败了:

getChartTooltip: function() {
    return function(graphType) {
        var prefix = (graphType === 'currency') ? '$' : ''; // Fails
        return prefix + Highcharts.numberFormat(this.y, 0);
    };
},

initializeChart: function() { 
  var graphType = 'currency';
  var chartOptions = {};
  chartOptions.tooltip = {
      formatter: getChartTooltip(graphType)
  };
  // create chart, etc... 
  $('change-chart-type').on('click', function() { 
     // Try to update the tooltip formatter, fail horribly...
     graphType = 'number';
     chart.series[0].update({
         tooltip: {
             formatter: _this.getChartTooltip(graphType)
         }
     });
 });
Run Code Online (Sandbox Code Playgroud)

有什么更好的方法呢?

Kab*_*lak 5

你不需要改变它tooltip.formatter,因为它formatter本身就会改变它tooltip.

我会尝试类似的东西:

tooltip: {
     formatter: function() {
          var unit = ' $',
              result = this.value;
          if(this.series.name == 'your_currency_serie_name'){
               result += unit;
          }
          return result;
     }
}
Run Code Online (Sandbox Code Playgroud)

'your_currency_serie_name'系列的名称在哪里引用货币值.