如何在ng-repeat angularjs中显示highcharts

时间:2016-01-26 12:22:02

标签: javascript angularjs angularjs-directive highcharts

我试图在ng-repeat中显示图表 这是html:

<tr ng-repeat="perspective in perspectives">
   <td>
      <highcharts-pie class="hc-pie" items="value"></highcharts-pie>
   </td>
   <td>{{perspective.perfomance}}</td>
   <td class="right-align">{{perspective.current}}</td>
   <td class="right-align">{{perspective.previous}}</td>
   <td class="right-align isPluse-{{perspective.plus}}">{{perspective.variance}}</td>
</tr>

来自控制器的数组:

$scope.perspectives=[
  {
    perfomance:'a',
    current:'1',
    previous:'2',
    variance:'-1',
    plus:false,
    graphData:[
      {value: 32.4},
      {value: 13.2},
      {value: 84.5},
      {value: 19.7},
      {value: 22.6},
      {value: 65.5},
      {value: 77.4},
      {value: 90.4},
      {value: 17.6},
      {value: 59.1},
      {value: 76.8},
      {value: 21.1}
   ]
  }
];

这是指令:

.directive('hcPie', function () {
  return {
    restrict: 'C',
    replace: true,
    scope: {
      items: '='
    },
    controller: function ($scope, $element, $attrs) {
    },
    template: '<div id="container" style="margin: 0 auto">not working</div>',
    link: function (scope, element, attrs) {
      var chart = new Highcharts.Chart({
        chart: {
          renderTo: element[0],
          type: 'column',
          backgroundColor: null
        },
        title: {
          text: null
        },
        subtitle: {
          text: null
        },
        xAxis: {
          categories: [
            'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'Jul',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec'
          ],
          labels: {
            enabled: false
          },
          gridLineWidth: 0,
          minorGridLineWidth: 0,
          lineColor: 'transparent',
          tickLength: 0
        },
        yAxis: {
          gridLineWidth: 0,
          minorGridLineWidth: 0,
          lineColor: 'transparent',
          min: 0,
          title: {
            text: null
          },
          labels: {
            enabled: false
          }
        },
        tooltip: {
          enabled: false
        },
        plotOptions: {
          column: {
            pointPadding: 0,
            borderWidth: 0,
            states: {
              hover: {
                color: '#FFFFFF'
              }
            }
          }
        },
        legend: {
          enabled: false
        },
        series: [{
          name: 'Value',
          color: '#EC5B00',
          data: scope.items
        }]
      });
      scope.$watch("items", function (newValue) {
        chart.series[0].setData(newValue, true);
        console.log(scope.items);
      }, true);
    }
  }
});

我也试过

<highcharts-pie class="hc-pie" items="perspectives.graphData.value"></highcharts-pie>

控制台显示我需要的值, 但是我不能用HTML显示它 我的错误是什么?

1 个答案:

答案 0 :(得分:2)

您需要在使用之前对数据进行预处理,可以这样做:

  $scope.processed = $scope.perspectives[0].graphData.map(function(elem,i){
    return [i,elem.value];
  });

highcharts想要使用特定的系列格式来查看DOCS

&#13;
&#13;
var myApp = angular.module("myApp", []);


myApp.controller("myCtrl", function($scope) {
  $scope.perspectives = [{
    perfomance: 'a',
    current: '1',
    previous: '2',
    variance: '-1',
    plus: false,
    graphData: [{
      value: 32.4
    }, {
      value: 13.2
    }, {
      value: 84.5
    }, {
      value: 19.7
    }, {
      value: 22.6
    }, {
      value: 65.5
    }, {
      value: 77.4
    }, {
      value: 90.4
    }, {
      value: 17.6
    }, {
      value: 59.1
    }, {
      value: 76.8
    }, {
      value: 21.1
    }]
  }];
  
  $scope.processed = $scope.perspectives[0].graphData.map(function(elem,i){
  	return [i,elem.value];
  });
  
  
})

.directive('hcPie', function() {
  return {
    restrict: 'C',
    replace: true,
    scope: {
      items: '='
    },
    controller: function($scope, $element, $attrs) {},
    template: '<div id="container" style="margin: 0 auto">not working</div>',
    link: function(scope, element, attrs) {

      console.log(scope, element);
      var chart = new Highcharts.Chart({
        chart: {
          renderTo: element[0],
          type: 'column',
          backgroundColor: null
        },
        title: {
          text: null
        },
        subtitle: {
          text: null
        },
        xAxis: {
          categories: [
            'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'Jul',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec'
          ],
          labels: {
            enabled: false
          },
          gridLineWidth: 0,
          minorGridLineWidth: 0,
          lineColor: 'transparent',
          tickLength: 0
        },
        yAxis: {
          gridLineWidth: 0,
          minorGridLineWidth: 0,
          lineColor: 'transparent',
          min: 0,
          title: {
            text: null
          },
          labels: {
            enabled: false
          }
        },
        tooltip: {
          enabled: false
        },
        plotOptions: {
          column: {
            pointPadding: 0,
            borderWidth: 0,
            states: {
              hover: {
                color: '#FFFFFF'
              }
            }
          }
        },
        legend: {
          enabled: false
        },
        series: [{
          name: 'Value',
          color: '#EC5B00',
          data: scope.items
        }]
      });
      scope.$watch("items", function(newValue) {
        chart.series[0].setData(newValue, true);
        console.log(scope.items);
      }, true);
    }
  }
});
&#13;
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  <table>
    <tbody>
      <tr ng-repeat="perspective in perspectives">
        <td>
          <highcharts-pie class="hc-pie" items="processed"></highcharts-pie>
        </td>
        <td>{{perspective.perfomance}}</td>
        <td class="right-align">{{perspective.current}}</td>
        <td class="right-align">{{perspective.previous}}</td>
        <td class="right-align isPluse-{{perspective.plus}}">{{perspective.variance}}</td>
      </tr>
    </tbody>
  </table>
  {{processed}}
</body>
&#13;
&#13;
&#13;