简体   繁体   English

在Highcharts中显示不可见系列的工具提示

[英]Display tooltip for invisible series in Highcharts

I am trying to display a custom tooltip using Highcharts. 我正在尝试使用Highcharts显示自定义工具提示。 You can find an example of the code here: http://jsfiddle.net/jalbertbowdenii/fDNh9/188/ 您可以在此处找到代码示例: http : //jsfiddle.net/jalbertbowdenii/fDNh9/188/

When you hover over the chart, you can see that the tooltip only contains values from Series 2, but not from Series 1 (which is invisible). 将鼠标悬停在图表上时,您会看到工具提示仅包含来自系列2的值,而不包含来自系列1(不可见)的值。 When you click on "Series 1" in the legend, you can see the values from Series 1 in the tooltip. 单击图例中的“系列1”时,可以在工具提示中看到系列1中的值。

EDIT: no code to commit, just fixing linkrot/adhering to editing rules... 编辑:无需提交code ,只需修复linkrot /遵守编辑规则即可...
Is there any way to display the values from an invisible series in a tooltip? 有什么方法可以在工具提示中显示不可见序列中的值吗?

tooltip: {
    formatter: function() {
        var s = '<b>'+ this.x +'</b>';
        var chart = this.points[0].series.chart; //get the chart object
        var categories = chart.xAxis[0].categories; //get the categories array
        var index = 0;
        while(this.x !== categories[index]){index++;} //compute the index of corr y value in each data arrays           
        $.each(chart.series, function(i, series) { //loop through series array
            s += '<br/>'+ series.name +': ' +
                series.data[index].y +'m';     //use index to get the y value
        });           
        return s;
    },
    shared: true
}

Another way to go about this is to make certain attributes of the series invisible, rather than the entire series itself. 解决此问题的另一种方法是使系列的某些属性(而不是整个系列本身)不可见。 This will allow you to see it in the tooltip and legend. 这样您就可以在工具提示和图例中看到它。

Here's what I did: 这是我所做的:

  1. First, I set the line color of the invisible series to "transparent." 首先,我将不可见系列的线条颜色设置为“透明”。
  2. Next, I set the fill color for the invisible series markers to "transparent." 接下来,我将不可见系列标记的填充颜色设置为“透明”。
  3. Finally, I disabled the hover state for the markers. 最后,我禁用了标记的悬停状态。 This prevents the shadowy highlight circles from appearing as you move your mouse cursor over each point in the visible series. 这样可以防止在将鼠标光标移到可见系列中的每个点上时出现阴影高光圆圈。

Here's a modified version of your original fiddle with these changes: http://jsfiddle.net/brightmatrix/fDNh9/184/ 这是您对原始提琴的修改后的版本,其中包含以下更改: http : //jsfiddle.net/brightmatrix/fDNh9/184/

series: [{
    data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
    lineColor: 'transparent',       // make the line invisible
    marker: { 
        fillColor: 'transparent',   // make the line markers invisible
        states: {
            hover: {
                enabled: false      // prevent the highlight circle on hover
            }
        }
    }
}, {
    data: [216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5]
}]

Two items to note: 注意两点:

  1. I've used the attribute enableMouseTracking: false with other invisible series to prevent users from interacting with them (to achieve visual effects). 我已将属性enableMouseTracking: false与其他不可见的系列一起使用,以防止用户与其互动(以实现视觉效果)。 If you set this for your invisible series, it will prevent the series data from appearing in your tooltip. 如果您为不可见的系列设置了此选项,则将阻止系列数据出现在工具提示中。
  2. If you wanted to keep your invisbile series from appearing in the legend, you can add the attribute showInLegend: false . 如果您希望不让隐藏序列出现在图例中,则可以添加属性showInLegend: false Its data will still show in the tooltip. 其数据仍将显示在工具提示中。

I hope this helps others who come across this question. 我希望这对遇到此问题的其他人有所帮助。

The tooltip formatter is a function like any other function so if you just make the data available it can return a string with values for all series. 工具提示格式化程序是与任何其他函数一样的函数,因此,如果您仅使数据可用,则它可以返回包含所有系列值的字符串。 In this example I moved the series arrays and categories to separate variables and the tooltip formatter uses an index into these arrays to find the values. 在此示例中,我将系列数组和类别移至单独的变量,并且工具提示格式化程序使用这些数组中的索引来查找值。

formatter: function() {
    var index = $.inArray(this.x, categories);
    var s = '<b>'+ this.x +'</b>';

    s += '<br/>'+ chart.series[0].name + ': ' + data1[index];
    s += '<br/>'+ chart.series[1].name + ': ' + data2[index];

    return s;
}

Too late for the answer but this is what I did. 答案太迟了,但这就是我所做的。 Plot the chart and make the color transparent. 绘制图表并使颜色透明。 Plotted it on the opposite y axis and set max to zero. 将其绘制在相反的y轴上,并将max设置为零。 Set alignTicks to false. 将alignTicks设置为false。 Something like this. 这样的事情。

chart: {
        alignTicks: false,
        type: 'line'
    },

Then the only thing needed is to change the color value in tooltip formatter since the label will be transparent. 然后,唯一需要做的就是在工具提示格式化程序中更改颜色值,因为标签将是透明的。

Hope this helps someone. 希望这对某人有帮助。

Happy Learning :) 快乐学习:)

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

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