简体   繁体   English

Highcharts将多个图表与多个系列上的工具提示同步

[英]Highcharts synchronize tooltip on multiple charts with multiple series

I am trying to synchronize shared tooltip across multiple charts, each having multiple series. 我正在尝试在多个图表之间同步共享的工具提示,每个图表都有多个系列。

The problem is in the below example, the tooltip always shows the 3 series, even though at that particular point there are only two series present. 问题在下面的示例中,工具提示始终显示3个系列,即使在该特定点上只有两个系列。

1) How do I make sure that a series is shown in tooltip only when it is actually present? 1)如何确保仅在工具提示中实际显示某个系列时?

2) How do I make sure the tooltip is closed when we move out of the chart? 2)当我们移出图表时,如何确定工具提示已关闭?

JSFiddle: https://jsfiddle.net/qoL7fx27/1/ JSFiddle: https ://jsfiddle.net/qoL7fx27/1/

Code for synchronization in fiddle: 小提琴中用于同步的代码:

$('#container').bind('mousemove touchmove touchstart', function (e) {
    var chart,
        point,
        i,
        event;

    for (i = 0; i < Highcharts.charts.length; i = i + 1) {
        chart = Highcharts.charts[i];
        var points = [];
        // Find coordinates within the chart
        event = chart.pointer.normalize(e.originalEvent);
        // Get the hovered point
        for(var j=0; j<chart.series.length; j++) {
           point = chart.series[j].searchPoint(event, true);
           points.push(point);  
        }

        chart.tooltip.refresh(points);

    }
});

1) How do I make sure that a series is shown in tooltip only when it is actually present? 1)如何确保仅在工具提示中实际显示某个系列时?

The unwanted behavior is caused by searchPoint function - it returns the nearest point even though the x position doesn't mach with other points. 不必要的行为是由searchPoint函数引起的-即使x位置不与其他点匹配,它也会返回最近的点。 So if the series has only one point it'll be always found. 因此,如果该系列只有一点,那么总会找到它。

Solution: 解:

Manually select points to display in tooltip.formatter : 手动选择要显示在tooltip.formatter点:

        formatter: function() {
          var outputString = '';

          this.points.forEach(function(point) {
            if (point.x === this.x) {
              outputString += "<span style='color:" + point.color + "'>\u25CF</span> " + point.series.name + ": <b>" + point.y + "</b><br/>";
            }
          }, this);
          return outputString;
        }

API reference: https://api.highcharts.com/highcharts/tooltip.formatter API参考: https //api.highcharts.com/highcharts/tooltip.formatter


2) How do I make sure the tooltip is closed when we move out of the chart? 2)当我们移出图表时,如何确定工具提示已关闭?

Restore the default Highcharts.Pointer.prototype.reset function by removing these lines: 通过删除以下Highcharts.Pointer.prototype.reset行来恢复默认的Highcharts.Pointer.prototype.reset函数:

 Highcharts.Pointer.prototype.reset = function() { return undefined; }; 


Demo for both questions: https://jsfiddle.net/BlackLabel/2mxxrk5n/ 两个问题的演示: https : //jsfiddle.net/BlackLabel/2mxxrk5n/


Update: 更新:

I posted the wrong answer for the second question. 对于第二个问题,我发布了错误的答案。 This code hides tooltips: 此代码隐藏了工具提示:

$('#container').bind('mouseout', function(e) {
  Highcharts.charts.forEach(function(chart) {
    chart.tooltip.hide();
    // undo point highlight
    chart.series.forEach(function(series) {
        series.points.forEach((point) => point.setState(''));
    });
  });
});

can you please tell me how to highlight the corresponding points in each chart? 您能告诉我如何突出显示每个图表中的对应点吗? As of now, the tooltip shows correctly, however the points are not highlighted in three charts 到目前为止,工具提示可以正确显示,但是在三个图表中这些点仍未突出显示

This piece of highlights points: 这段要点指出:

points.forEach(function(point_) {
  if (point_) {
    point_.highlight(e);
  }
}, this);

To achieve the desired behavior you must provide a logic for filtering points that should be Highlighted. 为了实现所需的行为,您必须提供用于过滤应突出显示的点的逻辑。 Here's a very simplified example adjusted to this particular case: 这是一个针对此特定情况的非常简化的示例:

// provide a logic for filtering points
if(points[0] && points[1].x > 0) {
    points.pop(); // remove the unwanted point
}

Here is my solution. 这是我的解决方案。 It's perfectly working for me. 这对我来说是完美的。 I made adjustments based on Synchronisation of multiple charts 我根据多个图表的同步进行了调整

Demo here 在这里演示

影响

The following code shows/hides the tooltip and makes sure they are aligned on mousemove and mouseleave . 以下代码显示/隐藏了工具提示,并确保它们在mousemovemouseleave上对齐。

Note that I found that I only need to find the first point searched and use it to show/hide the tooltip. 请注意,我发现只需要找到搜索到的第一个点,并使用它来显示/隐藏工具提示。 It's because all my time series share the same x values. 这是因为我所有的时间序列都共享相同的x值。

$("#container").bind("mousemove mouseleave", function(e) {
  for (let i = 0; i < Highcharts.charts.length; ++i) {
    let hart = Highcharts.charts[i];
    let event = chart.pointer.normalize(e.originalEvent); // Find coordinates within the chart
    let point;
    for (let j = 0; j < chart.series.length && !point; ++j) {
      point = chart.series[j].searchPoint(event, true);
    }
    if (!point) return;

    if (e.type === "mousemove") {
       point.onMouseOver();
      chart.xAxis[0].drawCrosshair(event, point); // Show the crosshair
    } else {
      point.onMouseOut();
      chart.tooltip.hide(point);
      chart.xAxis[0].hideCrosshair();
    }

  }
});

Keep reseting the reset function so as to disallow HighCharts resetting the points -- we take over the control. 继续reset功能,以免HighCharts重置点-我们接管控制权。

Highcharts.Pointer.prototype.reset = function() {
  return undefined;
};

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

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