1
|
var c3ext = {};
|
2
|
c3ext.generate = function (options) {
|
3
|
|
4
|
if (options.zoom2 != null) {
|
5
|
zoom2_reducers = options.zoom2.reducers || {};
|
6
|
zoom2_enabled = options.zoom2.enabled;
|
7
|
_zoom2_factor = options.zoom2.factor || 1;
|
8
|
_zoom2_maxItems = options.zoom2.maxItems;
|
9
|
}
|
10
|
|
11
|
if (!zoom2_enabled) {
|
12
|
return c3.generate(options);
|
13
|
}
|
14
|
|
15
|
|
16
|
var originalData = Q.copy(options.data);
|
17
|
var zoom2_reducers;
|
18
|
var zoom2_enabled;
|
19
|
var _zoom2_maxItems;
|
20
|
|
21
|
if (_zoom2_maxItems == null) {
|
22
|
var el = d3.select(options.bindto)[0][0];
|
23
|
if (el != null) {
|
24
|
var availWidth = el.clientWidth;
|
25
|
|
26
|
var pointSize = 20;
|
27
|
_zoom2_maxItems = Math.ceil(availWidth / pointSize);
|
28
|
}
|
29
|
if (_zoom2_maxItems == null || _zoom2_maxItems < 10) {
|
30
|
_zoom2_maxItems = 10;
|
31
|
}
|
32
|
}
|
33
|
|
34
|
function onZoomChanged(e) {
|
35
|
refresh();
|
36
|
}
|
37
|
|
38
|
var zoom2 = c3ext.ZoomBehavior({ changed: onZoomChanged, bindto: options.bindto });
|
39
|
|
40
|
zoom2.enhance = function () {
|
41
|
_zoom2_maxItems *= 2;
|
42
|
var totalItems = zoom2.getZoom().totalItems;
|
43
|
if (_zoom2_maxItems > totalItems)
|
44
|
_zoom2_maxItems = totalItems;
|
45
|
refresh();
|
46
|
}
|
47
|
zoom2.dehance = function () {
|
48
|
_zoom2_maxItems = Math.ceil(_zoom2_maxItems / 2) + 1;
|
49
|
refresh();
|
50
|
}
|
51
|
|
52
|
zoom2.maxItems = function () { return _zoom2_maxItems; };
|
53
|
function zoomAndReduceData(list, zoomRange, func, maxItems) {
|
54
|
//var maxItems = 10;//Math.ceil(10 * zoomFactor);
|
55
|
var list2 = list.slice(zoomRange[0], zoomRange[1]);
|
56
|
var chunkSize = 1;
|
57
|
var list3 = list2;
|
58
|
if (list3.length > maxItems) {
|
59
|
var chunkSize = Math.ceil(list2.length / maxItems);
|
60
|
list3 = list3.splitIntoChunksOf(chunkSize).map(func);
|
61
|
}
|
62
|
//console.log("x" + getCurrentZoomLevel() + ", maxItems=" + maxItems + " chunkSize=" + chunkSize + " totalBefore=" + list2.length + ", totalAfter=" + list3.length);
|
63
|
return list3;
|
64
|
}
|
65
|
|
66
|
function first(t) { return t[0]; }
|
67
|
|
68
|
var getDataForZoom = function (data) {
|
69
|
if (data.columns == null || data.columns.length == 0)
|
70
|
return;
|
71
|
|
72
|
var zoomInfo = zoom2.getZoom();
|
73
|
if (zoomInfo.totalItems != data.columns[0].length - 1) {
|
74
|
zoom2.setOptions({ totalItems: data.columns[0].length - 1 });
|
75
|
zoomInfo = zoom2.getZoom();
|
76
|
}
|
77
|
data.columns = originalData.columns.map(function (column) {
|
78
|
var name = column[0];
|
79
|
var reducer = zoom2_reducers[name] || first; //by default take the first
|
80
|
|
81
|
var values = column.slice(1);
|
82
|
var newValues = zoomAndReduceData(values, zoomInfo.currentZoom, reducer, _zoom2_maxItems);
|
83
|
return [name].concat(newValues);
|
84
|
});
|
85
|
return data;
|
86
|
};
|
87
|
|
88
|
getDataForZoom(options.data);
|
89
|
var chart = c3.generate(options);
|
90
|
var _chart_load_org = chart.load.bind(chart);
|
91
|
chart.zoom2 = zoom2;
|
92
|
chart.load = function (data) {
|
93
|
if (data.unload) {
|
94
|
unload(data.unload);
|
95
|
delete data.unload;
|
96
|
}
|
97
|
Q.copy(data, originalData);
|
98
|
refresh();
|
99
|
}
|
100
|
chart.unload = function (names) {
|
101
|
unload(names);
|
102
|
refresh();
|
103
|
}
|
104
|
|
105
|
function unload(names) {
|
106
|
originalData.columns.removeAll(function (t) { names.contains(t); });
|
107
|
}
|
108
|
|
109
|
|
110
|
function refresh() {
|
111
|
var data = Q.copy(originalData)
|
112
|
getDataForZoom(data);
|
113
|
_chart_load_org(data);
|
114
|
};
|
115
|
|
116
|
|
117
|
return chart;
|
118
|
}
|
119
|
|
120
|
c3ext.ZoomBehavior = function (options) {
|
121
|
var zoom = { __type: "ZoomBehavior" };
|
122
|
|
123
|
var _zoom2_factor;
|
124
|
var _left;
|
125
|
var totalItems;
|
126
|
var currentZoom;
|
127
|
var bindto = options.bindto;
|
128
|
var _zoomChanged = options.changed || function () { };
|
129
|
var element;
|
130
|
var mousewheelTimer;
|
131
|
var deltaY = 0;
|
132
|
var leftRatio = 0;
|
133
|
|
134
|
|
135
|
zoom.setOptions = function (options) {
|
136
|
if (options == null)
|
137
|
options = {};
|
138
|
_zoom2_factor = options.factor || 1;
|
139
|
_left = 0;
|
140
|
totalItems = options.totalItems || 0;
|
141
|
currentZoom = [0, totalItems];
|
142
|
_zoomChanged = options.changed || _zoomChanged;
|
143
|
}
|
144
|
|
145
|
zoom.setOptions(options);
|
146
|
|
147
|
|
148
|
function verifyZoom(newZoom) {
|
149
|
//newZoom.sort();
|
150
|
if (newZoom[1] > totalItems) {
|
151
|
var diff = newZoom[1] - totalItems;
|
152
|
newZoom[0] -= diff;
|
153
|
newZoom[1] -= diff;
|
154
|
}
|
155
|
if (newZoom[0] < 0) {
|
156
|
var diff = newZoom[0] * -1;
|
157
|
newZoom[0] += diff;
|
158
|
newZoom[1] += diff;
|
159
|
}
|
160
|
if (newZoom[1] > totalItems)
|
161
|
newZoom[1] = totalItems;
|
162
|
if (newZoom[0] < 0)
|
163
|
newZoom[0] = 0;
|
164
|
}
|
165
|
|
166
|
function zoomAndPan(zoomFactor, left) {
|
167
|
var itemsToShow = Math.ceil(totalItems / zoomFactor);
|
168
|
var newZoom = [left, left + itemsToShow];
|
169
|
verifyZoom(newZoom);
|
170
|
currentZoom = newZoom;
|
171
|
onZoomChanged();
|
172
|
}
|
173
|
|
174
|
function onZoomChanged() {
|
175
|
if (_zoomChanged != null)
|
176
|
_zoomChanged(zoom.getZoom());
|
177
|
}
|
178
|
function applyZoomAndPan() {
|
179
|
zoomAndPan(_zoom2_factor, _left);
|
180
|
}
|
181
|
function getItemsToShow() {
|
182
|
var itemsToShow = Math.ceil(totalItems / _zoom2_factor);
|
183
|
return itemsToShow;
|
184
|
}
|
185
|
|
186
|
|
187
|
zoom.getZoom = function () {
|
188
|
return { totalItems: totalItems, currentZoom: currentZoom.slice() };
|
189
|
}
|
190
|
|
191
|
zoom.factor = function (factor, skipDraw) {
|
192
|
if (arguments.length == 0)
|
193
|
return _zoom2_factor;
|
194
|
_zoom2_factor = factor;
|
195
|
if (_zoom2_factor < 1)
|
196
|
_zoom2_factor = 1;
|
197
|
if (skipDraw)
|
198
|
return;
|
199
|
applyZoomAndPan();
|
200
|
}
|
201
|
zoom.left = function (left, skipDraw) {
|
202
|
if (arguments.length == 0)
|
203
|
return _left;
|
204
|
_left = left;
|
205
|
if (_left < 0)
|
206
|
_left = 0;
|
207
|
var pageSize = getItemsToShow();
|
208
|
//_left += pageSize;
|
209
|
if (_left + pageSize > totalItems)
|
210
|
_left = totalItems - pageSize;
|
211
|
console.log({ left: _left, pageSize: pageSize });
|
212
|
if (skipDraw)
|
213
|
return;
|
214
|
applyZoomAndPan();
|
215
|
}
|
216
|
|
217
|
zoom.zoomAndPanByRatio = function (zoomRatio, panRatio) {
|
218
|
|
219
|
var pageSize = getItemsToShow();
|
220
|
var leftOffset = Math.round(pageSize * panRatio);
|
221
|
var mouseLeft = _left + leftOffset;
|
222
|
zoom.factor(zoom.factor() * zoomRatio, true);
|
223
|
|
224
|
var finalLeft = mouseLeft;
|
225
|
if (zoomRatio != 1) {
|
226
|
var pageSize2 = getItemsToShow();
|
227
|
var leftOffset2 = Math.round(pageSize2 * panRatio);
|
228
|
finalLeft = mouseLeft - leftOffset2;
|
229
|
}
|
230
|
zoom.left(finalLeft, true);
|
231
|
applyZoomAndPan();
|
232
|
}
|
233
|
|
234
|
zoom.zoomIn = function () {
|
235
|
zoom.zoomAndPanByRatio(2, 0);
|
236
|
}
|
237
|
|
238
|
zoom.zoomOut = function () {
|
239
|
zoom.zoomAndPanByRatio(0.5, 0);
|
240
|
}
|
241
|
|
242
|
zoom.panLeft = function () {
|
243
|
zoom.zoomAndPanByRatio(1, -1);
|
244
|
}
|
245
|
zoom.panRight = function () {
|
246
|
zoom.zoomAndPanByRatio(1, 1);
|
247
|
}
|
248
|
|
249
|
zoom.reset = function () {
|
250
|
_left = 0;
|
251
|
_zoom2_factor = 1;
|
252
|
applyZoomAndPan();
|
253
|
}
|
254
|
|
255
|
function doZoom() {
|
256
|
if (deltaY != 0) {
|
257
|
var maxDelta = 10;
|
258
|
var multiply = (maxDelta + deltaY) / maxDelta;
|
259
|
//var factor = chart.zoom2.factor()*multiply;
|
260
|
//factor= Math.ceil(factor*100) / 100;
|
261
|
console.log({ deltaY: deltaY, multiply: multiply });
|
262
|
zoom.zoomAndPanByRatio(multiply, leftRatio);//0.5);//leftRatio);
|
263
|
deltaY = 0;
|
264
|
}
|
265
|
}
|
266
|
|
267
|
function element_mousewheel(e) {
|
268
|
deltaY += e.deltaY;
|
269
|
leftRatio = (e.offsetX - 70) / (e.currentTarget.offsetWidth - 70);
|
270
|
//console.log({ "e.offsetX": e.offsetX, "e.currentTarget.offsetWidth": e.currentTarget.offsetWidth, leftRatio: leftRatio });
|
271
|
mousewheelTimer.set(150);
|
272
|
e.preventDefault();
|
273
|
}
|
274
|
|
275
|
if (bindto != null) {
|
276
|
element = $(options.bindto);
|
277
|
if (element.mousewheel) {
|
278
|
mousewheelTimer = new Timer(doZoom);
|
279
|
element.mousewheel(element_mousewheel);
|
280
|
}
|
281
|
}
|
282
|
|
283
|
return zoom;
|
284
|
|
285
|
}
|
286
|
|
287
|
if (typeof (Q) == "undefined") {
|
288
|
var Q = function () {
|
289
|
};
|
290
|
|
291
|
Q.copy = function (src, target, options, depth) {
|
292
|
///<summary>Copies an object into a target object, recursively cloning any object or array in the way, overwrite=true will overwrite a primitive field value even if exists</summary>
|
293
|
///<param name="src" />
|
294
|
///<param name="target" />
|
295
|
///<param name="options" type="Object">{ overwrite:false }</param>
|
296
|
///<returns type="Object">The copied object</returns>
|
297
|
if (depth == null)
|
298
|
depth = 0;
|
299
|
if (depth == 100) {
|
300
|
console.warn("Q.copy is in depth of 100 - possible circular reference")
|
301
|
}
|
302
|
options = options || { overwrite: false };
|
303
|
if (src == target || src == null)
|
304
|
return target;
|
305
|
if (typeof (src) != "object") {
|
306
|
if (options.overwrite || target == null)
|
307
|
return src;
|
308
|
return target;
|
309
|
}
|
310
|
if (typeof (src.clone) == "function") {
|
311
|
if (options.overwrite || target == null)
|
312
|
return src.clone();
|
313
|
return target;
|
314
|
}
|
315
|
if (target == null) {
|
316
|
if (src instanceof Array)
|
317
|
target = [];
|
318
|
else
|
319
|
target = {};
|
320
|
}
|
321
|
|
322
|
if (src instanceof Array) {
|
323
|
for (var i = 0; i < src.length; i++) {
|
324
|
var item = src[i];
|
325
|
var item2 = target[i];
|
326
|
item2 = Q.copy(item, item2, options, depth + 1);
|
327
|
target[i] = item2;
|
328
|
}
|
329
|
target.splice(src.length, target.length - src.length);
|
330
|
return target;
|
331
|
}
|
332
|
for (var p in src) {
|
333
|
var value = src[p];
|
334
|
var value2 = target[p];
|
335
|
value2 = Q.copy(value, value2, options, depth + 1);
|
336
|
target[p] = value2;
|
337
|
}
|
338
|
return target;
|
339
|
}
|
340
|
}
|
341
|
if (typeof (Timer) == "undefined") {
|
342
|
var Timer = function (action, ms) {
|
343
|
this.action = action;
|
344
|
if (ms != null)
|
345
|
this.set(ms);
|
346
|
}
|
347
|
|
348
|
Timer.prototype.set = function (ms) {
|
349
|
if (ms == null)
|
350
|
ms = this._ms;
|
351
|
else
|
352
|
this._ms = ms;
|
353
|
this.clear();
|
354
|
if (ms == null)
|
355
|
return;
|
356
|
this.timeout = window.setTimeout(this.onTick.bind(this), ms);
|
357
|
}
|
358
|
|
359
|
Timer.prototype.onTick = function () {
|
360
|
this.clear();
|
361
|
this.action();
|
362
|
}
|
363
|
|
364
|
Timer.prototype.clear = function (ms) {
|
365
|
if (this.timeout == null)
|
366
|
return;
|
367
|
window.clearTimeout(this.timeout);
|
368
|
this.timeout = null;
|
369
|
}
|
370
|
}
|
371
|
if (typeof(Array.prototype.splitIntoChunksOf)=="undefined") {
|
372
|
Array.prototype.splitIntoChunksOf = function (countInEachChunk) {
|
373
|
var chunks = Math.ceil(this.length / countInEachChunk);
|
374
|
var list = [];
|
375
|
for (var i = 0; i < this.length; i += countInEachChunk) {
|
376
|
list.push(this.slice(i, i + countInEachChunk));
|
377
|
}
|
378
|
return list;
|
379
|
}
|
380
|
}
|