Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharts Synchronized charts display tooltip

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.

enter image description here

Please advice.

like image 329
Tester Avatar asked Dec 25 '22 07:12

Tester


2 Answers

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/.

like image 99
Martin Schneider Avatar answered Jan 05 '23 05:01

Martin Schneider


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]);
        }
    }
};
like image 28
Aleksandr K. Avatar answered Jan 05 '23 04:01

Aleksandr K.