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