forked from angular-google-chart/angular-google-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-google-chart.js
62 lines (56 loc) · 2.58 KB
/
ng-google-chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @description Google Chart Api Directive Module for AngularJS
* @version 0.1
* @author Nicolas Bouillon <[email protected]>
* @license MIT
* @year 2013
*/
(function () {
'use strict';
angular.module('googlechart.directives', []).directive('googleChart', function ($timeout) {
return {
restrict: 'A',
scope: {
chart: '=chart'
},
link: function ($scope, $elm, $attr) {
// Watches, to refresh the chart when its data, title or dimensions change
$scope.$watch('chart', function () {
draw();
}, true); // true is for deep object equality checking
function draw() {
if (!draw.triggered && ($scope.chart != undefined)) {
draw.triggered = true;
$timeout(function () {
draw.triggered = false;
var dataTable = new google.visualization.DataTable($scope.chart.data, 0.5);
var chartWrapperArgs = {
chartType: $scope.chart.type,
dataTable: dataTable,
options: $scope.chart.options,
containerId: $elm[0]
};
if($scope.chartWrapper==null) {
$scope.chartWrapper = new google.visualization.ChartWrapper(chartWrapperArgs);
google.visualization.events.addListener($scope.chartWrapper, 'ready', function () {
$scope.chart.displayed = true;
});
google.visualization.events.addListener($scope.chartWrapper, 'error', function (err) {
console.log("Chart not displayed due to error: " + err.message);
});
}
else {
$scope.chartWrapper.setChartType($scope.chart.type);
$scope.chartWrapper.setDataTable(dataTable);
$scope.chartWrapper.setOptions($scope.chart.options);
}
$timeout(function () {
$scope.chartWrapper.draw();
});
}, 0, true);
}
}
}
};
});
})();