首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ng-重复的angularjs中显示图表

如何在ng-重复的angularjs中显示图表
EN

Stack Overflow用户
提问于 2016-01-26 12:22:01
回答 1查看 1.3K关注 0票数 4

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

代码语言:javascript
复制
<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>

以及来自控制器的数组:

代码语言:javascript
复制
$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}
   ]
  }
];

以下是指令:

代码语言:javascript
复制
.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);
    }
  }
});

我也试过

代码语言:javascript
复制
<highcharts-pie class="hc-pie" items="perspectives.graphData.value"></highcharts-pie>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-26 14:04:05

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

代码语言:javascript
复制
  $scope.processed = $scope.perspectives[0].graphData.map(function(elem,i){
    return [i,elem.value];
  });

高级图表需要一种特定的系列格式来查看文档

代码语言:javascript
复制
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);
    }
  }
});
代码语言:javascript
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35013764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档