-
Notifications
You must be signed in to change notification settings - Fork 23
/
rickshaw.js
281 lines (248 loc) · 12.1 KB
/
rickshaw.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
@toc
@param {Object} scope (attrs that must be defined on the scope (i.e. in the controller) - they can't just be defined in the partial html). REMEMBER: use snake-case when setting these on the partial!
TODO
@param {Object} attrs REMEMBER: use snake-case when setting these on the partial! i.e. my-attr='1' NOT myAttr='1'
TODO
@dependencies
TODO
@usage
partial / html:
TODO
controller / js:
TODO
//end: usage
*/
'use strict';
/* global Rickshaw */
angular.module('angular-rickshaw', [])
.directive('rickshaw', ['$compile', '$window', function($compile, $window) {
return {
restrict: 'EA',
scope: {
options: '=rickshawOptions',
series: '=rickshawSeries',
features: '=rickshawFeatures'
},
// replace: true,
link: function(scope, element, attrs) {
var mainEl;
var graphEl;
var legendEl;
var previewEl;
var xAxis;
var yAxis;
var graph;
var settings;
function redraw() {
graph.setSize();
graph.render();
}
function _splice(args) {
var data = args.data;
var series = args.series;
if (!args.series) {
return data;
}
series.forEach(function(s) {
var seriesKey = s.key || s.name;
if (!seriesKey) {
throw "series needs a key or a name";
}
data.forEach(function(d) {
var dataKey = d.key || d.name;
if (!dataKey) {
throw "data needs a key or a name";
}
if (seriesKey == dataKey) {
var properties = ['color', 'name', 'data'];
properties.forEach(function(p) {
if (d[p]) {
s[p] = d[p];
}
});
}
} );
});
}
function updateData() {
if (graph && settings) {
_splice({ data: scope.series, series: settings.series });
redraw();
}
}
function updateConfiguration() {
if (!graph) {
mainEl = angular.element(element);
mainEl.append(graphEl);
mainEl.empty();
graphEl = $compile('<div></div>')(scope);
mainEl.append(graphEl);
settings = angular.copy(scope.options);
settings.element = graphEl[0];
settings.series = scope.series;
graph = new Rickshaw.Graph(settings);
}
else {
if (scope.options) {
for (var key in scope.options) {
settings[key] = scope.options[key];
console.log(key + '=' + scope.options[key]);
}
settings.element = graphEl[0];
}
graph.configure(settings);
}
if (scope.features) {
if (scope.features.hover) {
var hoverConfig = {
graph: graph
};
hoverConfig.xFormatter = scope.features.hover.xFormatter;
hoverConfig.yFormatter = scope.features.hover.yFormatter;
hoverConfig.formatter = scope.features.hover.formatter;
var hoverDetail = new Rickshaw.Graph.HoverDetail(hoverConfig);
}
if (scope.features.palette) {
var palette = new Rickshaw.Color.Palette({scheme: scope.features.palette});
for (var i = 0; i < settings.series.length; i++) {
settings.series[i].color = palette.color();
}
}
}
redraw();
if (scope.features) {
if (scope.features.xAxis) {
if (!xAxis) {
var xAxisConfig = {
graph: graph
};
if (scope.features.xAxis.timeUnit) {
var time = new Rickshaw.Fixtures.Time();
xAxisConfig.timeUnit = time.unit(scope.features.xAxis.timeUnit);
}
if (scope.features.xAxis.tickFormat) {
xAxisConfig.tickFormat = scope.features.xAxis.tickFormat;
}
if (scope.features.xAxis.ticksTreatment) {
xAxisConfig.ticksTreatment = scope.features.xAxis.ticksTreatment;
}
if (scope.features.xAxis.time) {
if (scope.features.xAxis.time.local) {
xAxisConfig.timeFixture = new Rickshaw.Fixtures.Time.Local();
}
xAxis = new Rickshaw.Graph.Axis.Time(xAxisConfig);
}
else {
xAxis = new Rickshaw.Graph.Axis.X(xAxisConfig);
}
xAxis.render();
}
else {
// Update xAxis if Rickshaw allows it in future.
}
}
else {
// Remove xAxis if Rickshaw allows it in future.
}
if (scope.features.yAxis) {
var yAxisConfig = {
graph: graph
};
if (scope.features.yAxis.tickFormat) {
var tickFormat = scope.features.yAxis.tickFormat;
if (typeof tickFormat === 'string'){
yAxisConfig.tickFormat = Rickshaw.Fixtures.Number[tickFormat];
} else {
yAxisConfig.tickFormat = tickFormat;
}
}
if (!yAxis) {
yAxis = new Rickshaw.Graph.Axis.Y(yAxisConfig);
yAxis.render();
}
else {
// Update yAxis if Rickshaw allows it in future.
}
}
else {
// Remove yAxis if Rickshaw allows it in future.
}
if (scope.features.legend) {
if (!legendEl) {
legendEl = $compile('<div></div>')(scope);
mainEl.append(legendEl);
var legend = new Rickshaw.Graph.Legend({
graph: graph,
element: legendEl[0]
});
if (scope.features.legend.toggle) {
var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
graph: graph,
legend: legend
});
}
if (scope.features.legend.highlight) {
var highlighter = new Rickshaw.Graph.Behavior.Series.Highlight({
graph: graph,
legend: legend
});
}
}
}
else {
if (legendEl) {
legendEl.remove();
legendEl = null;
}
}
if (scope.features.preview) {
if (!previewEl) {
previewEl = $compile('<div></div>')(scope);
mainEl.append(previewEl);
new Rickshaw.Graph.RangeSlider.Preview({
graph: graph,
element: previewEl[0]
});
}
}
else {
if (previewEl) {
previewEl.remove();
previewEl = null;
}
}
}
}
var optionsWatch = scope.$watch('options', function(newValue, oldValue) {
if (!angular.equals(newValue, oldValue)) {
updateConfiguration();
}
}, true);
var seriesWatch = scope.$watchCollection('series', function(newValue, oldValue) {
if (!angular.equals(newValue, oldValue)) {
updateData();
}
}, true);
var featuresWatch = scope.$watch('features', function(newValue, oldValue) {
if (!angular.equals(newValue, oldValue)) {
updateConfiguration();
}
}, true);
scope.$on('$destroy', function() {
optionsWatch();
seriesWatch();
featuresWatch();
});
angular.element($window).on('resize', function() {
scope.$broadcast('rickshaw::resize');
});
scope.$on('rickshaw::resize', function() {
redraw();
});
updateConfiguration();
},
controller: ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
}]
};
}]);