【问题标题】:Capturing touch events (touchstart and touchmove) in addition to mousemove for synchronising highcharts除了 mousemove 之外,还捕获触摸事件(touchstart 和 touchmove)以同步 highcharts
【发布时间】:2020-04-09 17:57:16
【问题描述】:

在其他人herehere 的帮助下,我一直在构建一个基本网页,该网页显示来自具有多个同步高图的气象站的数据,我已经能够为基于鼠标的系统(Windows等),我如何调整下面的代码来捕获 touchstart 和 touchmove 事件:

//catch mousemove event and have all charts' crosshairs move along indicated values on x axis

function syncronizeCrossHairs(chart) {
  var container = $(chart.container),
    offset = container.offset(),
    x;

  container.mousemove(function(evt) {
    x = evt.clientX - chart.plotLeft - offset.left;
    //remove old plot line and draw new plot line (crosshair) for this chart
    var xAxis1 = chart1.xAxis[0],
      points = [],
      points1 = [],
      points2 = [],
      points3 = [],
      points4 = [],
      e = chart1.pointer.normalize(evt); // Find coordinates within the chart   

    chart1.series.forEach(s => {
      var point = s.searchPoint(e, true)
      if (point) {
        point.setState();
        points.push(point)
      }
    })

    if (points) {
      var number = 0;
      Highcharts.each(points, function(p, i) {
        if (!p.series.visible) {
          points.splice(i - number, 1);
          number++;
        }
      })
      if (points.length) {
        chart1.tooltip.refresh(points); // Show the tooltip
      }
    }

    xAxis1.removePlotLine("myPlotLineId");
    xAxis1.addPlotLine({
      value: chart.xAxis[0].translate(x, true),
      width: 1,
      id: "myPlotLineId"
    });

    /*----- second chart ------*/
    var xAxis2 = chart2.xAxis[0];

    chart2.series.forEach(s => {
      var point = s.searchPoint(e, true)
      if (point) {
        point.setState();
        points1.push(point)
      }
    })

    if (points1[0]) {
      var number = 0;
      Highcharts.each(points1, function(p, i) {
        if (!p.series.visible) {
          points1.splice(i - number, 1);
          number++;
        }
      })
      if (points1.length) {
        chart2.tooltip.refresh(points1); // Show the tooltip
      }
    }

    xAxis2.removePlotLine("myPlotLineId");
    xAxis2.addPlotLine({
      value: chart.xAxis[0].translate(x, true),
      width: 1,
      id: "myPlotLineId"
    });

    /*----- third chart ------*/
    var xAxis3 = chart3.xAxis[0];

    chart3.series.forEach(s => {
      var point = s.searchPoint(e, true)
      if (point) {
        point.setState();
        points2.push(point)
      }
    })

    if (points2[0]) {
      var number = 0;
      Highcharts.each(points1, function(p, i) {
        if (!p.series.visible) {
          points2.splice(i - number, 1);
          number++;
        }
      })
      if (points2.length) {
        chart3.tooltip.refresh(points2); // Show the tooltip
      }
    }

    xAxis3.removePlotLine("myPlotLineId");
    xAxis3.addPlotLine({
      value: chart.xAxis[0].translate(x, true),
      width: 1,
      id: "myPlotLineId"
    });

    //if you have other charts that need to be syncronized - update their crosshair (plot line) in the same way in this function.                   
  });
}

谢谢

【问题讨论】:

    标签: highcharts mousemove touchstart touchmove


    【解决方案1】:

    基于 Highcharts 官方演示库 https://www.highcharts.com/demo/synchronized-charts 中的这个示例,我能够将类似的模式包装到您的代码中。

    演示:http://jsfiddle.net/BlackLabel/mnLzbe1s/

      ['mousemove', 'touchmove', 'touchstart'].forEach(function(eventType) {
        var container = $(chart.container),
          offset = container.offset(),
          x;
        container[0].addEventListener(eventType,
          (function(evt) {
            x = evt.clientX - chart.plotLeft - offset.left;
            //remove old plot line and draw new plot line (crosshair) for this chart
            var xAxis1 = chart1.xAxis[0],
              points = [],
              points1 = [],
              points2 = [],
              points3 = [],
              e = chart1.pointer.normalize(evt); // Find coordinates within the chart
    
           ...
           })
         )
      })
    

    【讨论】:

    • 谢谢!!当我使用触摸滚动工具提示和绘图线时,绘图线仅显示在我正在触摸的图表上,工具提示完美运行。
    • 这里是 jsfiddle http://jsfiddle.net/ashenshugar/716jx4n9/ 上的工作版本(所有触摸图表上的情节线除外)的链接。
    • 在触摸移动(事件)时计算 plotLine 位置有问题。我认为在这种情况下,一个更好的解决方案是使用drawCrosshair 功能而不是plotLines。检查代码:jsfiddle.net/BlackLabel/xLsdrco8
    • 谢谢,效果很好。我非常感谢所有的帮助。
    • @ashenshugar 如果您满意,您可以投票支持我的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2013-12-23
    相关资源
    最近更新 更多