我怎样才能绘制动态高图。从json开始,y轴和图表的数量是动态的

时间:2016-07-27 10:58:45

标签: angularjs json highcharts

我是角度js中的新手,我需要在我的角度页面中使用高图。问题是我必须用来自json的动态数据绘制图表,图表的数量也是动态的,也许它应该从一个json绘制3或4个不同的图表。我搜索了很多,但无法解决我的问题。 此代码有效,但在不同系列的一个图表中显示数据。我需要在不同的图表中显示每个系列,在这种情况下,json发送4个数据,但它将被更改。



 1. List item

 $scope.draw_chart = function(){
     Highcharts.chart('container2', {
         chart:{
             type: 'spline',
             animation: Highcharts.svg, // don't animate in old IE
             marginRight: 10,
             events: {
                 load: function () {
                     var this_chart = this;
                     $scope.ws = ngSocket('ws://#');
                     $scope.ws.onOpen(function () {
                     });
                     var k = 0 ;
                     var time=0;
                     $scope.points_avarage = [];
                     $scope.ws.onMessage(function (message) {
                         listener(JSON.parse(message.data));
                         var z = JSON.parse(message.data);
                         var line_to_draw = z.result.length;
                         var j = 0 ;
                         for(i=0 ; i < line_to_draw*2 ; i+=2)
                         {
                             $scope.data_to_draw[i] = {
                                 name : z.result[j][0]['name'] ,
                                 y : z.result[j][0]['rx-bits-per-second']
                             }
                             $scope.data_to_draw[i+1] = {
                                 name : z.result[j][0]['name'] ,
                                 y : z.result[j][0]['tx-bits-per-second']
                             }

                             j++;
                         }


                    
                         this_chart.series[0].name= $scope.data_to_draw[0].name;
                         this_chart.series[1].name= $scope.data_to_draw[1].name;
                         this_chart.series[2].name= $scope.data_to_draw[2].name;
                         this_chart.series[3].name= $scope.data_to_draw[3].name;

                    
                         for(i=0; i < line_to_draw*2; i++) {
                             var x = (new Date()).getTime();  // current time
                             var y =  parseInt($scope.data_to_draw[i].y);
                             this_chart.series[i].addPoint([x, y], true, true);
                         
                          
                         }

                     });
                     var d = new Date().toTimeString();
                 }
             }
         },
         global: {
             useUTC: false
         },
         title: {
             text: 'Live data'
         },
         xAxis: {
             type: 'datetime'//,
            
         },
         yAxis: {
             title: {
                 text: 'Value'
             },
             plotLines: [{

                width: 1,
                 color: '#808080'
             }
            ]
         }
        

        plotOptions: {
             series: {
                 marker: {
                    enabled: false
                 }

             }
         },

         series: [{
            
             data: (function () {

                 var data = [],
                     time = (new Date()).getTime(),
                     i;

                 for (i = -5; i <= 0; i += 1) {
                     data.push({
                         x: time ,
                         y: 0
                     });
                 }
                 return data;
             }())
         },
             {
                 
                 data: (function () {
                     var data = [],
                         time = (new Date()).getTime(),
                         i;

                     for (i = -5; i <= 0; i += 1) {
                         data.push({
                             x: time ,
                             y: 0
                         });
                     }
                    return data;
                }())
             },
             {
                
                 data: (function () {
                     var data = [],
                         time = (new Date()).getTime(),
                         i;

                     for (i = -5; i <= 0; i += 1) {
                         data.push({
                            x: time ,
                            y: 0
                         });
                     }
                     return data;
                 }())
             },
             {
                
                 data: (function () {
                     var data = [],
                         time = (new Date()).getTime(),
                         i;

                     for (i = -5; i <= 0; i += 1) {
                         data.push({
                            x: time ,
                            y: 0
                         });
                     }
                     return data;
                 }())
             }

         ]
     });



 };
&#13;
<div id="containet" ng-init="draw_chart()"></div>
&#13;
&#13;
&#13;

0 个答案:

没有答案