在特定时间之后以角度js实时更新高图表

时间:2017-04-13 12:42:02

标签: angularjs highcharts

我希望在特定时间跨度之后以角度js实时更新高图表,并且它应该在没有用户交互的角度JS中反映在html上。 以下是Controller,Directive和HTML的代码。我正在使用rest web服务从后端获取数据。 控制器:

function dashboardController($scope, $http) {
    this.$scope = $scope;

        $scope.init=function(){
            _refreshPageData();
            function _refreshPageData() {
                $http({
                    method : 'GET',
                    url : 'http://localhost:8080/SpringWebApp/rest/employee/monthlydata'
                }).then(function successCallback(response) {
                    $scope.data = response.data;
                    var data = $scope.data;
                    var a = [];
                    parseInt($scope.data[0].data);
                    angular.forEach(data, function(data, key) {
                          a.push(parseInt(data.data));
                        });
                    console.log($scope.data);
                    $scope.chartConfig = {
                            xAxis: {
                                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                                             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                            },
                                        title: {
                                text: 'USD to EUR exchange rate from 2006 through 2008'
                            },            subtitle: {
                                text: document.ontouchstart === undefined ?
                                    'Click and drag in the plot area to zoom in' :
                                    'Pinch the chart to zoom in'
                            },
                            yAxis: { title: { text: 'Temperature (Celsius)' } },
                            tooltip: { valueSuffix: ' celsius' },
                            legend: { align: 'center', verticalAlign: 'bottom', borderWidth: 0 },
                               plotOptions: {
                                area: {
                                    fillColor: {
                                        linearGradient: { x1: 200, y1: 220, x2: 220, y2: 2221},
                                        stops: [
                                            [0, Highcharts.getOptions().colors[0]],
                                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                                        ]
                                    },
                                    marker: {
                                        radius: 2
                                    },
                                    lineWidth: 1,
                                    states: {
                                        hover: {
                                            lineWidth: 1
                                        }
                                    },
                                    threshold: null
                                }
                            },
                            series: [{
                              type:'area',
                              data :a
                            }]
                    };
                }, function errorCallback(response) {
                    console.log(response.statusText);
                });
            }
        }


}

Directive :

var chartDirective = function () {
    return {
        restrict: 'E',
        replace: true,
        template: '<div></div>',
        scope: {
            config: '='
        },
        link: function (scope, element, attrs) {
            var chart;
            var process = function () {
                var defaultOptions = {
                    chart: { renderTo: element[0] },
                };
                var config = angular.extend(defaultOptions, scope.config);
                chart = new Highcharts.Chart(config);
            };
            process();
            scope.$watch("config.series", function (loading) {
                process();
            });
            scope.$watch("config.loading", function (loading) {
                if (!chart) {
                    return;
                }
                if (loading) {
                    chart.showLoading();
                } else {
                    chart.hideLoading();
                }
            });
        }
    };
};

Html :

<!DOCTYPE html>
<html lang="en">

  <head>
    <script data-require="angular.js@*" data-semver="1.2.5" src="http://code.angularjs.org/1.2.5/angular.js"></script>
    <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script data-require="highcharts@*" data-semver="2.3.5" src="//cdnjs.cloudflare.com/ajax/libs/highcharts/2.3.5/highcharts.js"></script>
    <script src="chartDirective.js"></script>
    <script src="dashboardController.js"></script>
    <script src="app.js"></script>
    <meta charset="utf-8"> 
  </head>

  <body ng-app="app">
    <div ng-controller="dashboardController">
    <div ng-init="init()"></div>
      <chart config="chartConfig"></chart>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以使用$interval

根据时间间隔发出请求
$interval(function() {
    YourApi.response.then,
    series.addPoint([whatever);
}, 1000);