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