-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleChartDirective.js
165 lines (141 loc) · 7.05 KB
/
googleChartDirective.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @description Google Chart Api Directive Module for AngularJS
* @version 0.1
* @author Nicolas Bouillon <nicolas@bouil.org>
* @license MIT
* @year 2013
*/
(function () {
'use strict';
angular.module('googlechart.directives', []).directive('googleChart', ['$timeout', '$window', function ($timeout, $window) {
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
// Redraw the chart if the window is resized
angular.element($window).bind('resize', function () {
draw();
});
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,
view: $scope.chart.view,
options: $scope.chart.options,
containerId: $elm[0]
};
if($scope.chartWrapper==null) {
$scope.chartWrapper = new google.visualization.ChartWrapper(chartWrapperArgs);
$scope.readyListener = google.visualization.events.addListener($scope.chartWrapper, 'ready', function () {
$scope.chart.displayed = true;
setUpListeners($scope);
/**
* Callbacks for chart ready
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.ready !== "undefined"){
$scope.chart.methods.ready();
}
});
$scope.readyListener = google.visualization.events.addListener($scope.chartWrapper, 'error', function (err) {
console.log("Chart not displayed due to error: " + err.message);
/**
* Callbacks for chart error
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.error !== "undefined"){
$scope.chart.methods.error(err);
}
});
}
else {
$scope.chartWrapper.setChartType($scope.chart.type);
$scope.chartWrapper.setDataTable(dataTable);
$scope.chartWrapper.setView($scope.chart.view);
$scope.chartWrapper.setOptions($scope.chart.options);
setUpListeners($scope);
}
$timeout(function () {
$scope.chartWrapper.draw();
});
}, 0, true);
}
}
}
};
}]);
function setUpListeners($scope){
/**
* Reset all the listeners.
*/
if(typeof $scope.onMouseOutListener !== "undefined"){
google.visualization.events.removeListener($scope.onMouseOutListener);
delete $scope.onMouseOutListener;
}
if(typeof $scope.onMouseOverListener !== "undefined"){
google.visualization.events.removeListener($scope.onMouseOverListener);
delete $scope.onMouseOverLisstener;
}
if(typeof $scope.selectListener !== "undefined"){
google.visualization.events.removeListener($scope.selectListener);
delete $scope.selectListener;
}
if(typeof $scope.animationFinishListener !== "undefined"){
google.visualization.events.removeListener($scope.animationFinishListener);
delete $scope.animationFinishListener;
}
/**
* Callbacks for Selection in chart
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.select !== "undefined"){
$scope.selectListener = google.visualization.events.addListener($scope.chartWrapper, "select", function (event) {
$scope.chart.methods.select($scope.chartWrapper.getChart().getSelection(), event);
if (!$scope.$$phase){
$scope.$apply();
}
});
}
/**
* Callbacks for onmouseover in chart
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.onmouseover !== "undefined" && typeof $scope.chartWrapper.getChart() !== "undefined" && $scope.chartWrapper.getChart()!== null){
$scope.onMouseOverListener =google.visualization.events.addListener($scope.chartWrapper.getChart(), "onmouseover", function (event) {
$scope.chart.methods.onmouseover(event);
if (!$scope.$$phase){
$scope.$apply();
}
});
}
/**
* Callbacks for onmouseout in chart
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.onmouseout !== "undefined" && typeof $scope.chartWrapper.getChart() !== "undefined" && $scope.chartWrapper.getChart()!== null){
$scope.onMouseOutListener = google.visualization.events.addListener($scope.chartWrapper.getChart(), "onmouseout", function (event) {
$scope.chart.methods.onmouseout(event);
if (!$scope.$$phase){
$scope.$apply();
}
});
}
/**
* Callbacks for animationFinish in chart
*/
if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.animationfinish !== "undefined" && typeof $scope.chartWrapper.getChart() !== "undefined" && $scope.chartWrapper.getChart()!== null){
$scope.animationFinishListener =google.visualization.events.addListener($scope.chartWrapper.getChart(), "animationfinish", function (event) {
$scope.chart.methods.animationfinish(event);
if (!$scope.$$phase){
$scope.$apply();
}
});
}
}
})();