Debian Codegraph @

src/js/Rickshaw.Graph.js

raw
1 Rickshaw.namespace('Rickshaw.Graph');
2
3 Rickshaw.Graph = function(args) {
4
5 var self = this;
6
7 this.initialize = function(args) {
8
9 if (!args.element) throw "Rickshaw.Graph needs a reference to an element";
10 if (args.element.nodeType !== 1) throw "Rickshaw.Graph element was defined but not an HTML element";
11
12 this.element = args.element;
13 this.series = args.series;
14 this.window = {};
15
16 this.updateCallbacks = [];
17 this.configureCallbacks = [];
18
19 this.defaults = {
20 interpolation: 'cardinal',
21 offset: 'zero',
22 min: undefined,
23 max: undefined,
24 preserve: false,
25 xScale: undefined,
26 yScale: undefined,
27 stack: true
28 };
29
30 this._loadRenderers();
31 this.configure(args);
32 this.validateSeries(args.series);
33
34 this.series.active = function() { return self.series.filter( function(s) { return !s.disabled } ) };
35 this.setSize({ width: args.width, height: args.height });
36 this.element.classList.add('rickshaw_graph');
37
38 this.vis = d3.select(this.element)
39 .append("svg:svg")
40 .attr('width', this.width)
41 .attr('height', this.height);
42
43 this.discoverRange();
44 };
45
46 this._loadRenderers = function() {
47
48 for (var name in Rickshaw.Graph.Renderer) {
49 if (!name || !Rickshaw.Graph.Renderer.hasOwnProperty(name)) continue;
50 var r = Rickshaw.Graph.Renderer[name];
51 if (!r || !r.prototype || !r.prototype.render) continue;
52 self.registerRenderer(new r( { graph: self } ));
53 }
54 };
55
56 this.validateSeries = function(series) {
57
58 if (!Array.isArray(series) && !(series instanceof Rickshaw.Series)) {
59 var seriesSignature = Object.prototype.toString.apply(series);
60 throw "series is not an array: " + seriesSignature;
61 }
62
63 var pointsCount;
64
65 series.forEach( function(s) {
66
67 if (!(s instanceof Object)) {
68 throw "series element is not an object: " + s;
69 }
70 if (!(s.data)) {
71 throw "series has no data: " + JSON.stringify(s);
72 }
73 if (!Array.isArray(s.data)) {
74 throw "series data is not an array: " + JSON.stringify(s.data);
75 }
76
77 if (s.data.length > 0) {
78 var x = s.data[0].x;
79 var y = s.data[0].y;
80
81 if (typeof x != 'number' || ( typeof y != 'number' && y !== null ) ) {
82 throw "x and y properties of points should be numbers instead of " +
83 (typeof x) + " and " + (typeof y);
84 }
85 }
86
87 if (s.data.length >= 3) {
88 // probe to sanity check sort order
89 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) {
90 throw "series data needs to be sorted on x values for series name: " + s.name;
91 }
92 }
93
94 }, this );
95 };
96
97 this.dataDomain = function() {
98
99 var data = this.series.map( function(s) { return s.data } );
100
101 var min = d3.min( data.map( function(d) { return d[0].x } ) );
102 var max = d3.max( data.map( function(d) { return d[d.length - 1].x } ) );
103
104 return [min, max];
105 };
106
107 this.discoverRange = function() {
108
109 var domain = this.renderer.domain();
110
111 // this.*Scale is coming from the configuration dictionary
112 // which may be referenced by the Graph creator, or shared
113 // with other Graphs. We need to ensure we copy the scale
114 // so that our mutations do not change the object given to us.
115 // Hence the .copy()
116 this.x = (this.xScale || d3.scale.linear()).copy().domain(domain.x).range([0, this.width]);
117 this.y = (this.yScale || d3.scale.linear()).copy().domain(domain.y).range([this.height, 0]);
118
119 this.x.magnitude = d3.scale.linear()
120 .domain([domain.x[0] - domain.x[0], domain.x[1] - domain.x[0]])
121 .range([0, this.width]);
122
123 this.y.magnitude = d3.scale.linear()
124 .domain([domain.y[0] - domain.y[0], domain.y[1] - domain.y[0]])
125 .range([0, this.height]);
126 };
127
128 this.render = function() {
129
130 var stackedData = this.stackData();
131 this.discoverRange();
132
133 this.renderer.render();
134
135 this.updateCallbacks.forEach( function(callback) {
136 callback();
137 } );
138
139 };
140
141 this.update = this.render;
142
143 this.stackData = function() {
144
145 var data = this.series.active()
146 .map( function(d) { return d.data } )
147 .map( function(d) { return d.filter( function(d) { return this._slice(d) }, this ) }, this);
148
149 var preserve = this.preserve;
150 if (!preserve) {
151 this.series.forEach( function(series) {
152 if (series.scale) {
153 // data must be preserved when a scale is used
154 preserve = true;
155 }
156 } );
157 }
158
159 data = preserve ? Rickshaw.clone(data) : data;
160
161 this.series.active().forEach( function(series, index) {
162 if (series.scale) {
163 // apply scale to each series
164 var seriesData = data[index];
165 if(seriesData) {
166 seriesData.forEach( function(d) {
167 d.y = series.scale(d.y);
168 } );
169 }
170 }
171 } );
172
173 this.stackData.hooks.data.forEach( function(entry) {
174 data = entry.f.apply(self, [data]);
175 } );
176
177 var stackedData;
178
179 if (!this.renderer.unstack) {
180
181 this._validateStackable();
182
183 var layout = d3.layout.stack();
184 layout.offset( self.offset );
185 stackedData = layout(data);
186 }
187
188 stackedData = stackedData || data;
189
190 if (this.renderer.unstack) {
191 stackedData.forEach( function(seriesData) {
192 seriesData.forEach( function(d) {
193 d.y0 = d.y0 === undefined ? 0 : d.y0;
194 } );
195 } );
196 }
197
198 this.stackData.hooks.after.forEach( function(entry) {
199 stackedData = entry.f.apply(self, [data]);
200 } );
201
202 var i = 0;
203 this.series.forEach( function(series) {
204 if (series.disabled) return;
205 series.stack = stackedData[i++];
206 } );
207
208 this.stackedData = stackedData;
209 return stackedData;
210 };
211
212 this._validateStackable = function() {
213
214 var series = this.series;
215 var pointsCount;
216
217 series.forEach( function(s) {
218
219 pointsCount = pointsCount || s.data.length;
220
221 if (pointsCount && s.data.length != pointsCount) {
222 throw "stacked series cannot have differing numbers of points: " +
223 pointsCount + " vs " + s.data.length + "; see Rickshaw.Series.fill()";
224 }
225
226 }, this );
227 };
228
229 this.stackData.hooks = { data: [], after: [] };
230
231 this._slice = function(d) {
232
233 if (this.window.xMin || this.window.xMax) {
234
235 var isInRange = true;
236
237 if (this.window.xMin && d.x < this.window.xMin) isInRange = false;
238 if (this.window.xMax && d.x > this.window.xMax) isInRange = false;
239
240 return isInRange;
241 }
242
243 return true;
244 };
245
246 this.onUpdate = function(callback) {
247 this.updateCallbacks.push(callback);
248 };
249
250 this.onConfigure = function(callback) {
251 this.configureCallbacks.push(callback);
252 };
253
254 this.registerRenderer = function(renderer) {
255 this._renderers = this._renderers || {};
256 this._renderers[renderer.name] = renderer;
257 };
258
259 this.configure = function(args) {
260
261 this.config = this.config || {};
262
263 if (args.width || args.height) {
264 this.setSize(args);
265 }
266
267 Rickshaw.keys(this.defaults).forEach( function(k) {
268 this.config[k] = k in args ? args[k]
269 : k in this ? this[k]
270 : this.defaults[k];
271 }, this );
272
273 Rickshaw.keys(this.config).forEach( function(k) {
274 this[k] = this.config[k];
275 }, this );
276
277 if ('stack' in args) args.unstack = !args.stack;
278
279 var renderer = args.renderer || (this.renderer && this.renderer.name) || 'stack';
280 this.setRenderer(renderer, args);
281
282 this.configureCallbacks.forEach( function(callback) {
283 callback(args);
284 } );
285 };
286
287 this.setRenderer = function(r, args) {
288 if (typeof r == 'function') {
289 this.renderer = new r( { graph: self } );
290 this.registerRenderer(this.renderer);
291 } else {
292 if (!this._renderers[r]) {
293 throw "couldn't find renderer " + r;
294 }
295 this.renderer = this._renderers[r];
296 }
297
298 if (typeof args == 'object') {
299 this.renderer.configure(args);
300 }
301 };
302
303 this.setSize = function(args) {
304
305 args = args || {};
306
307 if (typeof window !== undefined) {
308 var style = window.getComputedStyle(this.element, null);
309 var elementWidth = parseInt(style.getPropertyValue('width'), 10);
310 var elementHeight = parseInt(style.getPropertyValue('height'), 10);
311 }
312
313 this.width = args.width || elementWidth || 400;
314 this.height = args.height || elementHeight || 250;
315
316 this.vis && this.vis
317 .attr('width', this.width)
318 .attr('height', this.height);
319 };
320
321 this.initialize(args);
322 };