Highcharts Synchronized charts display tooltip

拈花ヽ惹草 提交于 2020-01-14 18:59:08

问题


I want to display tooltip in Synchronized charts. Please see this 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
      }
    }
  });

The tooltip can only display the first series but no second series, even mouse hover the second series.

Please advice.


回答1:


First you have to set the tooltip-Option shared to true. Then you have to update the mousemove touchmove touchstart-Eventhandler to deal with multiple series/points

$('#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
              }
          }
      }
});

See http://jsfiddle.net/doc_snyder/51zdn0jz/6/ for your updated fiddle. I have graciously taken the code linked in this Post http://forum.highcharts.com/highcharts-usage/synchronize-chart-with-shared-tooltip-t33919/.




回答2:


I've written more flexible solution based on Martin Schneider answer.

In my container 3 charts, first has 2 series, second 6 series and third 3 series, part of series not visible by default, part with disabled mouse event handling.

$('#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]);
        }
    }
};


来源:https://stackoverflow.com/questions/36437297/highcharts-synchronized-charts-display-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!