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