在不赞成使用的情况下替换我的代码中的highcharts。

问题描述 投票:2回答:2

我有一些同步的高位图表,并在移动鼠标时使用以下代码同步十字准线:

//catch mousemove event and have all charts' crosshairs move along indicated values on x axis
function syncronizeCrossHairs(chart) {
['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;
    Highcharts.charts.forEach(ch => {
      var e = ch.pointer.normalize(evt), // Find coordinates within the chart   
        points = [];
      ch.series.forEach(s => {
        var point = s.searchPoint(e, true);
        if (point) {
          point.setState();
          if (s.visible) {
             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) {
          ch.tooltip.refresh(points); // Show the tooltip
        }
      }
      ch.xAxis[0].drawCrosshair(x, points[0])
    })
  }))
})
}

我现在收到以下控制台消息:

[Highcharts错误#32:www.highcharts.com/errors/32/?Highcharts.each = Array.forEach

Highcharts.each:Array.forEach

有人可以建议我如何替换代码中的Highcharts.each命令吗?

谢谢

highcharts
2个回答
1
投票

无论您是否拥有Highcharts。每个都将其更改为for循环。这是我的项目中带有同步图表的前后示例:

之前(使用不赞成使用的Highcharts.each方法:] >>

Highcharts.each(Highcharts.charts, function (chart) {
  if (chart && chart !== thisChart) {
    if (chart.xAxis[0].setExtremes) {
      chart.xAxis[0].setExtremes(e.min, e.max, undefined, false, {
        trigger: "syncExtremes"
      });
    }
  }
});

然后,只需将其更改为for循环:

for (let i = 0; i < Highcharts.charts.length; i++) {
  let chart = Highcharts.charts[i];
  if (chart && chart !== thisChart) {
    if (chart.xAxis[0].setExtremes) {
      chart.xAxis[0].setExtremes(e.min, e.max, undefined, false, {
        trigger: "syncExtremes"
      });
    }
  }
}
    

0
投票

我认为正在改变:

© www.soinside.com 2019 - 2024. All rights reserved.