最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Highcharts Synchronized charts display tooltip - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Apr 5, 2016 at 22:09 Tester asked Apr 5, 2016 at 21:22 TesterTester 8082 gold badges15 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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/doc_snyder/51zdn0jz/6/ for your updated fiddle. I have graciously taken the code linked in this Post http://forum.highcharts./highcharts-usage/synchronize-chart-with-shared-tooltip-t33919/.

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]);
        }
    }
};
发布评论

评论列表(0)

  1. 暂无评论