显示多个图表angular.js chart.js

时间:2015-11-13 15:28:48

标签: javascript angularjs chart.js

我想在一个页面中显示多个图表,但只显示一个(第一个,"批次")。

在HTML上:

<div ng-app="app" ng-controller="lots">
    <h3>Comparatif des lots en volume</h3>
        <canvas id="doughnut" class="chart chart-doughnut" chart-data="data" chart-labels="labels" height="100px" chart-legend="legend"></canvas>           
</div>
<div ng-app="app2" ng-controller="repartition">
    <h3>Répartition des différents types</h3>
        <canvas id="doughnut2" class="chart chart-doughnut" chart-data="data" chart-labels="labels" height="100px" chart-legend="legend"></canvas> 
</div>

剧本:

<script>
    angular.module("app", ["chart.js"]).controller("lots", function ($scope) {
      $scope.labels = ["Non lotis", "Lot 1", "Lot 2"];
      $scope.data = [90, 5, 5];
    });
    angular.module("app2", ["chart.js"]).controller("repartition", function ($scope) {
      $scope.labels = ["JCL", "Appli", "Copy", "Mapset", "PGM"];
      $scope.data = [80, 2, 3, 0.5, 10];
    });
</script>

如何同时显示两者?

1 个答案:

答案 0 :(得分:1)

根据AngularJS网站,每个HTML文档只能自动引导一个AngularJS应用程序。 Ng-app API document

要修复您的问题,您只允许为一个html文档安装一个ng-app。在这种情况下,您要在ng-app = "app"代码或body代码中声明html。然后将控制器分配给不同的div标记。

HTML
<body ng-app="app">
  <div ng-controller="lots">
    <h3>Comparatif des lots en volume</h3>
    <canvas id="doughnut" class="chart chart-doughnut"
            chart-data="data" chart-labels="labels" height="100px" 
            chart-legend="legend">
    </canvas>           
  </div>
  <div ng-controller="repartition">
    <h3>Répartition des différents types</h3>
    <canvas id="doughnut2" class="chart chart-doughnut" 
            chart-data="data" chart-labels="labels" height="100px" 
            chart-legend="legend">
    </canvas> 
  </div>
</body>
JS
angular.module("app", ["chart.js"])
       .controller("lots", function ($scope) {
           $scope.labels = ["Non lotis", "Lot 1", "Lot 2"];
           $scope.data = [90, 5, 5];
        })
       .controller("repartition", function ($scope) {
           $scope.labels = ["JCL", "Appli", "Copy", "Mapset", "PGM"];
           $scope.data = [80, 2, 3, 0.5, 10];
        });