-
Notifications
You must be signed in to change notification settings - Fork 11
/
d3.chart.base.js
402 lines (322 loc) · 11.1 KB
/
d3.chart.base.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*! d3.chart.base - v0.4.0
* License: MIT Expat
* Date: 2013-10-15
*/
(function(d3) {
// obtains element computed style
// context is chart
function _style(attr) {
var style,
element = this.base[0][0];
if (window.getComputedStyle) {
style = window.getComputedStyle(element);
} else if (element.currentStyle) {
style = element.currentStyle;
}
if (!attr) {
return style;
} else {
return style[attr];
}
}
// converts pixel values
var _toNumFromPx = (function() {
var rx = /px$/;
return function(value) {
if (rx.test(value)) {
return +(value.replace(rx, ""));
} else {
return value;
}
};
}());
// helper attribute setter on chart base.
// context is chart
function _initAttr(internalName, d3Name, defaultValue) {
var current = _toNumFromPx(_style.call(this, d3Name));
if (current === null || current === 0 || current === "") {
this[internalName] = defaultValue;
this.base.style(d3Name, defaultValue);
} else {
this[internalName] = _toNumFromPx(_style.call(this, d3Name));
}
}
// Borrowed from Underscore.js 1.5.2
// http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function _debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
return function() {
context = this;
args = arguments;
timestamp = new Date();
var later = function() {
var last = (new Date()) - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) { result = func.apply(context, args); }
}
};
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) { result = func.apply(context, args); }
return result;
};
}
// go over existing modes and determine which we are in
// returns true if a mode change occured, false otherwise.
function _determineMode() {
var oldMode = this._currentMode;
this._currentMode = null;
if ("modes" in this) {
var result = false;
for (var mode in this._modes) {
result = this._modes[mode].call(this);
if (result) {
this._currentMode = mode;
break;
}
}
}
return oldMode !== this._currentMode;
}
// takes care of removing/adding appropriate layers
function _onModeChange() {
var chart = this;
for(var layerName in chart._layersArguments) {
// is this chart in the current mode?
var layerArgs = chart._layersArguments[layerName];
// if this layer should not exist in the current mode
// unlayer it and then save it so we can reattach it
// later.
if (layerArgs.options.modes.indexOf(chart.mode()) === -1) {
// is it showing?
if (layerArgs.showing === true) {
// nope? remove it.
var removedLayer = chart.unlayer(layerName);
removedLayer.style("display","none");
chart._layersArguments[layerName].showing = false;
chart._layersArguments[layerName].layer = removedLayer;
}
} else {
// this layer is not showing, we need to add it
if (chart._layersArguments[layerName].showing === false) {
// if the layer has already been created, just re-add it
if (chart._layersArguments[layerName].layer !== null) {
oldLayer.call(chart, layerName, chart._layersArguments[layerName].layer);
chart._layersArguments[layerName].layer.style("display","inline");
} else {
// this layer must not have been drawn in the initial rendering
// but we do have the arguments, so render it using the
// old layering.
oldLayer.call(chart,
chart._layersArguments[layerName].name,
chart._layersArguments[layerName].selection,
chart._layersArguments[layerName].options);
}
chart._layersArguments[layerName].showing = true;
}
}
}
}
var BaseChart = d3.chart("BaseChart", {
initialize: function() {
var chart = this;
// layer structures container - for layers that are
// created on initialize but do not actually need to
// be rendered in the detected mode, we need to save the
// actual arguments so that we can construct it later.
this._layersArguments = {};
// save mode functions
this._modes = this.modes || {};
delete this.modes;
// store layers referenced per mode
this._modeLayers = {};
// determine current mode on initialization
_determineMode.call(this);
// setup some reasonable defaults
chart._width = _toNumFromPx(_style.call(chart, "width")) || 200;
chart._height = _toNumFromPx(_style.call(chart, "height")) || 200;
// make sure container height and width are set.
_initAttr.call(this, "_width", "width", 200);
_initAttr.call(this, "_height", "height", 200);
// bind to winow resize end
window.addEventListener("resize", _debounce(function() {
// trigger generic resize event
chart.trigger("resize");
// don't overwrite % widths.
if (!isNaN(chart._width)) {
chart.width(_toNumFromPx(_style.call(chart, "width")) || 200, {
noDraw : true
});
}
if (!isNaN(chart._height)) {
chart.height(_toNumFromPx(_style.call(chart, "height")) || 200, {
noDraw: true
});
}
// update current mode
var changed = _determineMode.call(chart);
if (changed) {
chart.trigger("change:mode", this._currentMode);
}
// only redraw if there is data
if (chart.data) {
chart.draw(chart.data);
}
}, 150));
window.addEventListener("orientationchange", function() {
// redraw on device rotation
chart.trigger("change:mode", this._currentMode);
// only redraw if there is data
if (chart.data) {
chart.draw(chart.data);
}
}, false);
// on mode change, update height and width, and redraw
// the chart
chart.on("change:mode", function() {
// sort out current mode
_onModeChange.call(chart);
});
},
// returns current mode
mode : function() {
return this._currentMode;
},
recomputeMode: function() {
var changed = _determineMode.call(this);
if (changed) {
this.trigger("change:mode", this._currentMode);
}
return changed;
},
width: function(newWidth, options) {
options = options || {};
if (arguments.length === 0) {
if (this._width && !isNaN(+this._width)) {
return this._width;
} else {
return _toNumFromPx(_style.call(this, "width"));
}
}
var oldWidth = this._width;
this._width = newWidth;
// only if the width actually changed:
if (this._width !== oldWidth) {
// set higher container width
this.base.style("width", isNaN(this._width) ?
this._width :
this._width + "px");
// trigger a change event
if (!options.silent) {
this.trigger("change:width", this._width, oldWidth);
}
// redraw if we saved the data on the chart
if (this.data && !options.noDraw) {
this.draw(this.data);
}
}
// always return the chart, for chaining magic.
return this;
},
height: function(newHeight, options) {
options = options || {};
if (arguments.length === 0) {
if (this._height && !isNaN(+this._height)) {
return this._height;
} else {
return _toNumFromPx(_style.call(this, "height"));
}
}
var oldHeight = this._height;
this._height = newHeight;
if (this._height !== oldHeight) {
this.base.style("height", isNaN(this._height) ?
this._height :
this._height + "px");
if (!options.silent) {
this.trigger("change:height", this._height, oldHeight);
}
if (this.data && !options.noDraw) {
this.draw(this.data);
}
}
return this;
}
});
var oldLayer = BaseChart.prototype.layer;
BaseChart.prototype.layer = function(name, selection, options) {
var chart = this;
// just return an existing layer if all we are
// passed is the name argument
if (arguments.length === 1) {
return oldLayer.call(this, name);
}
// save all the layer arguments. For layers that are created
// but do not need to be rendered in the current mode, this
// will ensure their arguments are intact for when they do
// need to be created.
chart._layersArguments[name] = {
name : name,
selection: selection,
options : options,
showing: false, // default hidden
layer: null // layer handle
};
// create the layer if it should exist in the curret
// mode.
var layer;
if (typeof options.modes === "undefined" ||
("modes" in options &&
options.modes.indexOf(chart.mode()) > -1)) {
// run default layer code
layer = oldLayer.call(this, name, selection, options);
// mark layer as showing.
chart._layersArguments[name].showing = true;
chart._layersArguments[name].layer = layer;
}
if ("modes" in options) {
// save available modes on the layer if we created it
if (layer) {
layer._modes = options.modes;
}
// cache the layer under the mode name. This will be useful
// when we are repainting layers.
options.modes.forEach(function(mode) {
// make sure mode exists
if (mode in chart._modes) {
chart._modeLayers[mode] = chart._modeLayers[mode] || [];
// save the layer as being mapped to this mode.
chart._modeLayers[mode].push(name);
} else {
throw new Error("Mode " + mode + " is not defined");
}
});
// make sure this layer has all modes if none were
// specified as an option.
} else if (chart._modes) {
var allModes = Object.keys(chart._modes);
if (layer) {
layer._modes = allModes;
}
allModes.forEach(function(mode) {
chart._modeLayers[mode] = chart._modeLayers[mode] || [];
chart._modeLayers[mode].push(name);
});
// mark layer as showing.
chart._layersArguments[name].showing = true;
chart._layersArguments[name].layer = layer;
}
return layer;
};
}(window.d3));