highcharts-ng addpoint每秒

时间:2016-10-14 10:45:54

标签: javascript angularjs highcharts highcharts-ng

我有一个livingata样条(每秒更新) 但我会用角度应用程序做同样的事情

(我想这样做:http://www.highcharts.com/demo/dynamic-update 在我的angularApp中使用highcharts-ng。)

没有角度我有:

events: {
                load: function () {

                    var series = this.series[0];
                    setInterval(function () {
                        var x = (new Date()).getTime(), // now
                            y = Math.random()*180;
                        series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }

但是有了角度,我尝试使用$ scope来编写代码......这没有任何作用:/

 var series = $scope.series[0];
 setInterval(function () {
        var x = (new Date()).getTime() // now
      var y = Math.random();
        series.addPoint([x, y], true, true);
    }, 1000); 
};

这里有一个小提琴:http://jsfiddle.net/c58b1z6b/10/

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我认为你可以通过添加$ scope的小改变来实现预期的结果。$ apply适用于你的setInterval函数:

     setInterval(function () {
        var x = (new Date()).getTime() // now
        var y = Math.random()*180;
        $scope.$apply(function() {
           //series.addPoint([x, y], true, true);
           series.data.push(y);
        })
    }, 1000); 

我已经注释掉了对addPoint()的原始调用,因为结果并不像我预期的那样......