我试图给出正确的参数以使用ng-repeat绘制图表:
这是HTML:

<tr ng-repeat="perspective in perspectives">
            <td>
                <div class="hc-pie" items="values"></div>
            </td>
            <td>{{perspective.perfomance}}</td>
            <td>{{perspective.current}}</td>
            <td>{{perspective.previous}}</td>
            <td>{{perspective.variance}}</td>
</tr>


和控制器:

$scope.perspectives=[
  {perfomance:'AAA',
    current:'a',
    previous:'b',
    variance:'c',
    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);
      }, true);
    }
  }
});


控制台没有任何错误,但我无法显示图表
我误会了什么?

最佳答案

更新请参见working fiddle here

有两个错误。首先是:您的阵列无法正常关闭。第二个是:您需要使用hc_pie的div中的Perspective.graphData值代替。
在控制器中:

$scope.perspectives=[
                     {perfomance:'AAA',
                       current:'a',
                       previous:'b',
                       variance:'c',
                       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]
                           ]
                     }
                     ];


并在html中使用以下

<table><tr ng-repeat="perspective in perspectives">
        <td>
            <div class="hc-pie" items="perspective.graphData"></div>
        </td>
        <td>{{perspective.perfomance}}</td>
        <td>{{perspective.current}}</td>
        <td>{{perspective.previous}}</td>
        <td>{{perspective.variance}}</td>
</tr></table>

关于javascript - 从指令angularjs给highchart正确的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35009960/

10-12 13:35