-
Notifications
You must be signed in to change notification settings - Fork 938
/
Copy pathRickshaw.Graph.js
326 lines (242 loc) · 7.91 KB
/
Rickshaw.Graph.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
Rickshaw.namespace('Rickshaw.Graph');
Rickshaw.Graph = function(args) {
var self = this;
this.initialize = function(args) {
if (!args.element) throw "Rickshaw.Graph needs a reference to an element";
if (args.element.nodeType !== 1) throw "Rickshaw.Graph element was defined but not an HTML element";
this.element = args.element;
this.series = args.series;
this.window = {};
this.updateCallbacks = [];
this.configureCallbacks = [];
this.defaults = {
interpolation: 'cardinal',
offset: 'zero',
min: undefined,
max: undefined,
preserve: false,
xScale: undefined,
yScale: undefined,
stack: true
};
this._loadRenderers();
this.configure(args);
this.validateSeries(args.series);
this.series.active = function() { return self.series.filter( function(s) { return !s.disabled } ) };
this.setSize({ width: args.width, height: args.height });
this.element.classList.add('rickshaw_graph');
this.vis = d3.select(this.element)
.append("svg:svg")
.attr('width', this.width)
.attr('height', this.height);
this.discoverRange();
};
this._loadRenderers = function() {
for (var name in Rickshaw.Graph.Renderer) {
if (!name || !Rickshaw.Graph.Renderer.hasOwnProperty(name)) continue;
var r = Rickshaw.Graph.Renderer[name];
if (!r || !r.prototype || !r.prototype.render) continue;
self.registerRenderer(new r( { graph: self } ));
}
};
this.validateSeries = function(series) {
if (!Array.isArray(series) && !(series instanceof Rickshaw.Series)) {
var seriesSignature = Object.prototype.toString.apply(series);
throw "series is not an array: " + seriesSignature;
}
var pointsCount;
series.forEach( function(s) {
if (!(s instanceof Object)) {
throw "series element is not an object: " + s;
}
if (!(s.data)) {
throw "series has no data: " + JSON.stringify(s);
}
if (!Array.isArray(s.data)) {
throw "series data is not an array: " + JSON.stringify(s.data);
}
if (s.data.length > 0) {
var x = s.data[0].x;
var y = s.data[0].y;
if (typeof x != 'number' || ( typeof y != 'number' && y !== null ) ) {
throw "x and y properties of points should be numbers instead of " +
(typeof x) + " and " + (typeof y);
}
}
if (s.data.length >= 3) {
// probe to sanity check sort order
if (s.data[2].x < s.data[1].x || s.data[1].x < s.data[0].x || s.data[s.data.length - 1].x < s.data[0].x) {
throw "series data needs to be sorted on x values for series name: " + s.name;
}
}
}, this );
};
this.dataDomain = function() {
var data = this.series.map( function(s) { return s.data } );
var min = d3.min( data.map( function(d) { return d[0].x } ) );
var max = d3.max( data.map( function(d) { return d[d.length - 1].x } ) );
return [min, max];
};
this.discoverRange = function() {
var domain = this.renderer.domain();
// this.*Scale is coming from the configuration dictionary
// which may be referenced by the Graph creator, or shared
// with other Graphs. We need to ensure we copy the scale
// so that our mutations do not change the object given to us.
// Hence the .copy()
this.x = (this.xScale || d3.scale.linear()).copy().domain(domain.x).range([0, this.width]);
this.y = (this.yScale || d3.scale.linear()).copy().domain(domain.y).range([this.height, 0]);
this.x.magnitude = d3.scale.linear()
.domain([domain.x[0] - domain.x[0], domain.x[1] - domain.x[0]])
.range([0, this.width]);
this.y.magnitude = d3.scale.linear()
.domain([domain.y[0] - domain.y[0], domain.y[1] - domain.y[0]])
.range([0, this.height]);
};
this.render = function() {
var stackedData = this.stackData();
this.discoverRange();
this.renderer.render();
this.updateCallbacks.forEach( function(callback) {
callback();
} );
};
this.update = this.render;
this.stackData = function() {
var data = this.series.active()
.map( function(d) { return d.data } )
.map( function(d) { return d.filter( function(d) { return this._slice(d) }, this ) }, this);
var preserve = this.preserve;
if (!preserve) {
this.series.forEach( function(series) {
if (series.scale) {
// data must be preserved when a scale is used
preserve = true;
}
} );
}
data = preserve ? Rickshaw.clone(data) : data;
this.series.active().forEach( function(series, index) {
if (series.scale) {
// apply scale to each series
var seriesData = data[index];
if(seriesData) {
seriesData.forEach( function(d) {
d.y = series.scale(d.y);
} );
}
}
} );
this.stackData.hooks.data.forEach( function(entry) {
data = entry.f.apply(self, [data]);
} );
var stackedData;
if (!this.renderer.unstack) {
this._validateStackable();
var layout = d3.layout.stack();
layout.offset( self.offset );
if (data.length) {
stackedData = layout(data);
} else {
stackedData = [];
}
}
stackedData = stackedData || data;
if (this.renderer.unstack) {
stackedData.forEach( function(seriesData) {
seriesData.forEach( function(d) {
d.y0 = d.y0 === undefined ? 0 : d.y0;
} );
} );
}
this.stackData.hooks.after.forEach( function(entry) {
stackedData = entry.f.apply(self, [data]);
} );
var i = 0;
this.series.forEach( function(series) {
if (series.disabled) return;
series.stack = stackedData[i++];
} );
this.stackedData = stackedData;
return stackedData;
};
this._validateStackable = function() {
var series = this.series;
var pointsCount;
series.forEach( function(s) {
pointsCount = pointsCount || s.data.length;
if (pointsCount && s.data.length != pointsCount) {
throw "stacked series cannot have differing numbers of points: " +
pointsCount + " vs " + s.data.length + "; see Rickshaw.Series.fill()";
}
}, this );
};
this.stackData.hooks = { data: [], after: [] };
this._slice = function(d) {
if (this.window.xMin || this.window.xMax) {
var isInRange = true;
if (this.window.xMin && d.x < this.window.xMin) isInRange = false;
if (this.window.xMax && d.x > this.window.xMax) isInRange = false;
return isInRange;
}
return true;
};
this.onUpdate = function(callback) {
this.updateCallbacks.push(callback);
};
this.onConfigure = function(callback) {
this.configureCallbacks.push(callback);
};
this.registerRenderer = function(renderer) {
this._renderers = this._renderers || {};
this._renderers[renderer.name] = renderer;
};
this.configure = function(args) {
this.config = this.config || {};
if (args.width || args.height) {
this.setSize(args);
}
Rickshaw.keys(this.defaults).forEach( function(k) {
this.config[k] = k in args ? args[k]
: k in this ? this[k]
: this.defaults[k];
}, this );
Rickshaw.keys(this.config).forEach( function(k) {
this[k] = this.config[k];
}, this );
if ('stack' in args) args.unstack = !args.stack;
var renderer = args.renderer || (this.renderer && this.renderer.name) || 'stack';
this.setRenderer(renderer, args);
this.configureCallbacks.forEach( function(callback) {
callback(args);
} );
};
this.setRenderer = function(r, args) {
if (typeof r == 'function') {
this.renderer = new r( { graph: self } );
this.registerRenderer(this.renderer);
} else {
if (!this._renderers[r]) {
throw "couldn't find renderer " + r;
}
this.renderer = this._renderers[r];
}
if (typeof args == 'object') {
this.renderer.configure(args);
}
};
this.setSize = function(args) {
args = args || {};
if (typeof window !== undefined) {
var style = window.getComputedStyle(this.element, null);
var elementWidth = parseInt(style.getPropertyValue('width'), 10);
var elementHeight = parseInt(style.getPropertyValue('height'), 10);
}
this.width = args.width || elementWidth || 400;
this.height = args.height || elementHeight || 250;
this.vis && this.vis
.attr('width', this.width)
.attr('height', this.height);
};
this.initialize(args);
};