简体   繁体   English

Angularjs和Chart.js最小示例

[英]Angularjs and Chart.js minimal example

I'm using angular and charts.js to create dynamic charts, but the graphics always show empty, there is any minimal example on how to use both of them? 我使用angular和charts.js创建动态图表,但是图形始终显示为空,如何使用这两个图表有一个最小的示例?

here a plunker: http://plnkr.co/edit/V1gIaCIAixQTcHtBj9YN?p=preview i'm still learning angular, so i'm not sure what i'm doing wrong. 这里是个笨蛋: http ://plnkr.co/edit/V1gIaCIAixQTcHtBj9YN?p=preview我仍在学习有角度的知识,所以我不确定我在做什么错。

The non working code: 非工作代码:

 <div ng-repeat="e in encuesta.cuestionario">
          <!--This doesn't works!-->
          <div id="canvas-holder" class="col-sm-4">
            <canvas id="{{'chart-area-'+$index}}" width="250" height="250" />
          </div>
          <div class="col-sm-6 formulario">
            <ul>
              <li>
                <p>{{e.pregunta}}</p>
              </li>
            </ul>
          </div>

        </div>

Here is a fairly simple take on your code: http://plnkr.co/edit/RvtPAcV4JKDkzikeSamM?p=preview 这是您的代码相当简单的方法: http : //plnkr.co/edit/RvtPAcV4JKDkzikeSamM?p=preview

I build the charts into a scope array based on the cuestionario length. 我根据cuestionario长度将图表构建到范围数组中。

for (i = 1; i < $scope.encuesta.cuestionario.length; i++) {
    $scope.charts.push({
        chartId: i
    });
}

Then render the charts inside a timeout to let digest complete: 然后在超时内渲染图表以使摘要完成:

function loadCharts() {
    $timeout(function() {
      angular.forEach($scope.charts, function(chart) {
        var ctx = angular.element(document.getElementById("chart-area-" + chart.chartId))[0].getContext("2d");
        window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {
          responsive: true
        });
      });
    });
  }

The other big thing to note is that we access the element as an array, "[0]": 另一个需要注意的重要事情是我们将元素作为数组“ [0]”进行访问:

var ctx = angular.element(document.getElementById("chart-area-" + chart.chartId))[0].getContext("2d");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM