Highcharts同步图表显示工具提示

时间:2016-04-05 21:22:14

标签: javascript highcharts

我想在同步图表中显示工具提示。请参阅此Jsfiddle

 $('#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];
      event = chart.pointer.normalize(e.originalEvent); // Find coordinates within the chart
      point = chart.series[0].searchPoint(event, true); // Get the hovered point

      if (point) {
        point.onMouseOver(); // Show the hover marker
        chart.tooltip.refresh(point); // Show the tooltip
        chart.xAxis[0].drawCrosshair(event, point); // Show the crosshair
      }
    }
  });

工具提示只能显示第一个系列而不能显示第二个系列,甚至鼠标悬停在第二个系列中。

enter image description here

请建议。

2 个答案:

答案 0 :(得分:7)

首先,您必须将工具提示 - 选项shared设置为true。然后你必须更新mousemove touchmove touchstart - Eventhandler来处理多个系列/点

$('#container').bind('mousemove touchmove touchstart', function(e) {
      var chart,
      points,
      i,
      secSeriesIndex = 1;

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

          if (points[0] && points[1]) {
              if (!points[0].series.visible) {
                  points.shift();
                  secSeriesIndex = 0;
              }
              if (!points[secSeriesIndex].series.visible) {
                  points.splice(secSeriesIndex,1);
              }
              if (points.length) {
                  chart.tooltip.refresh(points); // Show the tooltip
                  chart.xAxis[0].drawCrosshair(e, points[0]); // Show the crosshair
              }
          }
      }
});

请参阅https://en.wikipedia.org/wiki/MU_puzzle了解您的更新小提琴。我慷慨地采用了本帖http://jsfiddle.net/doc_snyder/51zdn0jz/6/中链接的代码。

答案 1 :(得分:3)

我根据Martin Schneider的回答编写了更灵活的解决方案。

在我的容器3图表中,首先是2系列,第2系列和第3系列,默认情况下系列的一部分不可见,部分是禁用鼠标事件处理。

$('#charts-container').on('mousemove touchmove touchstart', shared_tooltip_handler);

shared_tooltip_handler = function (e) {
    var chart, point, i, event;

    var charts = $(this).children('div');

    for (i = 0; i < charts.length; i = i + 1) {
        chart = $(charts[i]).highcharts();
        if (!chart) continue;
        // Find coordinates within the chart
        event = chart.pointer.normalize(e.originalEvent); 

        var points = [];
        for (j = 0; j < chart.series.length; j = j + 1) {
            serie = chart.series[j];
            if (!serie.visible || serie.enableMouseTracking === false) continue;

            point = serie.searchPoint(event, true);
            // Get the hovered point
            if (point) points.push(point); 
        }

        if (points.length) {
            if (chart.tooltip.shared) {
                chart.tooltip.refresh(points);
            } else {
                chart.tooltip.refresh(points[0]);
            }
            chart.xAxis[0].drawCrosshair(e, points[0]);
        }
    }
};