4

i am new to angular js. When i try to do the below code. It show some errors in console. ReferenceError: Chart is not defined .I am not able to understand the errors. So anybody please help me. And if the question is not correct , please correct the question

My Html code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>school</title>
        <link rel="stylesheet" href="style.css" type="text/css"/>
        <link rel="stylesheet" href="fonts/text-font/css/font_icon.css" type="text/css"/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0" />
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <!-- angular -->
        <script type="text/javascript" src="js/angular/angular.min.js"></script>
        <!-- angular chart -->
        <link rel="stylesheet" href="js/angular/angular-chart.css" type="text/css"/>
        <script type="text/javascript" src="js/angular/angular-chart.min.js"></script><br />
        <script type="text/javascript" src="js/angular/angular.js"></script>

        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body ng-app="app">
        <div ng-controller="LineCtrl">
                <canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels" 
                chart-legend="true" chart-series="series" chart-click="onClick"></canvas> 

            </div>
    </body >
</html>

my angular.js :

angular.module("app", ["chart.js"]) 
  // Optional configuration
  .config(['ChartJsProvider', function (ChartJsProvider) {
    // Configure all charts
    ChartJsProvider.setOptions({
      colours: ['#FF5252', '#FF8A80'],
      responsive: false
    });
    // Configure all line charts
    ChartJsProvider.setOptions('Line', {
      datasetFill: false
    });
  }])
  .controller("LineCtrl", ['$scope', '$timeout', function ($scope, $timeout) {

  $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
  $scope.series = ['Series A', 'Series B'];
  $scope.data = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90]
  ];
  $scope.onClick = function (points, evt) {
    console.log(points, evt);
  };

  // Simulate async data update
  $timeout(function () {
    $scope.data = [
      [28, 48, 40, 19, 86, 27, 90],
      [65, 59, 80, 81, 56, 55, 40]
    ];
  }, 3000);
}]);
Thameem
  • 3,426
  • 4
  • 26
  • 32
  • 1
    You are missing the ng-app="app" directive in the html, also the controller is not set in the html. Add ng-app="app" in the body, and wrap the canvas tag in a div with the ng-controller="LineCtrl" directive on it. – Anfelipe Oct 21 '15 at 03:10
  • thanks.i did that .but still it is not working @Anfelipe. what is that chart.js file in angular.module("app",["chart.js"]) – Thameem Oct 21 '15 at 03:15
  • 1
    that 'chart.js' is not a file, that's the name of the module you are injecting. – Anfelipe Oct 21 '15 at 03:19
  • @Anfelipe thanks. But its not working. – Thameem Oct 21 '15 at 03:23

5 Answers5

12

You need to add reference of chart.js library to make your chart working. As angular-chart.js internally uses Chart object of chart.js library.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • @Thameem Glad to help you.. Thanks :) – Pankaj Parkar Dec 10 '15 at 09:00
  • Okay, I am stuck on this issue as well, and I am sure I am missing something, if you add the package via NPM package manager, why would I have to include chart.js, the package doesnt already include it as a dependency? Wouldn't you think it would include it for ease of use?! – TGarrett May 25 '16 at 14:21
4

I put the corrections I mentioned in the comments, ng-app and ng-directives are missing.

index.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>school</title>
        <link rel="stylesheet" href=
  "http://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.css" type=
  "text/css" />
  <script src="http://cdn.rawgit.com/nnnick/Chart.js/master/Chart.min.js" type=
  "text/javascript">
</script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"
  type="text/javascript">
</script>
  <script src=
  "http://cdn.rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.js"
  type="text/javascript">
</script>

        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body ng-app="app">
      <div ng-controller="LineCtrl">
        <canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels" 
                    chart-legend="true" chart-series="series" chart-click="onClick"></canvas> 
        </div>
    </body>
</html>

script.js

angular.module("app", ["chart.js"]) // here i couldn't understand about chart.js fil
  // Optional configuration
  .config(['ChartJsProvider', function (ChartJsProvider) {
    // Configure all charts
    ChartJsProvider.setOptions({
      colours: ['#FF5252', '#FF8A80'],
      responsive: false
    });
    // Configure all line charts
    ChartJsProvider.setOptions('Line', {
      datasetFill: false
    });
  }])
  .controller("LineCtrl", ['$scope', '$timeout', function ($scope, $timeout) {

  $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
  $scope.series = ['Series A', 'Series B'];
  $scope.data = [
    [65, 59, 80, 81, 56, 55, 40],
    [28, 48, 40, 19, 86, 27, 90]
  ];
  $scope.onClick = function (points, evt) {
    console.log(points, evt);
  };

  // Simulate async data update
  $timeout(function () {
    $scope.data = [
      [28, 48, 40, 19, 86, 27, 90],
      [65, 59, 80, 81, 56, 55, 40]
    ];
  }, 3000);
}]);

I made a jsbin for it.

Anfelipe
  • 712
  • 10
  • 20
  • can you please see my question maybe you would have an idea http://stackoverflow.com/questions/37256487/i-want-to-change-the-chart-for-each-city-or-subcity-selected – Abderrahim May 17 '16 at 14:27
1

That could be a matter of order of the chart.min.js and angular-chart.min.js files.In fact angular-chart.min.js needs chart.min.js dependency. angular-chart.min.js checks for chart.min.js and this generates errors if chart.min.js is placed after.

<script src="/lib/chart.js/dist/Chart.min.js"></script>
<script src="/lib/angular-chart.js/dist/angular-chart.min.js"></script>
edkeveked
  • 17,989
  • 10
  • 55
  • 93
0

I had a similar problem (same error), after following the Angular Chart official documentation (http://jtblin.github.io/angular-chart.js/). In my case, the only thing that was missing (and isn't mentioned there) is the reference to "Chart,js" in "index.html".

<script src="bower_components/Chart.js/Chart.min.js"></script>
Fernando Ghisi
  • 419
  • 4
  • 8
0

This worked for me:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.11/angular.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>

<script src="http://cdn.jsdelivr.net/angular.chartjs/latest/angular-chart.min.js"></script>
Oscar2av
  • 41
  • 2