2015-05-15 91 views
2

我在這裏錯過了什麼嗎?兩天來,我仔細查看了這段代碼,看看剛開始的問題。我正在測試Chart Js插件,我打算使用它。圖表Js + Angular

似乎有一些與我的代碼衝突,dosent讓任何腳本運行。請幫助

//Charts// 
// Get the context of the canvas element we want to select 
var ctx = document.getElementById("myChart").getContext("2d"); 
var LineChart = new Chart(ctx).Line(data); 

//Data// 
data = { 
    labels : ["January", "February", "March", "April", "May", "June", "July"], 
    datasets : [{ 
    data:[65, 59, 80, 81, 56, 55, 40] 
    }] 
}; 

var app = angular.module('myApp',[]); 
app.controller('MainController', function($scope){ 
    $scope.Title = 'My Charts'; 
}); 

http://plnkr.co/edit/RXzjPllg0RSWjvgMjIoU?p=preview

回答

3

有你的圖表,你

http://plnkr.co/edit/YrI6RtQTkJkZde3Ynnhq?p=preview

var app = angular.module('Chart', []); 

    app.controller('MainController', ['$scope', function($scope) { 

     $scope.greet = 'My Charts'; 

     data = { 
      labels: ["January", "February", "March", "April", "May", "June", "July"], 
      datasets: [{ 
       data: [65, 59, 80, 81, 56, 55, 40] 
      }] 
     }; 

     var ctx = document.getElementById("myChart").getContext("2d"); 
     var LineChart = new Chart(ctx).Line(data); 

    }]); 

    //Data// 
+0

感謝@Yuujin,必須爲聊天代碼放在內MainController? –

+0

沒問題,將數據放入工廠/服務可能會更好,並且在應用程序啓動時在.run函數中添加var ctx/LineChart –

1

在這裏工作是爲我工作的例子。 在page.html中......

<!-- in head tag include: jquery.js jquery-ui.js bootstrap.js angular.js chart.js angular-chart.js angular-chart.css then this js --> 

    <script type="text/javascript"> 
     var app = angular.module("app", ["chart.js"]).controller("LineCtrl", function ($scope, $http) {  
      // get example [{"January":10},{"February":20},{"March":50},{"April":30},{"May":20},{"June":10},{"July":10}] 
     $http.get('http://mywebsite/myjsondata'). 
      success(function(data) { 
       var labelArray = []; 
       var dataArray = [];     
     angular.forEach(data, function(item){ 
      var jsObj = angular.fromJson(item); 
      for (var prop in jsObj) { 
       //alert(prop + " is " + jsObj[prop]); 
       labelArray.push(prop); 
      dataArray.push(parseInt(jsObj[prop])); 
      } 
     }); 

     //$scope.labels = ["January", "February", "March", "April", "May", "June", "July"]; 
     // $scope.data = [[10, 20, 50, 30, 20, 10, 10]]; 
     $scope.labels = labelArray;      
     $scope.data = [dataArray]; 
       $scope.series = ['Send Totals']; // Series A 
      }) 
      .error(function() { 
     alert("Sorry. Unable to retrieve the data.");  
     }); 
    </script> 

...在身體把這個

<div class="col-lg-6 col-sm-12 ng-scope" id="line-chart" ng-controller="LineCtrl"> 
      <div class="panel panel-default"> 
      <div class="panel-heading">Line Chart</div> 
      <div class="panel-body"> 
     <canvas id="line" class="chart chart-line" chart-data="data" 
     chart-labels="labels" chart-legend="true" chart-series="series" 
     chart-click="onClick" > 
     </canvas> 

     </div> 
     </div> 
    </div>   
+0

感謝這個答案,它節省了我的一天。 –