我可以使用两种不同的格式化程序来生成高级工具提示吗?

Ama*_*nda 16 javascript highcharts

我有一个highcharts表,其中包含两个使用命名值的数据系列.在我的一个系列的工具提示中,我想引用该系列中的数据点.所以这个答案中的解决方案:如何在同一图形的每条曲线中的Highcharts上使用不同的格式化程序?对我没有帮助.我需要的不仅仅是tooltipText,我需要一个格式化程序:

一个:

formatter: function() {
    return this.x + ': ' + this.series.name +
    '<br> $' + Highcharts.numberFormat(this.y, 0);
     }
Run Code Online (Sandbox Code Playgroud)

对于另一个:

formatter: function() {
    return 'In ' + this.x + ' the median value was' + this.median + 
    'and the total $' + Highcharts.numberFormat(this.y, 0);                        
 }
Run Code Online (Sandbox Code Playgroud)

Ric*_*ann 31

内部格式化程序this引用了焦点系列,您可以if/else在格式化程序中添加一个字符串并为每个格式化程序返回一个字符串,如下所示.

tooltip: {
    shared: false,
    formatter: function() {
        var text = '';
        if(this.series.name == 'MSFT') {
            text = this.x + ': ' + this.series.name +
                   '<br> $' + Highcharts.numberFormat(this.y, 0);
        } else {
            text = 'In ' + this.x + ' the median value was' + this.median +
                   'and the total $' + Highcharts.numberFormat(this.y, 0);
        }
        return text;
    }
}
Run Code Online (Sandbox Code Playgroud)

演示

  • +1,谢谢 - 虽然在我看来,最好使用`this.series.options.id`而不是`this.series.name`,所以它不依赖于可能容易改变的东西.初始化系列时可以设置id. (8认同)