如何在工具提示高图中获取多个系列数据?

时间:2013-10-11 07:14:17

标签: javascript highcharts tooltip

我想在每个列的工具提示中显示多个系列数据

tooltip: {
    formatter: function() {
        return '<span style="color:#D31B22;font-weight:bold;">' +this.series.name +': '+ this.y +'<br/>'+
               '<b style="color:#D31B22;font-weight:bold;">'+this.x +'</b><span>';
    }
},

和数据

series: [{
    showInLegend: false,
    name: 'Total Click',
    data: [3000,200,50,4000],
    color: '#9D9D9D'
}, {
    showInLegend: false,
    name: 'Total View',
    data: [100,2000,3000,4000],
    color: '#D8D8D8'
}]

我这样使用,但在工具提示中,一次只显示一个系列数据。 我想显示这样的数据(总视图:100和总点击次数:3000)

3 个答案:

答案 0 :(得分:30)

请尝试使用此代码

<强> updated DEMO

tooltip: {
        formatter: function() {
            var s = [];

            $.each(this.points, function(i, point) {
                s.push('<span style="color:#D31B22;font-weight:bold;">'+ point.series.name +' : '+
                    point.y +'<span>');
            });

            return s.join(' and ');
        },
        shared: true
    },

答案 1 :(得分:12)

您需要使用共享参数http://api.highcharts.com/highcharts#tooltip.shared,然后在formater中迭代每个点。

答案 2 :(得分:2)

如果有人在寻找散点图,则solution显示共享工具提示。

formatter: function(args) {
    var this_point_index = this.series.data.indexOf( this.point );
    var this_series_index = this.series.index;
    var that_series_index = this.series.index == 0 ? 1 : 0; // assuming 2 series
    var that_series = args.chart.series[that_series_index];
    var that_point = that_series.data[this_point_index];
    return 'Client: ' + this.point.name +
           '<br/>Client Health: ' + this.x +
           '<br/>' + this.series.name + ' Bandwidth: ' + this.y + 'Kbps' +
           '<br/>' + that_series.name + ' Bandwidth: ' + that_point.y + 'Kbps';
}

Jsfiddle link to Solution