-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-graphael.js
197 lines (183 loc) · 6.01 KB
/
angular-graphael.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @ngdoc module
* @name angular-graphael
* A set of AngularJS directives to make charts using gRaphaël.
* @link http://g.raphaeljs.com/
*/
angular.module('angular-graphael', []);
angular.module('angular-graphael').constant('mapData', function (target){
'use strict';
return Object.keys(target).map(function (key) {
return target[key];
});
});
/**
* @ngdoc directive
* @name barchart
* @module angular-graphael
* @description
* Creates a bar chart.
* @link http://g.raphaeljs.com/reference.html#Paper.barchart
*/
angular.module('angular-graphael').directive('barchart', ["$window", "mapData", function ($window, mapData) {
'use strict';
return {
restrict: 'E',
scope: {
width: '=',
height: '=',
x: '=',
y: '=',
options: '=',
values: '='
},
template: '<div></div>',
link: function (scope, element) {
// Right now we only care about watching if the data changes.
scope.$watch('values', function () {
var r,
// X coordinate
x = scope.x || 100,
// Y coordinate
y = scope.y || 100,
// Width
width = scope.width || 100,
// Height
height = scope.height || 100;
// If you don't remove the old chart, you're gonna have a bad time.
element[0].innerHTML = '';
// Set up the canvas
r = $window.Raphael(element[0]);
// Add the chart to the canvas with all of our options and data.
r.barchart(x, y, width, height, mapData(scope.values), scope.options);
});
}
};
}]);
/**
* @ngdoc directive
* @name dotchart
* @module angular-graphael
* @description
* Creates a scatter plot chart.
* @link http://g.raphaeljs.com/reference.html#Paper.dotchart
*/
angular.module('angular-graphael').directive('dotchart', ["$window", "mapData", function ($window, mapData) {
'use strict';
return {
restrict: 'E',
scope: {
valuesX: '=',
valuesY: '=',
size: '=',
options: '=',
x: '=',
y: '='
},
template: '<div></div>',
link: function (scope, element) {
// Right now we only care about watching if the data changes.
scope.$watch('size', function () {
var r,
// X coordinate
x = scope.x || 0,
// Y coordinate
y = scope.y || 0,
// Width
width = scope.width || 500,
// Height
height = scope.height || 80;
// If you don't remove the old chart, you're gonna have a bad time.
element[0].innerHTML = '';
// Set up the canvas
r = $window.Raphael(element[0]);
// Add the chart to the canvas with all of our options and data.
r.dotchart(x, y, width, height, scope.valuesX, scope.valuesY, mapData(scope.size), scope.options);
});
}
};
}]);
/**
* @ngdoc directive
* @name linechart
* @module angular-graphael
* @description
* Creates a line chart.
* @link http://g.raphaeljs.com/reference.html#Paper.linechart
*/
angular.module('angular-graphael').directive('linechart', ["$window", "mapData", function ($window, mapData) {
'use strict';
return {
restrict: 'E',
scope: {
width: '=',
height: '=',
x: '=',
y: '=',
options: '=',
valuesX: '=',
valuesY: '='
},
template: '<div></div>',
link: function (scope, element) {
// Right now we only care about watching if the data changes.
scope.$watch('values', function () {
var r,
// X coordinate
x = scope.x || 100,
// Y coordinate
y = scope.y || 100,
// Width
width = scope.width || 100,
// Height
height = scope.height || 100;
// If you don't remove the old chart, you're gonna have a bad time.
element[0].innerHTML = '';
// Set up the canvas
r = $window.Raphael(element[0]);
// Add the chart to the canvas with all of our options and data.
r.linechart(x, y, width, height, mapData(scope.valuesX), mapData(scope.valuesY), scope.options);
});
}
};
}]);
/**
* @ngdoc directive
* @name piechart
* @module angular-graphael
* @description
* Creates a pie chart.
* @link http://g.raphaeljs.com/reference.html#Paper.piechart
*/
angular.module('angular-graphael').directive('piechart', ["$window", "mapData", function ($window, mapData) {
'use strict';
return {
restrict: 'E',
scope: {
values: '=',
radius: '=',
cx: '=',
cy: '=',
options: '='
},
template: '<div></div>',
link: function (scope, element) {
// Right now we only care about watching if the data changes.
scope.$watch('values', function () {
var r,
// Center X
cx = scope.cx || 100,
// Center Y
cy = scope.cy || 100,
// Radius
rad = scope.radius || 100;
// If you don't remove the old chart, you're gonna have a bad time.
element[0].innerHTML = '';
// Set up the canvas
r = $window.Raphael(element[0]);
// Add the chart to the canvas with all of our options and data.
r.piechart(cx, cy, rad, mapData(scope.values), scope.options);
});
}
};
}]);