Angular-Chart.js它不显示图表

时间:2016-05-10 15:44:31

标签: javascript angularjs chart.js angular-chart

我正在尝试使用Angular-chart.js 它没有为我显示任何东西这里是我的javascript和html页面

(function(){
 angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
    $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
    $scope.series = ['Series A', 'Series B'];

    $scope.data = [
      [65, 59, 80, 81, 56, 55, 40],
      [28, 48, 40, 19, 86, 27, 90]
    ];
 });
})(); 
    
<!DOCTYPE html>
<html>

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.2/Chart.min.js
    "></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/angular.chartjs/latest/angular-chart.css
    " />
    <script src="angular-chart.min.js
    "></script>
    <script src="app.js"></script>
</head>

<body ng-app="app">
    <div ng-controller="BarCtrl">
        <canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels"></canvas>
    </div>

</body>

</html>

我不明白错误在哪里, 在此先感谢您的帮助

4 个答案:

答案 0 :(得分:3)

看起来angular-chart.js不支持Chart.js的第2版。下面是一个片段,显示了一个工作图表。它包括chart.js的v1.1.1

angular.module("app", ["chart.js"]).controller("BarCtrl", function($scope) {
  $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
  $scope.series = ['Series A', 'Series B'];

  $scope.data = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90]
  ];
});
<!DOCTYPE html>
<html>

<head>
  <script data-require="angularjs@1.5.3" data-semver="1.5.3" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.1.1/Chart.min.js"></script>
  <script src="http://jtblin.github.io/angular-chart.js/dist/angular-chart.js"></script>
  <link rel="stylesheet" href="https://raw.githubusercontent.com/jtblin/angular-chart.js/master/dist/angular-chart.min.css" />
  <script src="app.js"></script>
</head>

<body ng-app="app">
  <div ng-controller="BarCtrl">
    <canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series"></canvas>
  </div>
</body>

</html>

答案 1 :(得分:0)

您的数据绑定错误

<canvas id="bar" class="chart chart-bar" data="chart-data" labels="chart-labels" ></canvas>

您的控制器中没有可变图表数据或图表标签。你以不同的方式命名它们。

实际上,属性的顺序相反。应该是chart-data =“data”chart-labels =“labels”

来自http://jtblin.github.io/angular-chart.js/

的示例
<canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series"></canvas>

答案 2 :(得分:0)

在html data =&#34; chart-data&#34;将是chart-data =&#34;数据&#34;

答案 3 :(得分:0)

尝试使用代码时出现此错误:

(intermediate value)[e] is not a function angular chart

似乎有一个版本不匹配,尝试与我自己的项目相同的错误。 请参阅Chart.js的V1.1.1,正如@dustin已经回答的那样。

选中fiddle