在highcharts ng

时间:2017-03-22 07:38:54

标签: javascript angularjs ajax highcharts-ng

我正在为我的角度项目开发highcharts ng,但数据没有被填满。 我认为可能存在与加载DOM和被调用函数有关的问题。

我的HTML部分:



<div style="width: 49%; float:right;"><highchart id="chart12" config="highchartsNG"></highchart></div>
&#13;
&#13;
&#13;

模块部分:

&#13;
&#13;
myApp = angular.module('myApp', ['LocalStorageModule', 'ui.bootstrap', 'highcharts-ng', 'ngAnimate', 'ui.router', 'angular-loading-bar', 'hc.marked', 'ngToast', 'angularMoment', 'slick', 'app-templates']);
&#13;
&#13;
&#13;

控制器部分:

&#13;
&#13;
myApp.controller('ReportController', ['$scope', 'CompanyService', function ($scope, CompanyService) {
    (function () {
        loadReport();
    })();

    function loadReport() {
      CompanyService.getReportData().then(function (data) {
        if (data.status === 200){
            $scope.dates = data.data.date;
            $scope.counts = data.data.count;

            $scope.fetchedData = {
              data: data.data.count
            };
            //
            // console.log("dates ==> ", $scope.dates);          // ["2017-03-14", "2017-03-19"]
            // console.log("counts ==> ", $scope.counts);   // {data: [22, 15]}

        }
      }, function (error) {

      });
    };

    $scope.dates = ['2017-03-19', '2017-03-18', '2017-03-17', '2017-03-16', '2017-03-15', '2017-03-14', '2017-03-13'];
    $scope.counts = [2,10,20, 25, 5, 15, 8];

  $scope.$watchGroup(['counts', 'dates'], function(newValues, oldValues) {

    // newValues[0] --> $scope.line
    // newValues[1] --> $scope.bar

    if(newValues !== oldValues) {
      $scope.highchartsNG = {
          chart: {
              type: 'column'
          },
          title: {
              text: 'Stacked column chart'
          },
          xAxis: {
              categories: $scope.dates
          },
          yAxis: {
              min: 0,
              title: {
                  text: 'Total count'
              },
              stackLabels: {
                  enabled: true,
                  style: {
                      fontWeight: 'bold',
                      color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                  }
              }
          },
          legend: {
              align: 'right',
              x: -70,
              verticalAlign: 'top',
              y: 20,
              floating: true,
              backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
              borderColor: '#CCC',
              borderWidth: 1,
              shadow: false
          },
          tooltip: {
              formatter: function() {
                  return '<b>'+ this.x +'</b><br/>'+
                      this.series.name +': '+ this.y +'<br/>'+
                      'Total: '+ this.point.stackTotal;
              }
          },
          plotOptions: {
              column: {
                  stacking: 'normal',
                  dataLabels: {
                      enabled: true,
                      color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                      style: {
                          textShadow: '0 0 3px black, 0 0 3px black'
                      }
                  }
              }
          },
          series: [{
            data: $scope.counts
          }]
      };
    }
  });

}]);
&#13;
&#13;
&#13;

我有referred并尝试实施它,但存在控制台错误

TypeError:无法设置属性&#39; getChartObj&#39;未定义的     在HighChartNGController。$ onInit

我认为应该在DOM加载后调用一个函数,这实际上由$watchGroup来处理。但我无法弄清楚如何。 任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

