I want to implement a Chart of a Sharepoint tracker list.
I decided to use angular-chart.js, to use this code I also needed Charter.js. So I downloaded both and decided to make a try as one of the examples in their page.
They have a bar chart with a code like this:
<script>
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]
];
});
</script>
<canvas id="bar" class="chart chart-bar"
chart-data="data" chart-labels="labels"> chart-series="series"
</canvas>
So I decided to make a similar code and I customize it a little, my Sharepoint list have these columns: Title, Number Actions, Section.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.1.4.min.js"></script>
<script src="angular.js"></script>
<script src="Chart.js"></script>
<script src="angular-chart.min.js"></script>
<script>
var myApp = angular.module("myApp", ["chart.js"]);
myApp.controller("myController", function($scope, $http) {
//Here's the code to call the Sharepoint List
$http({
method:'GET',
url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('My Tracker')/items?$select=*,LinkTitle",
headers: { "Accept": "application/json; odata=verbose" }
}).success(function(data, status, headers, config) {
$scope.dataResults = data.d.results;
}).error(function(data, status, headers, config) {
alert("Error! The Tracker Chart can't be loaded.");
});
//These are the chart settings
$scope.labels = dataResults.LinkTitle
$scope.series = [
'Number Actions',
'Section'
];
$scope.data = [
dataResults.Number_x0020_Actions,
dataResults.Section
];
});
</script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<canvas id="bar" class="chart chart-bar"
chart-data="data"
chart-labels="labels"
chart-series="series">
</canvas>
</div>
</body>
</html>
Obviously this is not working, because I don't know what I'm doing. But I would like to know how can I link the chart with my sharepoint list, because in every site I visit when people ask how to make a chart with angularJS they write the data already in the code, but I need to read that data directly from the list.
Please help c:
EDIT:
This is my final code, I hope it can help somebody:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.1.4.min.js"></script>
<script src="angular.js"></script>
<script src="Chart.js"></script>
<script src="angular-chart.min.js"></script>
<script>
var myApp = angular.module("myApp", ["chart.js"]).controller("myController", function($scope, $http) {
$http({
method:'GET',
url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('My Tracker')/items?$select=*,LinkTitle",
headers: { "Accept": "application/json; odata=verbose" }
}).success(function(data, status, headers, config) {
$scope.dataResults = data.d.results;
var dataR = $scope.dataResults, arrayLabels = [], arrayNumberActions = [], arraySections = [];
for(var i = 0 ; i< dataR.length; i++){
var currentItem = dataR[i];
arrayLabels .push(currentItem.LinkTitle);
arrayNumberActions.push(currentItem.Number_x0020_Actions);
arraySections.push(currentItem.Section);
}
$scope.labels = arrayLabels;
$scope.series = ['Number Actions', 'Section'];
$scope.data = [
arrayNumberActions,
arraySections
];
}).error(function(data, status, headers, config) {
alert("Error! The Tracker Chart can't be loaded.");
});
});
</script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<canvas id="bar" class="chart chart-bar"
chart-data="data" chart-labels="labels" chart-series="series">
</canvas>
</div>
</body>
</html>