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 | ||
198 | // Subclass PlotKit.CanvasRenderer to add: | |
199 | // 1. X/Y grid overlay | |
200 | // 2. Ability to draw error bars (if required) | |
201 | ||
202 | /** | |
203 | * Sets some PlotKit.CanvasRenderer options | |
204 | * @param {Object} element The canvas to attach to | |
285a6bda | 205 | * @param {Layout} layout The DygraphLayout object for this graph. |
6a1aa64f DV |
206 | * @param {Object} options Options to pass on to CanvasRenderer |
207 | */ | |
9317362d DV |
208 | DygraphCanvasRenderer = function(dygraph, element, layout, options) { |
209 | // TODO(danvk): remove options, just use dygraph.attr_. | |
9317362d | 210 | this.dygraph_ = dygraph; |
fbe31dc8 DV |
211 | |
212 | // default options | |
213 | this.options = { | |
f474c2a3 DV |
214 | "strokeWidth": 0.5, |
215 | "drawXAxis": true, | |
216 | "drawYAxis": true, | |
217 | "axisLineColor": "black", | |
218 | "axisLineWidth": 0.5, | |
219 | "axisTickSize": 3, | |
220 | "axisLabelColor": "black", | |
221 | "axisLabelFont": "Arial", | |
222 | "axisLabelFontSize": 9, | |
223 | "axisLabelWidth": 50, | |
224 | "drawYGrid": true, | |
225 | "drawXGrid": true, | |
43af96e7 | 226 | "gridLineColor": "rgb(128,128,128)", |
e7746234 EC |
227 | "fillAlpha": 0.15, |
228 | "underlayCallback": null | |
fbe31dc8 | 229 | }; |
fc80a396 | 230 | Dygraph.update(this.options, options); |
6a1aa64f | 231 | |
fbe31dc8 | 232 | this.layout = layout; |
b0c3b730 | 233 | this.element = element; |
fbe31dc8 DV |
234 | this.container = this.element.parentNode; |
235 | ||
fbe31dc8 DV |
236 | this.height = this.element.height; |
237 | this.width = this.element.width; | |
238 | ||
239 | // --- check whether everything is ok before we return | |
240 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
241 | throw "Canvas is not supported."; | |
242 | ||
243 | // internal state | |
244 | this.xlabels = new Array(); | |
245 | this.ylabels = new Array(); | |
ce49c2fa | 246 | this.annotations = new Array(); |
fbe31dc8 DV |
247 | |
248 | this.area = { | |
249 | x: this.options.yAxisLabelWidth + 2 * this.options.axisTickSize, | |
250 | y: 0 | |
251 | }; | |
252 | this.area.w = this.width - this.area.x - this.options.rightGap; | |
253 | this.area.h = this.height - this.options.axisLabelFontSize - | |
254 | 2 * this.options.axisTickSize; | |
255 | ||
b0c3b730 DV |
256 | this.container.style.position = "relative"; |
257 | this.container.style.width = this.width + "px"; | |
fbe31dc8 DV |
258 | }; |
259 | ||
260 | DygraphCanvasRenderer.prototype.clear = function() { | |
261 | if (this.isIE) { | |
262 | // VML takes a while to start up, so we just poll every this.IEDelay | |
263 | try { | |
264 | if (this.clearDelay) { | |
265 | this.clearDelay.cancel(); | |
266 | this.clearDelay = null; | |
267 | } | |
268 | var context = this.element.getContext("2d"); | |
269 | } | |
270 | catch (e) { | |
76171648 | 271 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
272 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
273 | this.clearDelay.addCallback(bind(this.clear, this)); | |
274 | return; | |
275 | } | |
276 | } | |
277 | ||
278 | var context = this.element.getContext("2d"); | |
279 | context.clearRect(0, 0, this.width, this.height); | |
280 | ||
2160ed4a | 281 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 DV |
282 | var el = this.xlabels[i]; |
283 | el.parentNode.removeChild(el); | |
2160ed4a DV |
284 | } |
285 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 DV |
286 | var el = this.ylabels[i]; |
287 | el.parentNode.removeChild(el); | |
2160ed4a | 288 | } |
ce49c2fa DV |
289 | for (var i = 0; i < this.annotations.length; i++) { |
290 | var el = this.annotations[i]; | |
291 | el.parentNode.removeChild(el); | |
292 | } | |
fbe31dc8 DV |
293 | this.xlabels = new Array(); |
294 | this.ylabels = new Array(); | |
ce49c2fa | 295 | this.annotations = new Array(); |
fbe31dc8 DV |
296 | }; |
297 | ||
298 | ||
299 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
300 | var canvas = null; | |
301 | try { | |
21d3323f | 302 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 303 | canvas = document.createElement("canvas"); |
fbe31dc8 | 304 | else |
b0c3b730 | 305 | canvas = canvasName; |
fbe31dc8 DV |
306 | var context = canvas.getContext("2d"); |
307 | } | |
308 | catch (e) { | |
309 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
310 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
311 | if ((!ie) || (ie[1] < 6) || (opera)) | |
312 | return false; | |
313 | return true; | |
314 | } | |
315 | return true; | |
6a1aa64f | 316 | }; |
6a1aa64f DV |
317 | |
318 | /** | |
319 | * Draw an X/Y grid on top of the existing plot | |
320 | */ | |
285a6bda | 321 | DygraphCanvasRenderer.prototype.render = function() { |
6a1aa64f DV |
322 | // Draw the new X/Y grid |
323 | var ctx = this.element.getContext("2d"); | |
e7746234 EC |
324 | |
325 | if (this.options.underlayCallback) { | |
d9e6fa47 | 326 | this.options.underlayCallback(ctx, this.area, this.layout, this.dygraph_); |
e7746234 EC |
327 | } |
328 | ||
6a1aa64f DV |
329 | if (this.options.drawYGrid) { |
330 | var ticks = this.layout.yticks; | |
331 | ctx.save(); | |
f2f24402 | 332 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
333 | ctx.lineWidth = this.options.axisLineWidth; |
334 | for (var i = 0; i < ticks.length; i++) { | |
335 | var x = this.area.x; | |
336 | var y = this.area.y + ticks[i][0] * this.area.h; | |
337 | ctx.beginPath(); | |
338 | ctx.moveTo(x, y); | |
339 | ctx.lineTo(x + this.area.w, y); | |
340 | ctx.closePath(); | |
341 | ctx.stroke(); | |
342 | } | |
343 | } | |
344 | ||
345 | if (this.options.drawXGrid) { | |
346 | var ticks = this.layout.xticks; | |
347 | ctx.save(); | |
f2f24402 | 348 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
349 | ctx.lineWidth = this.options.axisLineWidth; |
350 | for (var i=0; i<ticks.length; i++) { | |
351 | var x = this.area.x + ticks[i][0] * this.area.w; | |
352 | var y = this.area.y + this.area.h; | |
353 | ctx.beginPath(); | |
354 | ctx.moveTo(x, y); | |
355 | ctx.lineTo(x, this.area.y); | |
356 | ctx.closePath(); | |
357 | ctx.stroke(); | |
358 | } | |
359 | } | |
2ce09b19 DV |
360 | |
361 | // Do the ordinary rendering, as before | |
2ce09b19 | 362 | this._renderLineChart(); |
fbe31dc8 | 363 | this._renderAxis(); |
ce49c2fa | 364 | this._renderAnnotations(); |
fbe31dc8 DV |
365 | }; |
366 | ||
367 | ||
368 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
369 | if (!this.options.drawXAxis && !this.options.drawYAxis) | |
370 | return; | |
371 | ||
372 | var context = this.element.getContext("2d"); | |
373 | ||
34fedff8 DV |
374 | var labelStyle = { |
375 | "position": "absolute", | |
376 | "fontSize": this.options.axisLabelFontSize + "px", | |
377 | "zIndex": 10, | |
f474c2a3 | 378 | "color": this.options.axisLabelColor, |
34fedff8 DV |
379 | "width": this.options.axisLabelWidth + "px", |
380 | "overflow": "hidden" | |
381 | }; | |
382 | var makeDiv = function(txt) { | |
383 | var div = document.createElement("div"); | |
384 | for (var name in labelStyle) { | |
85b99f0b DV |
385 | if (labelStyle.hasOwnProperty(name)) { |
386 | div.style[name] = labelStyle[name]; | |
387 | } | |
fbe31dc8 | 388 | } |
34fedff8 DV |
389 | div.appendChild(document.createTextNode(txt)); |
390 | return div; | |
fbe31dc8 DV |
391 | }; |
392 | ||
393 | // axis lines | |
394 | context.save(); | |
f474c2a3 | 395 | context.strokeStyle = this.options.axisLineColor; |
fbe31dc8 DV |
396 | context.lineWidth = this.options.axisLineWidth; |
397 | ||
fbe31dc8 | 398 | if (this.options.drawYAxis) { |
8b7a0cc3 | 399 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
2160ed4a DV |
400 | for (var i = 0; i < this.layout.yticks.length; i++) { |
401 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
402 | if (typeof(tick) == "function") return; |
403 | var x = this.area.x; | |
404 | var y = this.area.y + tick[0] * this.area.h; | |
405 | context.beginPath(); | |
406 | context.moveTo(x, y); | |
407 | context.lineTo(x - this.options.axisTickSize, y); | |
408 | context.closePath(); | |
409 | context.stroke(); | |
410 | ||
34fedff8 | 411 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
412 | var top = (y - this.options.axisLabelFontSize / 2); |
413 | if (top < 0) top = 0; | |
414 | ||
415 | if (top + this.options.axisLabelFontSize + 3 > this.height) { | |
416 | label.style.bottom = "0px"; | |
417 | } else { | |
418 | label.style.top = top + "px"; | |
419 | } | |
420 | label.style.left = "0px"; | |
421 | label.style.textAlign = "right"; | |
422 | label.style.width = this.options.yAxisLabelWidth + "px"; | |
b0c3b730 | 423 | this.container.appendChild(label); |
fbe31dc8 | 424 | this.ylabels.push(label); |
2160ed4a | 425 | } |
fbe31dc8 DV |
426 | |
427 | // The lowest tick on the y-axis often overlaps with the leftmost | |
428 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
429 | // compensate if necessary. | |
430 | var bottomTick = this.ylabels[0]; | |
431 | var fontSize = this.options.axisLabelFontSize; | |
432 | var bottom = parseInt(bottomTick.style.top) + fontSize; | |
433 | if (bottom > this.height - fontSize) { | |
434 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
435 | fontSize / 2) + "px"; | |
436 | } | |
437 | } | |
438 | ||
439 | context.beginPath(); | |
440 | context.moveTo(this.area.x, this.area.y); | |
441 | context.lineTo(this.area.x, this.area.y + this.area.h); | |
442 | context.closePath(); | |
443 | context.stroke(); | |
444 | } | |
445 | ||
446 | if (this.options.drawXAxis) { | |
447 | if (this.layout.xticks) { | |
2160ed4a DV |
448 | for (var i = 0; i < this.layout.xticks.length; i++) { |
449 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
450 | if (typeof(dataset) == "function") return; |
451 | ||
452 | var x = this.area.x + tick[0] * this.area.w; | |
453 | var y = this.area.y + this.area.h; | |
454 | context.beginPath(); | |
455 | context.moveTo(x, y); | |
456 | context.lineTo(x, y + this.options.axisTickSize); | |
457 | context.closePath(); | |
458 | context.stroke(); | |
459 | ||
34fedff8 | 460 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
461 | label.style.textAlign = "center"; |
462 | label.style.bottom = "0px"; | |
463 | ||
464 | var left = (x - this.options.axisLabelWidth/2); | |
465 | if (left + this.options.axisLabelWidth > this.width) { | |
466 | left = this.width - this.options.xAxisLabelWidth; | |
467 | label.style.textAlign = "right"; | |
468 | } | |
469 | if (left < 0) { | |
470 | left = 0; | |
471 | label.style.textAlign = "left"; | |
472 | } | |
473 | ||
474 | label.style.left = left + "px"; | |
475 | label.style.width = this.options.xAxisLabelWidth + "px"; | |
b0c3b730 | 476 | this.container.appendChild(label); |
fbe31dc8 | 477 | this.xlabels.push(label); |
2160ed4a | 478 | } |
fbe31dc8 DV |
479 | } |
480 | ||
481 | context.beginPath(); | |
482 | context.moveTo(this.area.x, this.area.y + this.area.h); | |
483 | context.lineTo(this.area.x + this.area.w, this.area.y + this.area.h); | |
484 | context.closePath(); | |
485 | context.stroke(); | |
486 | } | |
487 | ||
488 | context.restore(); | |
6a1aa64f DV |
489 | }; |
490 | ||
fbe31dc8 | 491 | |
ce49c2fa DV |
492 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
493 | var annotationStyle = { | |
494 | "position": "absolute", | |
495 | "fontSize": this.options.axisLabelFontSize + "px", | |
496 | "zIndex": 10, | |
3bf2fa91 | 497 | "overflow": "hidden" |
ce49c2fa DV |
498 | }; |
499 | ||
ab5e5c75 DV |
500 | var bindEvt = function(eventName, classEventName, p, self) { |
501 | return function(e) { | |
502 | var a = p.annotation; | |
503 | if (a.hasOwnProperty(eventName)) { | |
504 | a[eventName](a, p, self.dygraph_, e); | |
505 | } else if (self.dygraph_.attr_(classEventName)) { | |
506 | self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e ); | |
507 | } | |
508 | }; | |
509 | } | |
510 | ||
ce49c2fa DV |
511 | // Get a list of point with annotations. |
512 | var points = this.layout.annotated_points; | |
513 | for (var i = 0; i < points.length; i++) { | |
514 | var p = points[i]; | |
e6d53148 DV |
515 | if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { |
516 | continue; | |
517 | } | |
518 | ||
ce5e8d36 DV |
519 | var a = p.annotation; |
520 | var tick_height = 6; | |
521 | if (a.hasOwnProperty("tickHeight")) { | |
522 | tick_height = a.tickHeight; | |
9a40897e DV |
523 | } |
524 | ||
ce49c2fa DV |
525 | var div = document.createElement("div"); |
526 | for (var name in annotationStyle) { | |
527 | if (annotationStyle.hasOwnProperty(name)) { | |
528 | div.style[name] = annotationStyle[name]; | |
529 | } | |
530 | } | |
ce5e8d36 DV |
531 | if (!a.hasOwnProperty('icon')) { |
532 | div.className = "dygraphDefaultAnnotation"; | |
533 | } | |
534 | if (a.hasOwnProperty('cssClass')) { | |
535 | div.className += " " + a.cssClass; | |
536 | } | |
537 | ||
a5ad69cc DV |
538 | var width = a.hasOwnProperty('width') ? a.width : 16; |
539 | var height = a.hasOwnProperty('height') ? a.height : 16; | |
ce5e8d36 DV |
540 | if (a.hasOwnProperty('icon')) { |
541 | var img = document.createElement("img"); | |
542 | img.src = a.icon; | |
33030f33 DV |
543 | img.width = width; |
544 | img.height = height; | |
ce5e8d36 DV |
545 | div.appendChild(img); |
546 | } else if (p.annotation.hasOwnProperty('shortText')) { | |
547 | div.appendChild(document.createTextNode(p.annotation.shortText)); | |
5c528fa2 | 548 | } |
ce5e8d36 | 549 | div.style.left = (p.canvasx - width / 2) + "px"; |
d14b9eed DV |
550 | if (a.attachAtBottom) { |
551 | div.style.top = (this.area.h - height - tick_height) + "px"; | |
552 | } else { | |
553 | div.style.top = (p.canvasy - height - tick_height) + "px"; | |
554 | } | |
ce5e8d36 DV |
555 | div.style.width = width + "px"; |
556 | div.style.height = height + "px"; | |
ce49c2fa DV |
557 | div.title = p.annotation.text; |
558 | div.style.color = this.colors[p.name]; | |
559 | div.style.borderColor = this.colors[p.name]; | |
e6d53148 | 560 | a.div = div; |
ab5e5c75 | 561 | |
9a40897e DV |
562 | Dygraph.addEvent(div, 'click', |
563 | bindEvt('clickHandler', 'annotationClickHandler', p, this)); | |
564 | Dygraph.addEvent(div, 'mouseover', | |
565 | bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); | |
566 | Dygraph.addEvent(div, 'mouseout', | |
567 | bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); | |
568 | Dygraph.addEvent(div, 'dblclick', | |
569 | bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); | |
ab5e5c75 | 570 | |
ce49c2fa DV |
571 | this.container.appendChild(div); |
572 | this.annotations.push(div); | |
9a40897e DV |
573 | |
574 | var ctx = this.element.getContext("2d"); | |
575 | ctx.strokeStyle = this.colors[p.name]; | |
576 | ctx.beginPath(); | |
d14b9eed DV |
577 | if (!a.attachAtBottom) { |
578 | ctx.moveTo(p.canvasx, p.canvasy); | |
579 | ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); | |
580 | } else { | |
581 | ctx.moveTo(p.canvasx, this.area.h); | |
582 | ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); | |
583 | } | |
9a40897e DV |
584 | ctx.closePath(); |
585 | ctx.stroke(); | |
ce49c2fa DV |
586 | } |
587 | }; | |
588 | ||
589 | ||
6a1aa64f DV |
590 | /** |
591 | * Overrides the CanvasRenderer method to draw error bars | |
592 | */ | |
285a6bda | 593 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
6a1aa64f DV |
594 | var context = this.element.getContext("2d"); |
595 | var colorCount = this.options.colorScheme.length; | |
596 | var colorScheme = this.options.colorScheme; | |
43af96e7 | 597 | var fillAlpha = this.options.fillAlpha; |
6a1aa64f | 598 | var errorBars = this.layout.options.errorBars; |
5954ef32 | 599 | var fillGraph = this.layout.options.fillGraph; |
354e15ab | 600 | var stackedGraph = this.layout.options.stackedGraph; |
afdc483f | 601 | var stepPlot = this.layout.options.stepPlot; |
21d3323f DV |
602 | |
603 | var setNames = []; | |
ca43052c | 604 | for (var name in this.layout.datasets) { |
85b99f0b DV |
605 | if (this.layout.datasets.hasOwnProperty(name)) { |
606 | setNames.push(name); | |
607 | } | |
ca43052c | 608 | } |
21d3323f | 609 | var setCount = setNames.length; |
6a1aa64f | 610 | |
f032c51d AV |
611 | this.colors = {} |
612 | for (var i = 0; i < setCount; i++) { | |
613 | this.colors[setNames[i]] = colorScheme[i % colorCount]; | |
614 | } | |
615 | ||
ff00d3e2 DV |
616 | // Update Points |
617 | // TODO(danvk): here | |
2160ed4a DV |
618 | for (var i = 0; i < this.layout.points.length; i++) { |
619 | var point = this.layout.points[i]; | |
6a1aa64f DV |
620 | point.canvasx = this.area.w * point.x + this.area.x; |
621 | point.canvasy = this.area.h * point.y + this.area.y; | |
622 | } | |
6a1aa64f DV |
623 | |
624 | // create paths | |
9317362d | 625 | var isOK = function(x) { return x && !isNaN(x); }; |
6a1aa64f | 626 | |
80aaae18 DV |
627 | var ctx = context; |
628 | if (errorBars) { | |
6a834bbb DV |
629 | if (fillGraph) { |
630 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
631 | } | |
632 | ||
6a1aa64f DV |
633 | for (var i = 0; i < setCount; i++) { |
634 | var setName = setNames[i]; | |
f032c51d | 635 | var color = this.colors[setName]; |
6a1aa64f DV |
636 | |
637 | // setup graphics context | |
80aaae18 | 638 | ctx.save(); |
56623f3b | 639 | var prevX = NaN; |
afdc483f | 640 | var prevY = NaN; |
6a1aa64f | 641 | var prevYs = [-1, -1]; |
6a1aa64f | 642 | var yscale = this.layout.yscale; |
f474c2a3 DV |
643 | // should be same color as the lines but only 15% opaque. |
644 | var rgb = new RGBColor(color); | |
43af96e7 NK |
645 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
646 | fillAlpha + ')'; | |
f474c2a3 | 647 | ctx.fillStyle = err_color; |
05c9d0c4 DV |
648 | ctx.beginPath(); |
649 | for (var j = 0; j < this.layout.points.length; j++) { | |
650 | var point = this.layout.points[j]; | |
6a1aa64f | 651 | if (point.name == setName) { |
5954ef32 | 652 | if (!isOK(point.y)) { |
56623f3b | 653 | prevX = NaN; |
ae85914a | 654 | continue; |
5011e7a1 | 655 | } |
ce49c2fa | 656 | |
ff00d3e2 | 657 | // TODO(danvk): here |
afdc483f NN |
658 | if (stepPlot) { |
659 | var newYs = [ prevY - point.errorPlus * yscale, | |
47600757 | 660 | prevY + point.errorMinus * yscale ]; |
afdc483f NN |
661 | prevY = point.y; |
662 | } else { | |
663 | var newYs = [ point.y - point.errorPlus * yscale, | |
47600757 | 664 | point.y + point.errorMinus * yscale ]; |
afdc483f | 665 | } |
6a1aa64f DV |
666 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
667 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 668 | if (!isNaN(prevX)) { |
afdc483f | 669 | if (stepPlot) { |
47600757 | 670 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 671 | } else { |
47600757 | 672 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 673 | } |
5954ef32 DV |
674 | ctx.lineTo(point.canvasx, newYs[0]); |
675 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 676 | if (stepPlot) { |
47600757 | 677 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 678 | } else { |
47600757 | 679 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 680 | } |
5954ef32 DV |
681 | ctx.closePath(); |
682 | } | |
354e15ab | 683 | prevYs = newYs; |
5954ef32 DV |
684 | prevX = point.canvasx; |
685 | } | |
686 | } | |
687 | ctx.fill(); | |
688 | } | |
689 | } else if (fillGraph) { | |
354e15ab DE |
690 | var axisY = 1.0 + this.layout.minyval * this.layout.yscale; |
691 | if (axisY < 0.0) axisY = 0.0; | |
692 | else if (axisY > 1.0) axisY = 1.0; | |
693 | axisY = this.area.h * axisY + this.area.y; | |
694 | ||
695 | var baseline = [] // for stacked graphs: baseline for filling | |
696 | ||
697 | // process sets in reverse order (needed for stacked graphs) | |
698 | for (var i = setCount - 1; i >= 0; i--) { | |
5954ef32 | 699 | var setName = setNames[i]; |
f032c51d | 700 | var color = this.colors[setName]; |
5954ef32 DV |
701 | |
702 | // setup graphics context | |
703 | ctx.save(); | |
56623f3b | 704 | var prevX = NaN; |
5954ef32 | 705 | var prevYs = [-1, -1]; |
5954ef32 DV |
706 | var yscale = this.layout.yscale; |
707 | // should be same color as the lines but only 15% opaque. | |
708 | var rgb = new RGBColor(color); | |
43af96e7 NK |
709 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
710 | fillAlpha + ')'; | |
5954ef32 DV |
711 | ctx.fillStyle = err_color; |
712 | ctx.beginPath(); | |
713 | for (var j = 0; j < this.layout.points.length; j++) { | |
714 | var point = this.layout.points[j]; | |
5954ef32 DV |
715 | if (point.name == setName) { |
716 | if (!isOK(point.y)) { | |
56623f3b | 717 | prevX = NaN; |
5954ef32 DV |
718 | continue; |
719 | } | |
354e15ab DE |
720 | var newYs; |
721 | if (stackedGraph) { | |
722 | lastY = baseline[point.canvasx]; | |
723 | if (lastY === undefined) lastY = axisY; | |
724 | baseline[point.canvasx] = point.canvasy; | |
725 | newYs = [ point.canvasy, lastY ]; | |
726 | } else { | |
727 | newYs = [ point.canvasy, axisY ]; | |
728 | } | |
56623f3b | 729 | if (!isNaN(prevX)) { |
05c9d0c4 | 730 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 731 | if (stepPlot) { |
47600757 | 732 | ctx.lineTo(point.canvasx, prevYs[0]); |
afdc483f | 733 | } else { |
47600757 | 734 | ctx.lineTo(point.canvasx, newYs[0]); |
afdc483f | 735 | } |
05c9d0c4 DV |
736 | ctx.lineTo(point.canvasx, newYs[1]); |
737 | ctx.lineTo(prevX, prevYs[1]); | |
738 | ctx.closePath(); | |
6a1aa64f | 739 | } |
354e15ab | 740 | prevYs = newYs; |
6a1aa64f DV |
741 | prevX = point.canvasx; |
742 | } | |
05c9d0c4 | 743 | } |
6a1aa64f DV |
744 | ctx.fill(); |
745 | } | |
80aaae18 DV |
746 | } |
747 | ||
748 | for (var i = 0; i < setCount; i++) { | |
749 | var setName = setNames[i]; | |
f032c51d | 750 | var color = this.colors[setName]; |
80aaae18 DV |
751 | |
752 | // setup graphics context | |
753 | context.save(); | |
754 | var point = this.layout.points[0]; | |
755 | var pointSize = this.dygraph_.attr_("pointSize"); | |
756 | var prevX = null, prevY = null; | |
757 | var drawPoints = this.dygraph_.attr_("drawPoints"); | |
758 | var points = this.layout.points; | |
759 | for (var j = 0; j < points.length; j++) { | |
760 | var point = points[j]; | |
761 | if (point.name == setName) { | |
762 | if (!isOK(point.canvasy)) { | |
763 | // this will make us move to the next point, not draw a line to it. | |
764 | prevX = prevY = null; | |
765 | } else { | |
766 | // A point is "isolated" if it is non-null but both the previous | |
767 | // and next points are null. | |
768 | var isIsolated = (!prevX && (j == points.length - 1 || | |
769 | !isOK(points[j+1].canvasy))); | |
770 | ||
771 | if (!prevX) { | |
772 | prevX = point.canvasx; | |
773 | prevY = point.canvasy; | |
774 | } else { | |
775 | ctx.beginPath(); | |
776 | ctx.strokeStyle = color; | |
777 | ctx.lineWidth = this.options.strokeWidth; | |
778 | ctx.moveTo(prevX, prevY); | |
afdc483f | 779 | if (stepPlot) { |
47600757 | 780 | ctx.lineTo(point.canvasx, prevY); |
afdc483f | 781 | } |
80aaae18 DV |
782 | prevX = point.canvasx; |
783 | prevY = point.canvasy; | |
784 | ctx.lineTo(prevX, prevY); | |
785 | ctx.stroke(); | |
786 | } | |
787 | ||
788 | if (drawPoints || isIsolated) { | |
789 | ctx.beginPath(); | |
790 | ctx.fillStyle = color; | |
7bf6a9fe DV |
791 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
792 | 0, 2 * Math.PI, false); | |
80aaae18 DV |
793 | ctx.fill(); |
794 | } | |
795 | } | |
796 | } | |
797 | } | |
798 | } | |
6a1aa64f | 799 | |
6a1aa64f DV |
800 | context.restore(); |
801 | }; |