使用以下代码

   myApp.controller('ReportController', ['$scope', 'CompanyService', function ($scope, CompanyService) {
  (function () {
    loadReport();


      })();

      $scope.highchartsNG = {
        chart: {
          type: 'column'
        },
        title: {
          text: 'Stacked column chart'
        },
        xAxis: {
          categories: []
        },
        yAxis: {
          min: 0,
          title: {
            text: 'Total count'
          },
          stackLabels: {
            enabled: true,
            style: {
              fontWeight: 'bold',
              color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
          }
        },
        legend: {
          align: 'right',
          x: -70,
          verticalAlign: 'top',
          y: 20,
          floating: true,
          backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
          borderColor: '#CCC',
          borderWidth: 1,
          shadow: false
        },
        tooltip: {
          formatter: function() {
            return '<b>'+ this.x +'</b><br/>'+
            this.series.name +': '+ this.y +'<br/>'+
            'Total: '+ this.point.stackTotal;
          }
        },
        plotOptions: {
          column: {
            stacking: 'normal',
            dataLabels: {
              enabled: true,
              color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
              style: {
                textShadow: '0 0 3px black, 0 0 3px black'
              }
            }
          }
        },
        series: [{
          data: []
        }]
      };

      function loadReport() {
        CompanyService.getReportData().then(function (data) {
          if (data.status === 200){
            $scope.dates = data.data.date;
            $scope.counts = data.data.count;

            $scope.fetchedData = {
              data: data.data.count
            };
                //
                // console.log("dates ==> ", $scope.dates);          // ["2017-03-14", "2017-03-19"]
                // console.log("counts ==> ", $scope.counts);   // {data: [22, 15]}

              }
            }, function (error) {

            });
      };

      $scope.dates = ['2017-03-19', '2017-03-18', '2017-03-17', '2017-03-16', '2017-03-15', '2017-03-14', '2017-03-13'];
      $scope.counts = [2,10,20, 25, 5, 15, 8];

          //Assign old value
          $scope.highchartsNG.series[0].data=$scope.counts;
          $scope.highchartsNG.xAxis.categories=$scope.dates;  



      $scope.$watchGroup(['counts', 'dates'], function(newValues, oldValues) {

        // newValues[0] --> $scope.line
        // newValues[1] --> $scope.bar

        if(newValues !== oldValues) {

          //Assign value only
          $scope.highchartsNG.series[0].data=$scope.counts;
          $scope.highchartsNG.xAxis.categories=$scope.dates;     

        }
      });

    }]);

第二版

myApp.controller('ReportController', ['$scope', 'CompanyService', function ($scope, CompanyService) {


  $scope.highchartsNG = {
    chart: {
      type: 'column'
    },
    title: {
      text: 'Stacked column chart'
    },
    xAxis: {
      categories: []
    },
    yAxis: {
      min: 0,
      title: {
        text: 'Total count'
      },
      stackLabels: {
        enabled: true,
        style: {
          fontWeight: 'bold',
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
        }
      }
    },
    legend: {
      align: 'right',
      x: -70,
      verticalAlign: 'top',
      y: 20,
      floating: true,
      backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || 'white',
      borderColor: '#CCC',
      borderWidth: 1,
      shadow: false
    },
    tooltip: {
      formatter: function() {
        return '<b>'+ this.x +'</b><br/>'+
        this.series.name +': '+ this.y +'<br/>'+
        'Total: '+ this.point.stackTotal;
      }
    },
    plotOptions: {
      column: {
        stacking: 'normal',
        dataLabels: {
          enabled: true,
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
          style: {
            textShadow: '0 0 3px black, 0 0 3px black'
          }
        }
      }
    },
    series: [{
      data: []
    }]
  };


  $scope.dates = ['2017-03-19', '2017-03-18', '2017-03-17', '2017-03-16', '2017-03-15', '2017-03-14', '2017-03-13'];
  $scope.counts = [2,10,20, 25, 5, 15, 8];




    //Assign old value
    $scope.highchartsNG.series[0].data=$scope.counts;
    $scope.highchartsNG.xAxis.categories=$scope.dates;  

    CompanyService.getReportData().then(function (data) {
      if (data.status === 200){
        $scope.dates = data.data.date;
        $scope.counts = data.data.count;

        $scope.fetchedData = {
          data: data.data.count
        };

        $scope.highchartsNG.series[0].data=$scope.counts;
        $scope.highchartsNG.xAxis.categories=$scope.dates;    

                //
                // console.log("dates ==> ", $scope.dates);          // ["2017-03-14", "2017-03-19"]
                // console.log("counts ==> ", $scope.counts);   // {data: [22, 15]}

              }
            }, function (error) {

            });



          /*$scope.$watchGroup(['counts', 'dates'], function(newValues, oldValues) {



            if(newValues !== oldValues) {

             //Assign value only
             $scope.highchartsNG.series[0].data=$scope.counts;
             $scope.highchartsNG.xAxis.categories=$scope.dates;     

           }
         });*/

       }]);