简体   繁体   English

HighCharts:仅在系列重叠时使用共享工具提示

[英]HighCharts: Use shared tooltip only when series overlap

In the below HighCharts example, the series A and B have identical data. 在下面的HighCharts示例中, A系列和B系列具有相同的数据。 Only the line for B is visible in the chart plot area, as it is plotted directly over A . 在图表绘图区域中只能看到B的线,因为它直接绘制在A

It is impossible for the end user to know that the A is behind B . 最终用户不可能知道AB后面。

We can set tooltip.shared = true in the configuration object to show all the data values for a given x-axis point when hovered over any series. 我们可以在配置对象中设置tooltip.shared = true ,以显示在任何系列上悬停时给定x轴点的所有数据值。 However, in my real-life example I have up to 50 series plotted on the chart and this is not appropriate. 但是,在我的实际例子中,我在图表上绘制了多达50个系列,这是不合适的。

Is it possible to keep the behaviour of tooltip.shared = false , but when the user hovers over a series that overlaps at that point with one or more series, to show all (and only) of the overlapping series values in the tooltip? 是否可以保持tooltip.shared = false的行为,但是当用户将鼠标悬停在该点与一个或多个系列重叠的系列上时,是否在工具提示中显示所有(且仅)重叠的系列值? Or is there any other user-friendly way to indicate that there are 2+ identical y-values at a given x-value? 或者是否有任何其他用户友好的方式来表明在给定的x值上有2个以上相同的y值?

http://jsfiddle.net/adamtsiopani/XbYZz/ http://jsfiddle.net/adamtsiopani/XbYZz/

$(function () {
    $('#container').highcharts({
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        tooltip: {
            valueSuffix: '°C'
        },
        series: [{
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'New York',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'Berlin',
            data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
});

http://jsfiddle.net/adamtsiopani/XbYZz/ http://jsfiddle.net/adamtsiopani/XbYZz/

Highcharts doesn't have a solution for this yet. Highcharts还没有解决方案。 They have a feature to hide one series so other is visible, this is a good alternative. 它们具有隐藏一个系列的功能,因此其他系列可见,这是一个很好的选择。 But if you need to get a shared tool-tip when the 2 series overlap it can be done as shown in the below fiddle. 但是如果您需要在2系列重叠时获得共享工具提示,则可以如下面的小提琴所示完成。

$(function () {
    var series1 = [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];

    var series2 = [24.9, 50.5, 106.4, 90.2, 80.0, 150.0, 160.6, 170.5, 160.4, 180.1, 95.6, 54.4];

    $('#container').highcharts({
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        tooltip: {
            formatter: function () {
                var s1 = this.series.chart.series[0].processedYData[this.point.index];
                var s2 = this.series.chart.series[1].processedYData[this.point.index];
                if (s1 == s2) {
                    return '<b>' + this.series.chart.series[0].name + ' :' + s1 + '</b><br/><b>' + this.series.chart.series[1].name + ' :' + s2 + '</b><br/>in month : ' + this.x;
                }
                return '<b>' + this.series.name + ' :' + this.y + '</b><br/>in month : ' + this.x;
            }
        },
        series: [{
            data: series1
        }, {
            data: series2
        }]
    });
});

http://jsfiddle.net/Malinga/2jbdqe6x/7/ http://jsfiddle.net/Malinga/2jbdqe6x/7/

reference: http://www.malinga.me/highcharts-shared-tooltips-only-in-overlapping-points/#more-109 参考: http//www.malinga.me/highcharts-shared-tooltips-only-in-overlapping-points/#more-109

Unless an elaborate work-around is concocted, highcharts does not support this yet. 除非精心制作复杂的解决方案,否则highcharts尚不支持这一点。 See this post (which has a comment from a user who claims to be a highcharts engineer): 请参阅此帖子(其中包含声称自己是高级工程师的用户的评论):

is a way to see all data in tooltip when the points are overlap with each other(or very close), but see only one data when a point is far from others 是一种在点相互重叠(或非常接近)时查看工具提示中的所有数据的方法,但是当一个点远离其他点时只能看到一个数据

I guess you will just have to rely on users using the legend for deselecting a series that is blocking another one. 我猜你只需依靠使用图例的用户取消选择阻止另一个的系列。

As user, I would be confused to see 50 series on a chart - is that readable? 作为用户,我会很困惑地看到图表上的50个系列 - 这是可读的吗? Good idea would be to use separate yAxis, or separate panes, but still 50 series.. 好主意是使用单独的yAxis,或单独的窗格,但仍然是50系列..

However, you can do some workaround. 但是,您可以做一些解决方法。 Don't use shared tooltip, but in tooltip formatter, get actual x-index (for example var xIndex = series.xData.indexOf(this.x); ) then loop through all series, get values from series data (for example var yValue = series.yData[xIndex]; ). 不要使用共享工具提示,但在工具提示格式化程序中,获取实际的x-index(例如var xIndex = series.xData.indexOf(this.x); )然后遍历所有系列,从系列数据中获取值(例如var yValue = series.yData[xIndex]; )。 Now compare with this.y and if values are similar add more points to the returned tooltip. 现在与this.y进行比较,如果值相似,则在返回的工具提示中添加更多的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM