Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | // Copyright 2006 Dan Vanderkam (danvdk@gmail.com) |
2 | // All Rights Reserved. | |
3 | ||
4 | /** | |
3df0ccf0 DV |
5 | * @fileoverview Based on PlotKit, but modified to meet the needs of dygraphs. |
6 | * In particular, support for: | |
7 | * - grid overlays | |
8 | * - error bars | |
9 | * - dygraphs attribute system | |
6a1aa64f DV |
10 | */ |
11 | ||
6a1aa64f | 12 | /** |
3df0ccf0 | 13 | * Creates a new DygraphLayout object. |
6a1aa64f | 14 | * @param {Object} options Options for PlotKit.Layout |
285a6bda | 15 | * @return {Object} The DygraphLayout object |
6a1aa64f | 16 | */ |
efe0829a DV |
17 | DygraphLayout = function(dygraph, options) { |
18 | this.dygraph_ = dygraph; | |
19 | this.options = {}; // TODO(danvk): remove, use attr_ instead. | |
fc80a396 | 20 | Dygraph.update(this.options, options ? options : {}); |
efe0829a | 21 | this.datasets = new Array(); |
5c528fa2 | 22 | this.annotations = new Array() |
6a1aa64f | 23 | }; |
efe0829a DV |
24 | |
25 | DygraphLayout.prototype.attr_ = function(name) { | |
26 | return this.dygraph_.attr_(name); | |
27 | }; | |
28 | ||
29 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
30 | this.datasets[setname] = set_xy; | |
31 | }; | |
32 | ||
ce49c2fa | 33 | // TODO(danvk): CONTRACT remove |
5c528fa2 DV |
34 | DygraphLayout.prototype.setAnnotations = function(ann) { |
35 | // The Dygraph object's annotations aren't parsed. We parse them here and | |
36 | // save a copy. | |
37 | var parse = this.attr_('xValueParser'); | |
38 | for (var i = 0; i < ann.length; i++) { | |
39 | var a = {}; | |
40 | if (!ann[i].x) { | |
41 | this.dygraph_.error("Annotations must have an 'x' property"); | |
42 | return; | |
43 | } | |
44 | Dygraph.update(a, ann[i]); | |
45 | a.xval = parse(a.x); | |
46 | this.annotations.push(a); | |
ce49c2fa | 47 | } |
ce49c2fa DV |
48 | }; |
49 | ||
efe0829a DV |
50 | DygraphLayout.prototype.evaluate = function() { |
51 | this._evaluateLimits(); | |
52 | this._evaluateLineCharts(); | |
53 | this._evaluateLineTicks(); | |
ce49c2fa | 54 | this._evaluateAnnotations(); |
efe0829a DV |
55 | }; |
56 | ||
57 | DygraphLayout.prototype._evaluateLimits = function() { | |
58 | this.minxval = this.maxxval = null; | |
f6401bf6 DV |
59 | if (this.options.dateWindow) { |
60 | this.minxval = this.options.dateWindow[0]; | |
61 | this.maxxval = this.options.dateWindow[1]; | |
62 | } else { | |
63 | for (var name in this.datasets) { | |
64 | if (!this.datasets.hasOwnProperty(name)) continue; | |
65 | var series = this.datasets[name]; | |
66 | var x1 = series[0][0]; | |
67 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
85b99f0b | 68 | |
f6401bf6 DV |
69 | var x2 = series[series.length - 1][0]; |
70 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
71 | } | |
efe0829a DV |
72 | } |
73 | this.xrange = this.maxxval - this.minxval; | |
74 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); | |
75 | ||
76 | this.minyval = this.options.yAxis[0]; | |
77 | this.maxyval = this.options.yAxis[1]; | |
78 | this.yrange = this.maxyval - this.minyval; | |
79 | this.yscale = (this.yrange != 0 ? 1/this.yrange : 1.0); | |
80 | }; | |
81 | ||
82 | DygraphLayout.prototype._evaluateLineCharts = function() { | |
83 | // add all the rects | |
84 | this.points = new Array(); | |
85 | for (var setName in this.datasets) { | |
85b99f0b DV |
86 | if (!this.datasets.hasOwnProperty(setName)) continue; |
87 | ||
88 | var dataset = this.datasets[setName]; | |
89 | for (var j = 0; j < dataset.length; j++) { | |
90 | var item = dataset[j]; | |
91 | var point = { | |
ff00d3e2 | 92 | // TODO(danvk): here |
85b99f0b DV |
93 | x: ((parseFloat(item[0]) - this.minxval) * this.xscale), |
94 | y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale), | |
95 | xval: parseFloat(item[0]), | |
96 | yval: parseFloat(item[1]), | |
97 | name: setName | |
98 | }; | |
99 | ||
100 | // limit the x, y values so they do not overdraw | |
101 | if (point.y <= 0.0) { | |
102 | point.y = 0.0; | |
103 | } | |
104 | if (point.y >= 1.0) { | |
105 | point.y = 1.0; | |
106 | } | |
1a26f3fb | 107 | this.points.push(point); |
85b99f0b | 108 | } |
efe0829a DV |
109 | } |
110 | }; | |
111 | ||
112 | DygraphLayout.prototype._evaluateLineTicks = function() { | |
113 | this.xticks = new Array(); | |
114 | for (var i = 0; i < this.options.xTicks.length; i++) { | |
115 | var tick = this.options.xTicks[i]; | |
116 | var label = tick.label; | |
117 | var pos = this.xscale * (tick.v - this.minxval); | |
118 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
119 | this.xticks.push([pos, label]); | |
120 | } | |
121 | } | |
122 | ||
123 | this.yticks = new Array(); | |
124 | for (var i = 0; i < this.options.yTicks.length; i++) { | |
125 | var tick = this.options.yTicks[i]; | |
126 | var label = tick.label; | |
127 | var pos = 1.0 - (this.yscale * (tick.v - this.minyval)); | |
128 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
129 | this.yticks.push([pos, label]); | |
130 | } | |
131 | } | |
132 | }; | |
133 | ||
6a1aa64f DV |
134 | |
135 | /** | |
136 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
137 | * @private | |
138 | */ | |
285a6bda | 139 | DygraphLayout.prototype.evaluateWithError = function() { |
6a1aa64f DV |
140 | this.evaluate(); |
141 | if (!this.options.errorBars) return; | |
142 | ||
143 | // Copy over the error terms | |
144 | var i = 0; // index in this.points | |
145 | for (var setName in this.datasets) { | |
85b99f0b DV |
146 | if (!this.datasets.hasOwnProperty(setName)) continue; |
147 | var j = 0; | |
148 | var dataset = this.datasets[setName]; | |
149 | for (var j = 0; j < dataset.length; j++, i++) { | |
150 | var item = dataset[j]; | |
151 | var xv = parseFloat(item[0]); | |
152 | var yv = parseFloat(item[1]); | |
153 | ||
154 | if (xv == this.points[i].xval && | |
155 | yv == this.points[i].yval) { | |
156 | this.points[i].errorMinus = parseFloat(item[2]); | |
157 | this.points[i].errorPlus = parseFloat(item[3]); | |
158 | } | |
159 | } | |
6a1aa64f DV |
160 | } |
161 | }; | |
162 | ||
ce49c2fa DV |
163 | DygraphLayout.prototype._evaluateAnnotations = function() { |
164 | // Add the annotations to the point to which they belong. | |
165 | // Make a map from (setName, xval) to annotation for quick lookups. | |
166 | var annotations = {}; | |
167 | for (var i = 0; i < this.annotations.length; i++) { | |
168 | var a = this.annotations[i]; | |
169 | annotations[a.xval + "," + a.series] = a; | |
170 | } | |
171 | ||
172 | this.annotated_points = []; | |
173 | for (var i = 0; i < this.points.length; i++) { | |
174 | var p = this.points[i]; | |
175 | var k = p.xval + "," + p.name; | |
176 | if (k in annotations) { | |
177 | p.annotation = annotations[k]; | |
178 | this.annotated_points.push(p); | |
179 | } | |
180 | } | |
181 | }; | |
182 | ||
6a1aa64f DV |
183 | /** |
184 | * Convenience function to remove all the data sets from a graph | |
185 | */ | |
285a6bda | 186 | DygraphLayout.prototype.removeAllDatasets = function() { |
6a1aa64f DV |
187 | delete this.datasets; |
188 | this.datasets = new Array(); | |
189 | }; | |
190 | ||
191 | /** | |
192 | * Change the values of various layout options | |
193 | * @param {Object} new_options an associative array of new properties | |
194 | */ | |
285a6bda | 195 | DygraphLayout.prototype.updateOptions = function(new_options) { |
fc80a396 | 196 | Dygraph.update(this.options, new_options ? new_options : {}); |
6a1aa64f DV |
197 | }; |
198 | ||
199 | // Subclass PlotKit.CanvasRenderer to add: | |
200 | // 1. X/Y grid overlay | |
201 | // 2. Ability to draw error bars (if required) | |
202 | ||
203 | /** | |
204 | * Sets some PlotKit.CanvasRenderer options | |
205 | * @param {Object} element The canvas to attach to | |
285a6bda | 206 | * @param {Layout} layout The DygraphLayout object for this graph. |
6a1aa64f DV |
207 | * @param {Object} options Options to pass on to CanvasRenderer |
208 | */ | |
9317362d DV |
209 | DygraphCanvasRenderer = function(dygraph, element, layout, options) { |
210 | // TODO(danvk): remove options, just use dygraph.attr_. | |
9317362d | 211 | this.dygraph_ = dygraph; |
fbe31dc8 DV |
212 | |
213 | // default options | |
214 | this.options = { | |
f474c2a3 DV |
215 | "strokeWidth": 0.5, |
216 | "drawXAxis": true, | |
217 | "drawYAxis": true, | |
218 | "axisLineColor": "black", | |
219 | "axisLineWidth": 0.5, | |
220 | "axisTickSize": 3, | |
221 | "axisLabelColor": "black", | |
222 | "axisLabelFont": "Arial", | |
223 | "axisLabelFontSize": 9, | |
224 | "axisLabelWidth": 50, | |
225 | "drawYGrid": true, | |
226 | "drawXGrid": true, | |
43af96e7 | 227 | "gridLineColor": "rgb(128,128,128)", |
e7746234 EC |
228 | "fillAlpha": 0.15, |
229 | "underlayCallback": null | |
fbe31dc8 | 230 | }; |
fc80a396 | 231 | Dygraph.update(this.options, options); |
6a1aa64f | 232 | |
fbe31dc8 | 233 | this.layout = layout; |
b0c3b730 | 234 | this.element = element; |
fbe31dc8 DV |
235 | this.container = this.element.parentNode; |
236 | ||
fbe31dc8 DV |
237 | this.height = this.element.height; |
238 | this.width = this.element.width; | |
239 | ||
240 | // --- check whether everything is ok before we return | |
241 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
242 | throw "Canvas is not supported."; | |
243 | ||
244 | // internal state | |
245 | this.xlabels = new Array(); | |
246 | this.ylabels = new Array(); | |
ce49c2fa | 247 | this.annotations = new Array(); |
fbe31dc8 DV |
248 | |
249 | this.area = { | |
250 | x: this.options.yAxisLabelWidth + 2 * this.options.axisTickSize, | |
251 | y: 0 | |
252 | }; | |
253 | this.area.w = this.width - this.area.x - this.options.rightGap; | |
254 | this.area.h = this.height - this.options.axisLabelFontSize - | |
255 | 2 * this.options.axisTickSize; | |
256 | ||
b0c3b730 DV |
257 | this.container.style.position = "relative"; |
258 | this.container.style.width = this.width + "px"; | |
fbe31dc8 DV |
259 | }; |
260 | ||
261 | DygraphCanvasRenderer.prototype.clear = function() { | |
262 | if (this.isIE) { | |
263 | // VML takes a while to start up, so we just poll every this.IEDelay | |
264 | try { | |
265 | if (this.clearDelay) { | |
266 | this.clearDelay.cancel(); | |
267 | this.clearDelay = null; | |
268 | } | |
269 | var context = this.element.getContext("2d"); | |
270 | } | |
271 | catch (e) { | |
76171648 | 272 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
273 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
274 | this.clearDelay.addCallback(bind(this.clear, this)); | |
275 | return; | |
276 | } | |
277 | } | |
278 | ||
279 | var context = this.element.getContext("2d"); | |
280 | context.clearRect(0, 0, this.width, this.height); | |
281 | ||
2160ed4a | 282 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 DV |
283 | var el = this.xlabels[i]; |
284 | el.parentNode.removeChild(el); | |
2160ed4a DV |
285 | } |
286 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 DV |
287 | var el = this.ylabels[i]; |
288 | el.parentNode.removeChild(el); | |
2160ed4a | 289 | } |
ce49c2fa DV |
290 | for (var i = 0; i < this.annotations.length; i++) { |
291 | var el = this.annotations[i]; | |
292 | el.parentNode.removeChild(el); | |
293 | } | |
fbe31dc8 DV |
294 | this.xlabels = new Array(); |
295 | this.ylabels = new Array(); | |
ce49c2fa | 296 | this.annotations = new Array(); |
fbe31dc8 DV |
297 | }; |
298 | ||
299 | ||
300 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
301 | var canvas = null; | |
302 | try { | |
21d3323f | 303 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 304 | canvas = document.createElement("canvas"); |
fbe31dc8 | 305 | else |
b0c3b730 | 306 | canvas = canvasName; |
fbe31dc8 DV |
307 | var context = canvas.getContext("2d"); |
308 | } | |
309 | catch (e) { | |
310 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
311 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
312 | if ((!ie) || (ie[1] < 6) || (opera)) | |
313 | return false; | |
314 | return true; | |
315 | } | |
316 | return true; | |
6a1aa64f | 317 | }; |
6a1aa64f DV |
318 | |
319 | /** | |
320 | * Draw an X/Y grid on top of the existing plot | |
321 | */ | |
285a6bda | 322 | DygraphCanvasRenderer.prototype.render = function() { |
6a1aa64f DV |
323 | // Draw the new X/Y grid |
324 | var ctx = this.element.getContext("2d"); | |
e7746234 EC |
325 | |
326 | if (this.options.underlayCallback) { | |
d9e6fa47 | 327 | this.options.underlayCallback(ctx, this.area, this.layout, this.dygraph_); |
e7746234 EC |
328 | } |
329 | ||
6a1aa64f DV |
330 | if (this.options.drawYGrid) { |
331 | var ticks = this.layout.yticks; | |
332 | ctx.save(); | |
f2f24402 | 333 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
334 | ctx.lineWidth = this.options.axisLineWidth; |
335 | for (var i = 0; i < ticks.length; i++) { | |
336 | var x = this.area.x; | |
337 | var y = this.area.y + ticks[i][0] * this.area.h; | |
338 | ctx.beginPath(); | |
339 | ctx.moveTo(x, y); | |
340 | ctx.lineTo(x + this.area.w, y); | |
341 | ctx.closePath(); | |
342 | ctx.stroke(); | |
343 | } | |
344 | } | |
345 | ||
346 | if (this.options.drawXGrid) { | |
347 | var ticks = this.layout.xticks; | |
348 | ctx.save(); | |
f2f24402 | 349 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
350 | ctx.lineWidth = this.options.axisLineWidth; |
351 | for (var i=0; i<ticks.length; i++) { | |
352 | var x = this.area.x + ticks[i][0] * this.area.w; | |
353 | var y = this.area.y + this.area.h; | |
354 | ctx.beginPath(); | |
355 | ctx.moveTo(x, y); | |
356 | ctx.lineTo(x, this.area.y); | |
357 | ctx.closePath(); | |
358 | ctx.stroke(); | |
359 | } | |
360 | } | |
2ce09b19 DV |
361 | |
362 | // Do the ordinary rendering, as before | |
2ce09b19 | 363 | this._renderLineChart(); |
fbe31dc8 | 364 | this._renderAxis(); |
ce49c2fa | 365 | this._renderAnnotations(); |
fbe31dc8 DV |
366 | }; |
367 | ||
368 | ||
369 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
370 | if (!this.options.drawXAxis && !this.options.drawYAxis) | |
371 | return; | |
372 | ||
373 | var context = this.element.getContext("2d"); | |
374 | ||
34fedff8 DV |
375 | var labelStyle = { |
376 | "position": "absolute", | |
377 | "fontSize": this.options.axisLabelFontSize + "px", | |
378 | "zIndex": 10, | |
f474c2a3 | 379 | "color": this.options.axisLabelColor, |
34fedff8 DV |
380 | "width": this.options.axisLabelWidth + "px", |
381 | "overflow": "hidden" | |
382 | }; | |
383 | var makeDiv = function(txt) { | |
384 | var div = document.createElement("div"); | |
385 | for (var name in labelStyle) { | |
85b99f0b DV |
386 | if (labelStyle.hasOwnProperty(name)) { |
387 | div.style[name] = labelStyle[name]; | |
388 | } | |
fbe31dc8 | 389 | } |
34fedff8 DV |
390 | div.appendChild(document.createTextNode(txt)); |
391 | return div; | |
fbe31dc8 DV |
392 | }; |
393 | ||
394 | // axis lines | |
395 | context.save(); | |
f474c2a3 | 396 | context.strokeStyle = this.options.axisLineColor; |
fbe31dc8 DV |
397 | context.lineWidth = this.options.axisLineWidth; |
398 | ||
fbe31dc8 | 399 | if (this.options.drawYAxis) { |
8b7a0cc3 | 400 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
2160ed4a DV |
401 | for (var i = 0; i < this.layout.yticks.length; i++) { |
402 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
403 | if (typeof(tick) == "function") return; |
404 | var x = this.area.x; | |
405 | var y = this.area.y + tick[0] * this.area.h; | |
406 | context.beginPath(); | |
407 | context.moveTo(x, y); | |
408 | context.lineTo(x - this.options.axisTickSize, y); | |
409 | context.closePath(); | |
410 | context.stroke(); | |
411 | ||
34fedff8 | 412 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
413 | var top = (y - this.options.axisLabelFontSize / 2); |
414 | if (top < 0) top = 0; | |
415 | ||
416 | if (top + this.options.axisLabelFontSize + 3 > this.height) { | |
417 | label.style.bottom = "0px"; | |
418 | } else { | |
419 | label.style.top = top + "px"; | |
420 | } | |
421 | label.style.left = "0px"; | |
422 | label.style.textAlign = "right"; | |
423 | label.style.width = this.options.yAxisLabelWidth + "px"; | |
b0c3b730 | 424 | this.container.appendChild(label); |
fbe31dc8 | 425 | this.ylabels.push(label); |
2160ed4a | 426 | } |
fbe31dc8 DV |
427 | |
428 | // The lowest tick on the y-axis often overlaps with the leftmost | |
429 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
430 | // compensate if necessary. | |
431 | var bottomTick = this.ylabels[0]; | |
432 | var fontSize = this.options.axisLabelFontSize; | |
433 | var bottom = parseInt(bottomTick.style.top) + fontSize; | |
434 | if (bottom > this.height - fontSize) { | |
435 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
436 | fontSize / 2) + "px"; | |
437 | } | |
438 | } | |
439 | ||
440 | context.beginPath(); | |
441 | context.moveTo(this.area.x, this.area.y); | |
442 | context.lineTo(this.area.x, this.area.y + this.area.h); | |
443 | context.closePath(); | |
444 | context.stroke(); | |
445 | } | |
446 | ||
447 | if (this.options.drawXAxis) { | |
448 | if (this.layout.xticks) { | |
2160ed4a DV |
449 | for (var i = 0; i < this.layout.xticks.length; i++) { |
450 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
451 | if (typeof(dataset) == "function") return; |
452 | ||
453 | var x = this.area.x + tick[0] * this.area.w; | |
454 | var y = this.area.y + this.area.h; | |
455 | context.beginPath(); | |
456 | context.moveTo(x, y); | |
457 | context.lineTo(x, y + this.options.axisTickSize); | |
458 | context.closePath(); | |
459 | context.stroke(); | |
460 | ||
34fedff8 | 461 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
462 | label.style.textAlign = "center"; |
463 | label.style.bottom = "0px"; | |
464 | ||
465 | var left = (x - this.options.axisLabelWidth/2); | |
466 | if (left + this.options.axisLabelWidth > this.width) { | |
467 | left = this.width - this.options.xAxisLabelWidth; | |
468 | label.style.textAlign = "right"; | |
469 | } | |
470 | if (left < 0) { | |
471 | left = 0; | |
472 | label.style.textAlign = "left"; | |
473 | } | |
474 | ||
475 | label.style.left = left + "px"; | |
476 | label.style.width = this.options.xAxisLabelWidth + "px"; | |
b0c3b730 | 477 | this.container.appendChild(label); |
fbe31dc8 | 478 | this.xlabels.push(label); |
2160ed4a | 479 | } |
fbe31dc8 DV |
480 | } |
481 | ||
482 | context.beginPath(); | |
483 | context.moveTo(this.area.x, this.area.y + this.area.h); | |
484 | context.lineTo(this.area.x + this.area.w, this.area.y + this.area.h); | |
485 | context.closePath(); | |
486 | context.stroke(); | |
487 | } | |
488 | ||
489 | context.restore(); | |
6a1aa64f DV |
490 | }; |
491 | ||
fbe31dc8 | 492 | |
ce49c2fa DV |
493 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
494 | var annotationStyle = { | |
495 | "position": "absolute", | |
496 | "fontSize": this.options.axisLabelFontSize + "px", | |
497 | "zIndex": 10, | |
498 | "width": "20px", | |
499 | "overflow": "hidden", | |
ce49c2fa DV |
500 | }; |
501 | ||
502 | // Get a list of point with annotations. | |
503 | var points = this.layout.annotated_points; | |
504 | for (var i = 0; i < points.length; i++) { | |
505 | var p = points[i]; | |
506 | var div = document.createElement("div"); | |
507 | for (var name in annotationStyle) { | |
508 | if (annotationStyle.hasOwnProperty(name)) { | |
509 | div.style[name] = annotationStyle[name]; | |
510 | } | |
511 | } | |
5c528fa2 DV |
512 | div.className = "dygraphDefaultAnnotation"; |
513 | if (p.annotation.hasOwnProperty('cssClass')) { | |
514 | div.className += " " + p.annotation.cssClass; | |
515 | } | |
ce49c2fa DV |
516 | div.appendChild(document.createTextNode(p.annotation.shortText)); |
517 | div.style.left = (p.canvasx - 10) + "px"; | |
518 | div.style.top = p.canvasy + "px"; | |
519 | div.title = p.annotation.text; | |
520 | div.style.color = this.colors[p.name]; | |
521 | div.style.borderColor = this.colors[p.name]; | |
522 | this.container.appendChild(div); | |
523 | this.annotations.push(div); | |
524 | } | |
525 | }; | |
526 | ||
527 | ||
6a1aa64f DV |
528 | /** |
529 | * Overrides the CanvasRenderer method to draw error bars | |
530 | */ | |
285a6bda | 531 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
6a1aa64f DV |
532 | var context = this.element.getContext("2d"); |
533 | var colorCount = this.options.colorScheme.length; | |
534 | var colorScheme = this.options.colorScheme; | |
43af96e7 | 535 | var fillAlpha = this.options.fillAlpha; |
6a1aa64f | 536 | var errorBars = this.layout.options.errorBars; |
5954ef32 | 537 | var fillGraph = this.layout.options.fillGraph; |
354e15ab | 538 | var stackedGraph = this.layout.options.stackedGraph; |
afdc483f | 539 | var stepPlot = this.layout.options.stepPlot; |
21d3323f DV |
540 | |
541 | var setNames = []; | |
ca43052c | 542 | for (var name in this.layout.datasets) { |
85b99f0b DV |
543 | if (this.layout.datasets.hasOwnProperty(name)) { |
544 | setNames.push(name); | |
545 | } | |
ca43052c | 546 | } |
21d3323f | 547 | var setCount = setNames.length; |
6a1aa64f | 548 | |
f032c51d AV |
549 | this.colors = {} |
550 | for (var i = 0; i < setCount; i++) { | |
551 | this.colors[setNames[i]] = colorScheme[i % colorCount]; | |
552 | } | |
553 | ||
ff00d3e2 DV |
554 | // Update Points |
555 | // TODO(danvk): here | |
2160ed4a DV |
556 | for (var i = 0; i < this.layout.points.length; i++) { |
557 | var point = this.layout.points[i]; | |
6a1aa64f DV |
558 | point.canvasx = this.area.w * point.x + this.area.x; |
559 | point.canvasy = this.area.h * point.y + this.area.y; | |
560 | } | |
6a1aa64f DV |
561 | |
562 | // create paths | |
9317362d | 563 | var isOK = function(x) { return x && !isNaN(x); }; |
6a1aa64f | 564 | |
80aaae18 DV |
565 | var ctx = context; |
566 | if (errorBars) { | |
6a834bbb DV |
567 | if (fillGraph) { |
568 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
569 | } | |
570 | ||
6a1aa64f DV |
571 | for (var i = 0; i < setCount; i++) { |
572 | var setName = setNames[i]; | |
f032c51d | 573 | var color = this.colors[setName]; |
6a1aa64f DV |
574 | |
575 | // setup graphics context | |
80aaae18 | 576 | ctx.save(); |
56623f3b | 577 | var prevX = NaN; |
afdc483f | 578 | var prevY = NaN; |
6a1aa64f | 579 | var prevYs = [-1, -1]; |
6a1aa64f | 580 | var yscale = this.layout.yscale; |
f474c2a3 DV |
581 | // should be same color as the lines but only 15% opaque. |
582 | var rgb = new RGBColor(color); | |
43af96e7 NK |
583 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
584 | fillAlpha + ')'; | |
f474c2a3 | 585 | ctx.fillStyle = err_color; |
05c9d0c4 DV |
586 | ctx.beginPath(); |
587 | for (var j = 0; j < this.layout.points.length; j++) { | |
588 | var point = this.layout.points[j]; | |
6a1aa64f | 589 | if (point.name == setName) { |
5954ef32 | 590 | if (!isOK(point.y)) { |
56623f3b | 591 | prevX = NaN; |
ae85914a | 592 | continue; |
5011e7a1 | 593 | } |
ce49c2fa | 594 | |
ff00d3e2 | 595 | // TODO(danvk): here |
afdc483f NN |
596 | if (stepPlot) { |
597 | var newYs = [ prevY - point.errorPlus * yscale, | |
47600757 | 598 | prevY + point.errorMinus * yscale ]; |
afdc483f NN |
599 | prevY = point.y; |
600 | } else { | |
601 | var newYs = [ point.y - point.errorPlus * yscale, | |
47600757 | 602 | point.y + point.errorMinus * yscale ]; |
afdc483f | 603 | } |
6a1aa64f DV |
604 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
605 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 606 | if (!isNaN(prevX)) { |
afdc483f | 607 | if (stepPlot) { |
47600757 | 608 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 609 | } else { |
47600757 | 610 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 611 | } |
5954ef32 DV |
612 | ctx.lineTo(point.canvasx, newYs[0]); |
613 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 614 | if (stepPlot) { |
47600757 | 615 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 616 | } else { |
47600757 | 617 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 618 | } |
5954ef32 DV |
619 | ctx.closePath(); |
620 | } | |
354e15ab | 621 | prevYs = newYs; |
5954ef32 DV |
622 | prevX = point.canvasx; |
623 | } | |
624 | } | |
625 | ctx.fill(); | |
626 | } | |
627 | } else if (fillGraph) { | |
354e15ab DE |
628 | var axisY = 1.0 + this.layout.minyval * this.layout.yscale; |
629 | if (axisY < 0.0) axisY = 0.0; | |
630 | else if (axisY > 1.0) axisY = 1.0; | |
631 | axisY = this.area.h * axisY + this.area.y; | |
632 | ||
633 | var baseline = [] // for stacked graphs: baseline for filling | |
634 | ||
635 | // process sets in reverse order (needed for stacked graphs) | |
636 | for (var i = setCount - 1; i >= 0; i--) { | |
5954ef32 | 637 | var setName = setNames[i]; |
f032c51d | 638 | var color = this.colors[setName]; |
5954ef32 DV |
639 | |
640 | // setup graphics context | |
641 | ctx.save(); | |
56623f3b | 642 | var prevX = NaN; |
5954ef32 | 643 | var prevYs = [-1, -1]; |
5954ef32 DV |
644 | var yscale = this.layout.yscale; |
645 | // should be same color as the lines but only 15% opaque. | |
646 | var rgb = new RGBColor(color); | |
43af96e7 NK |
647 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
648 | fillAlpha + ')'; | |
5954ef32 DV |
649 | ctx.fillStyle = err_color; |
650 | ctx.beginPath(); | |
651 | for (var j = 0; j < this.layout.points.length; j++) { | |
652 | var point = this.layout.points[j]; | |
5954ef32 DV |
653 | if (point.name == setName) { |
654 | if (!isOK(point.y)) { | |
56623f3b | 655 | prevX = NaN; |
5954ef32 DV |
656 | continue; |
657 | } | |
354e15ab DE |
658 | var newYs; |
659 | if (stackedGraph) { | |
660 | lastY = baseline[point.canvasx]; | |
661 | if (lastY === undefined) lastY = axisY; | |
662 | baseline[point.canvasx] = point.canvasy; | |
663 | newYs = [ point.canvasy, lastY ]; | |
664 | } else { | |
665 | newYs = [ point.canvasy, axisY ]; | |
666 | } | |
56623f3b | 667 | if (!isNaN(prevX)) { |
05c9d0c4 | 668 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 669 | if (stepPlot) { |
47600757 | 670 | ctx.lineTo(point.canvasx, prevYs[0]); |
afdc483f | 671 | } else { |
47600757 | 672 | ctx.lineTo(point.canvasx, newYs[0]); |
afdc483f | 673 | } |
05c9d0c4 DV |
674 | ctx.lineTo(point.canvasx, newYs[1]); |
675 | ctx.lineTo(prevX, prevYs[1]); | |
676 | ctx.closePath(); | |
6a1aa64f | 677 | } |
354e15ab | 678 | prevYs = newYs; |
6a1aa64f DV |
679 | prevX = point.canvasx; |
680 | } | |
05c9d0c4 | 681 | } |
6a1aa64f DV |
682 | ctx.fill(); |
683 | } | |
80aaae18 DV |
684 | } |
685 | ||
686 | for (var i = 0; i < setCount; i++) { | |
687 | var setName = setNames[i]; | |
f032c51d | 688 | var color = this.colors[setName]; |
80aaae18 DV |
689 | |
690 | // setup graphics context | |
691 | context.save(); | |
692 | var point = this.layout.points[0]; | |
693 | var pointSize = this.dygraph_.attr_("pointSize"); | |
694 | var prevX = null, prevY = null; | |
695 | var drawPoints = this.dygraph_.attr_("drawPoints"); | |
696 | var points = this.layout.points; | |
697 | for (var j = 0; j < points.length; j++) { | |
698 | var point = points[j]; | |
699 | if (point.name == setName) { | |
700 | if (!isOK(point.canvasy)) { | |
701 | // this will make us move to the next point, not draw a line to it. | |
702 | prevX = prevY = null; | |
703 | } else { | |
704 | // A point is "isolated" if it is non-null but both the previous | |
705 | // and next points are null. | |
706 | var isIsolated = (!prevX && (j == points.length - 1 || | |
707 | !isOK(points[j+1].canvasy))); | |
708 | ||
709 | if (!prevX) { | |
710 | prevX = point.canvasx; | |
711 | prevY = point.canvasy; | |
712 | } else { | |
713 | ctx.beginPath(); | |
714 | ctx.strokeStyle = color; | |
715 | ctx.lineWidth = this.options.strokeWidth; | |
716 | ctx.moveTo(prevX, prevY); | |
afdc483f | 717 | if (stepPlot) { |
47600757 | 718 | ctx.lineTo(point.canvasx, prevY); |
afdc483f | 719 | } |
80aaae18 DV |
720 | prevX = point.canvasx; |
721 | prevY = point.canvasy; | |
722 | ctx.lineTo(prevX, prevY); | |
723 | ctx.stroke(); | |
724 | } | |
725 | ||
726 | if (drawPoints || isIsolated) { | |
727 | ctx.beginPath(); | |
728 | ctx.fillStyle = color; | |
7bf6a9fe DV |
729 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
730 | 0, 2 * Math.PI, false); | |
80aaae18 DV |
731 | ctx.fill(); |
732 | } | |
733 | } | |
734 | } | |
735 | } | |
736 | } | |
6a1aa64f | 737 | |
6a1aa64f DV |
738 | context.restore(); |
739 | }; |