-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-c3.js
302 lines (265 loc) · 7.59 KB
/
angular-c3.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
(function() {
'use strict';
angular.module('c3', [])
.directive('c3', function() {
function getKeys(src, includeXs) {
if (!src) {
return false;
}
var xs = {};
if (src.hasOwnProperty('x')) {
xs[src.x] = true;
} else if (src.xs && src.xs.length) {
angular.forEach(src.xs, function(x) {
xs[x] = true;
});
}
var keys;
if (src.columns && src.columns.length) {
keys = {};
src.columns.forEach(function(data) {
if (!xs[data[0]] || includeXs) {
keys[data[0]] = true;
}
});
return keys;
} else if (src.rows && src.rows.length) {
keys = {};
src.rows[0].forEach(function(key) {
if (!xs[key] || includeXs) {
keys[key] = true;
}
});
return keys;
} else if (src.keys) {
keys = {};
if (src.keys.hasOwnProperty('x') && includeXs) {
keys[src.keys.x] = true;
}
if (src.keys.value && src.keys.value.length) {
src.keys.value.forEach(function(key) {
keys[key] = true;
});
}
return keys;
} else {
return false;
}
}
function getUnload(prev, curr) {
var prevKeys = getKeys(prev, true);
if (!prevKeys) {
return false;
}
var currKeys = getKeys(curr, true);
if (!currKeys) {
return Object.keys(prevKeys);
}
var delta = [];
Object.keys(prevKeys).forEach(function(key) {
if (!currKeys.hasOwnProperty(key)) {
delta.push(key);
}
});
return (delta.length ? delta : false);
}
function doHide(chart, data) {
// TODO: handle boolean data.hide
var keys = getKeys(data);
if (!keys) {
return;
}
keys = Object.keys(keys);
var hidden = [];
var shown = [];
if (!data.hide || !data.hide.length) {
keys.forEach(function(key) { shown.push(key); });
} else {
keys.forEach(function(key) {
if (data.hide.indexOf(key) >= 0) {
hidden.push(key);
} else {
shown.push(key);
}
});
}
if (hidden.length > 0) {
chart.hide(hidden);
}
if (shown.length > 0) {
chart.show(shown);
}
// Not exactly sure why this is needed, but without it,
// you have legend items stay focused after a hover
chart.revert();
}
function toggleHide(id, data) {
// TODO: handle boolean data.hide
if (data.hide) {
var idx = data.hide.indexOf(id);
if (idx >= 0) {
data.hide.splice(idx, 1);
} else {
data.hide.push(id);
}
} else {
data.hide = [id];
}
}
function hideAllBut(id, data) {
// TODO: handle boolean data.hide
var keys = getKeys(data);
if (!keys) {
return;
}
if (keys.hasOwnProperty(id)) {
delete keys[id];
}
if (data.hide) {
data.hide.length = 0;
Object.keys(keys).forEach(function(key) {
data.hide.push(key);
});
} else {
data.hide = Object.keys(keys);
}
}
// Properties that can be passed into chart.load() API ('hide' is included
// because we call doHide() in the done callback of load)
var loadableDataProps = {
hide: true, url: true, columns: true, rows: true, json: true, classes: true,
categories: true, axes: true, colors: true, type: true, types: true,
};
function partitionConfig(config) {
var result = { loadableData: {}, unloadableData: {}, nonData: {} };
if (config) {
angular.forEach(config, function(value, prop) {
if (prop !== 'data') {
result.nonData[prop] = value;
}
});
if (config.data) {
angular.forEach(config.data, function(value, prop) {
if (loadableDataProps.hasOwnProperty(prop)) {
result.loadableData[prop] = value;
} else {
result.unloadableData[prop] = value;
}
});
}
}
return result;
}
// return false if can't/shouldn't update the chart using the load API;
// otherwise, return an object that can be passed into the .load() call
function getLoadParam(options, prevConfig, currConfig) {
if (!!options && !options.useLoadApi) {
return false;
} else {
var prev = partitionConfig(prevConfig);
var curr = partitionConfig(currConfig);
// NOTE: no need to compare .loadableData because we're already deep
// watching the configs, so the loadableData's must be non-equal
if ( angular.equals(prev.nonData, curr.nonData) &&
angular.equals(prev.unloadableData, curr.unloadableData)) {
var unload = getUnload(prev.loadableData, curr.loadableData);
if (unload) {
angular.extend(curr.loadableData, {unload: unload});
}
return curr.loadableData;
} else {
return false;
}
}
}
function decoratedConfig(scope, elem, attrs) {
var config = angular.merge({}, scope.c3, {
data: {
onclick: function(d, elem) {
var event = this.internal.d3.event;
// use applyAsync to give c3 a chance to finish what it's doing;
// if we don't, strange things happen, as collections being iterated
// by d3 get modified mid-iteration
scope.$applyAsync(function() {
if (scope.dataClick) {
scope.dataClick({ datum: d, event: event });
}
});
}
},
legend: {
item: {
onclick: function(id) {
var event = this.d3.event;
// use applyAsync to give c3 a chance to finish what it's doing;
// if we don't, strange things happen, as collections being iterated
// by d3 get modified mid-iteration
scope.$applyAsync(function() {
function defaultClick() {
if (event.altKey) {
hideAllBut(id, scope.c3.data);
} else {
toggleHide(id, scope.c3.data);
}
}
if (angular.isDefined(attrs.c3LegendClick)) {
scope.legendClick({ id: id, event: event, default: defaultClick });
} else {
defaultClick();
}
});
}
}
}
});
// angular.merge doesn't work well with HTMLElement so we need to set it
// by reference ourselves.
config.bindto = elem;
return config;
}
return {
restrict: 'A',
scope: {
c3: '=c3',
options: '=?c3Options',
dataClick: '&c3DataClick',
legendClick: '&c3LegendClick'
},
link: function(scope, elem, attrs) {
var chart;
scope.$watch('c3', function(value, prevValue) {
if (value) {
var loadParam;
if (!chart) {
chart = c3.generate(decoratedConfig(scope, elem[0], attrs));
} else if (loadParam = getLoadParam(scope.options, prevValue, value)) {
angular.extend(loadParam, {
done: function() {
doHide(chart, scope.c3.data);
}
});
chart.load(loadParam);
} else {
chart.destroy();
chart = c3.generate(decoratedConfig(scope, elem[0], attrs));
}
} else {
if (chart && typeof chart.destroy === 'function') {
chart = chart.destroy();
}
}
}, true);
elem.on('$destroy', function() {
if (chart && typeof chart.destroy === 'function') {
chart = chart.destroy();
}
});
scope.$on('$destroy', function() {
if (chart && typeof chart.destroy === 'function') {
chart = chart.destroy();
}
});
}
};
});
}());