Commit | Line | Data |
---|---|---|
88e95c46 DV |
1 | /** |
2 | * @license | |
3 | * Copyright 2006 Dan Vanderkam (danvdk@gmail.com) | |
4 | * MIT-licensed (http://opensource.org/licenses/MIT) | |
5 | */ | |
6a1aa64f DV |
6 | |
7 | /** | |
74a5af31 DV |
8 | * @fileoverview Based on PlotKit.CanvasRenderer, but modified to meet the |
9 | * needs of dygraphs. | |
10 | * | |
3df0ccf0 | 11 | * In particular, support for: |
0abfbd7e | 12 | * - grid overlays |
3df0ccf0 DV |
13 | * - error bars |
14 | * - dygraphs attribute system | |
6a1aa64f DV |
15 | */ |
16 | ||
6a1aa64f | 17 | /** |
423f5ed3 DV |
18 | * The DygraphCanvasRenderer class does the actual rendering of the chart onto |
19 | * a canvas. It's based on PlotKit.CanvasRenderer. | |
6a1aa64f | 20 | * @param {Object} element The canvas to attach to |
2cf95fff RK |
21 | * @param {Object} elementContext The 2d context of the canvas (injected so it |
22 | * can be mocked for testing.) | |
285a6bda | 23 | * @param {Layout} layout The DygraphLayout object for this graph. |
74a5af31 | 24 | * @constructor |
6a1aa64f | 25 | */ |
c0f54d4f DV |
26 | |
27 | "use strict"; | |
28 | ||
29 | var DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) { | |
9317362d | 30 | this.dygraph_ = dygraph; |
fbe31dc8 | 31 | |
fbe31dc8 | 32 | this.layout = layout; |
b0c3b730 | 33 | this.element = element; |
2cf95fff | 34 | this.elementContext = elementContext; |
fbe31dc8 DV |
35 | this.container = this.element.parentNode; |
36 | ||
fbe31dc8 DV |
37 | this.height = this.element.height; |
38 | this.width = this.element.width; | |
39 | ||
40 | // --- check whether everything is ok before we return | |
41 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
42 | throw "Canvas is not supported."; | |
43 | ||
44 | // internal state | |
45 | this.xlabels = new Array(); | |
46 | this.ylabels = new Array(); | |
ce49c2fa | 47 | this.annotations = new Array(); |
ad1798c2 | 48 | this.chartLabels = {}; |
fbe31dc8 | 49 | |
70be5ed1 | 50 | this.area = layout.getPlotArea(); |
423f5ed3 DV |
51 | this.container.style.position = "relative"; |
52 | this.container.style.width = this.width + "px"; | |
53 | ||
54 | // Set up a clipping area for the canvas (and the interaction canvas). | |
55 | // This ensures that we don't overdraw. | |
920208fb PF |
56 | if (this.dygraph_.isUsingExcanvas_) { |
57 | this._createIEClipArea(); | |
58 | } else { | |
971870e5 DV |
59 | // on Android 3 and 4, setting a clipping area on a canvas prevents it from |
60 | // displaying anything. | |
61 | if (!Dygraph.isAndroid()) { | |
62 | var ctx = this.dygraph_.canvas_ctx_; | |
63 | ctx.beginPath(); | |
64 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
65 | ctx.clip(); | |
920208fb | 66 | |
971870e5 DV |
67 | ctx = this.dygraph_.hidden_ctx_; |
68 | ctx.beginPath(); | |
69 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
70 | ctx.clip(); | |
71 | } | |
920208fb | 72 | } |
423f5ed3 DV |
73 | }; |
74 | ||
75 | DygraphCanvasRenderer.prototype.attr_ = function(x) { | |
76 | return this.dygraph_.attr_(x); | |
77 | }; | |
78 | ||
fbe31dc8 DV |
79 | DygraphCanvasRenderer.prototype.clear = function() { |
80 | if (this.isIE) { | |
81 | // VML takes a while to start up, so we just poll every this.IEDelay | |
82 | try { | |
83 | if (this.clearDelay) { | |
84 | this.clearDelay.cancel(); | |
85 | this.clearDelay = null; | |
86 | } | |
2cf95fff | 87 | var context = this.elementContext; |
fbe31dc8 DV |
88 | } |
89 | catch (e) { | |
76171648 | 90 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
91 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
92 | this.clearDelay.addCallback(bind(this.clear, this)); | |
93 | return; | |
94 | } | |
95 | } | |
96 | ||
2cf95fff | 97 | var context = this.elementContext; |
fbe31dc8 DV |
98 | context.clearRect(0, 0, this.width, this.height); |
99 | ||
2160ed4a | 100 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 | 101 | var el = this.xlabels[i]; |
b304aaec | 102 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a DV |
103 | } |
104 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 | 105 | var el = this.ylabels[i]; |
b304aaec | 106 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a | 107 | } |
ce49c2fa DV |
108 | for (var i = 0; i < this.annotations.length; i++) { |
109 | var el = this.annotations[i]; | |
b304aaec | 110 | if (el.parentNode) el.parentNode.removeChild(el); |
ce49c2fa | 111 | } |
ad1798c2 DV |
112 | for (var k in this.chartLabels) { |
113 | if (!this.chartLabels.hasOwnProperty(k)) continue; | |
114 | var el = this.chartLabels[k]; | |
115 | if (el.parentNode) el.parentNode.removeChild(el); | |
116 | } | |
fbe31dc8 DV |
117 | this.xlabels = new Array(); |
118 | this.ylabels = new Array(); | |
ce49c2fa | 119 | this.annotations = new Array(); |
ad1798c2 | 120 | this.chartLabels = {}; |
fbe31dc8 DV |
121 | }; |
122 | ||
123 | ||
124 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
125 | var canvas = null; | |
126 | try { | |
21d3323f | 127 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 128 | canvas = document.createElement("canvas"); |
fbe31dc8 | 129 | else |
b0c3b730 | 130 | canvas = canvasName; |
fbe31dc8 DV |
131 | var context = canvas.getContext("2d"); |
132 | } | |
133 | catch (e) { | |
134 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
135 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
136 | if ((!ie) || (ie[1] < 6) || (opera)) | |
137 | return false; | |
138 | return true; | |
139 | } | |
140 | return true; | |
6a1aa64f | 141 | }; |
6a1aa64f DV |
142 | |
143 | /** | |
600d841a DV |
144 | * @param { [String] } colors Array of color strings. Should have one entry for |
145 | * each series to be rendered. | |
146 | */ | |
147 | DygraphCanvasRenderer.prototype.setColors = function(colors) { | |
148 | this.colorScheme_ = colors; | |
149 | }; | |
150 | ||
151 | /** | |
6a1aa64f DV |
152 | * Draw an X/Y grid on top of the existing plot |
153 | */ | |
285a6bda | 154 | DygraphCanvasRenderer.prototype.render = function() { |
528ce7e5 DV |
155 | // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to |
156 | // half-integers. This prevents them from drawing in two rows/cols. | |
2cf95fff | 157 | var ctx = this.elementContext; |
528ce7e5 DV |
158 | function halfUp(x){return Math.round(x)+0.5}; |
159 | function halfDown(y){return Math.round(y)-0.5}; | |
e7746234 | 160 | |
423f5ed3 | 161 | if (this.attr_('underlayCallback')) { |
1e41bd2d DV |
162 | // NOTE: we pass the dygraph object to this callback twice to avoid breaking |
163 | // users who expect a deprecated form of this callback. | |
423f5ed3 | 164 | this.attr_('underlayCallback')(ctx, this.area, this.dygraph_, this.dygraph_); |
e7746234 EC |
165 | } |
166 | ||
423f5ed3 | 167 | if (this.attr_('drawYGrid')) { |
6a1aa64f | 168 | var ticks = this.layout.yticks; |
bbba718a | 169 | // TODO(konigsberg): I don't think these calls to save() have a corresponding restore(). |
6a1aa64f | 170 | ctx.save(); |
423f5ed3 | 171 | ctx.strokeStyle = this.attr_('gridLineColor'); |
990d6a35 | 172 | ctx.lineWidth = this.attr_('gridLineWidth'); |
6a1aa64f | 173 | for (var i = 0; i < ticks.length; i++) { |
880a574f DV |
174 | // TODO(danvk): allow secondary axes to draw a grid, too. |
175 | if (ticks[i][0] != 0) continue; | |
528ce7e5 DV |
176 | var x = halfUp(this.area.x); |
177 | var y = halfDown(this.area.y + ticks[i][1] * this.area.h); | |
6a1aa64f DV |
178 | ctx.beginPath(); |
179 | ctx.moveTo(x, y); | |
180 | ctx.lineTo(x + this.area.w, y); | |
181 | ctx.closePath(); | |
182 | ctx.stroke(); | |
183 | } | |
184 | } | |
185 | ||
423f5ed3 | 186 | if (this.attr_('drawXGrid')) { |
6a1aa64f DV |
187 | var ticks = this.layout.xticks; |
188 | ctx.save(); | |
423f5ed3 | 189 | ctx.strokeStyle = this.attr_('gridLineColor'); |
990d6a35 | 190 | ctx.lineWidth = this.attr_('gridLineWidth'); |
6a1aa64f | 191 | for (var i=0; i<ticks.length; i++) { |
528ce7e5 DV |
192 | var x = halfUp(this.area.x + ticks[i][0] * this.area.w); |
193 | var y = halfDown(this.area.y + this.area.h); | |
6a1aa64f | 194 | ctx.beginPath(); |
880a574f | 195 | ctx.moveTo(x, y); |
6a1aa64f DV |
196 | ctx.lineTo(x, this.area.y); |
197 | ctx.closePath(); | |
198 | ctx.stroke(); | |
199 | } | |
200 | } | |
2ce09b19 DV |
201 | |
202 | // Do the ordinary rendering, as before | |
2ce09b19 | 203 | this._renderLineChart(); |
fbe31dc8 | 204 | this._renderAxis(); |
ccd9d7c2 | 205 | this._renderChartLabels(); |
ce49c2fa | 206 | this._renderAnnotations(); |
fbe31dc8 DV |
207 | }; |
208 | ||
920208fb PF |
209 | DygraphCanvasRenderer.prototype._createIEClipArea = function() { |
210 | var className = 'dygraph-clip-div'; | |
211 | var graphDiv = this.dygraph_.graphDiv; | |
212 | ||
213 | // Remove old clip divs. | |
214 | for (var i = graphDiv.childNodes.length-1; i >= 0; i--) { | |
215 | if (graphDiv.childNodes[i].className == className) { | |
216 | graphDiv.removeChild(graphDiv.childNodes[i]); | |
217 | } | |
218 | } | |
219 | ||
220 | // Determine background color to give clip divs. | |
221 | var backgroundColor = document.bgColor; | |
222 | var element = this.dygraph_.graphDiv; | |
223 | while (element != document) { | |
224 | var bgcolor = element.currentStyle.backgroundColor; | |
225 | if (bgcolor && bgcolor != 'transparent') { | |
226 | backgroundColor = bgcolor; | |
227 | break; | |
228 | } | |
229 | element = element.parentNode; | |
230 | } | |
231 | ||
232 | function createClipDiv(area) { | |
233 | if (area.w == 0 || area.h == 0) { | |
234 | return; | |
235 | } | |
236 | var elem = document.createElement('div'); | |
237 | elem.className = className; | |
238 | elem.style.backgroundColor = backgroundColor; | |
239 | elem.style.position = 'absolute'; | |
240 | elem.style.left = area.x + 'px'; | |
241 | elem.style.top = area.y + 'px'; | |
242 | elem.style.width = area.w + 'px'; | |
243 | elem.style.height = area.h + 'px'; | |
244 | graphDiv.appendChild(elem); | |
245 | } | |
246 | ||
247 | var plotArea = this.area; | |
248 | // Left side | |
249 | createClipDiv({x:0, y:0, w:plotArea.x, h:this.height}); | |
250 | // Top | |
251 | createClipDiv({x:plotArea.x, y:0, w:this.width-plotArea.x, h:plotArea.y}); | |
252 | // Right side | |
253 | createClipDiv({x:plotArea.x+plotArea.w, y:0, w:this.width-plotArea.x-plotArea.w, h:this.height}); | |
254 | // Bottom | |
255 | createClipDiv({x:plotArea.x, y:plotArea.y+plotArea.h, w:this.width-plotArea.x, h:this.height-plotArea.h-plotArea.y}); | |
256 | } | |
fbe31dc8 DV |
257 | |
258 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
423f5ed3 | 259 | if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return; |
fbe31dc8 | 260 | |
528ce7e5 DV |
261 | // Round pixels to half-integer boundaries for crisper drawing. |
262 | function halfUp(x){return Math.round(x)+0.5}; | |
263 | function halfDown(y){return Math.round(y)-0.5}; | |
264 | ||
2cf95fff | 265 | var context = this.elementContext; |
fbe31dc8 | 266 | |
34fedff8 | 267 | var labelStyle = { |
423f5ed3 DV |
268 | position: "absolute", |
269 | fontSize: this.attr_('axisLabelFontSize') + "px", | |
270 | zIndex: 10, | |
271 | color: this.attr_('axisLabelColor'), | |
272 | width: this.attr_('axisLabelWidth') + "px", | |
74a5af31 | 273 | // height: this.attr_('axisLabelFontSize') + 2 + "px", |
ccd9d7c2 | 274 | lineHeight: "normal", // Something other than "normal" line-height screws up label positioning. |
423f5ed3 | 275 | overflow: "hidden" |
34fedff8 | 276 | }; |
48e614ac | 277 | var makeDiv = function(txt, axis, prec_axis) { |
34fedff8 DV |
278 | var div = document.createElement("div"); |
279 | for (var name in labelStyle) { | |
85b99f0b DV |
280 | if (labelStyle.hasOwnProperty(name)) { |
281 | div.style[name] = labelStyle[name]; | |
282 | } | |
fbe31dc8 | 283 | } |
ba451526 | 284 | var inner_div = document.createElement("div"); |
48e614ac DV |
285 | inner_div.className = 'dygraph-axis-label' + |
286 | ' dygraph-axis-label-' + axis + | |
287 | (prec_axis ? ' dygraph-axis-label-' + prec_axis : ''); | |
ba451526 DV |
288 | inner_div.appendChild(document.createTextNode(txt)); |
289 | div.appendChild(inner_div); | |
34fedff8 | 290 | return div; |
fbe31dc8 DV |
291 | }; |
292 | ||
293 | // axis lines | |
294 | context.save(); | |
423f5ed3 DV |
295 | context.strokeStyle = this.attr_('axisLineColor'); |
296 | context.lineWidth = this.attr_('axisLineWidth'); | |
fbe31dc8 | 297 | |
423f5ed3 | 298 | if (this.attr_('drawYAxis')) { |
8b7a0cc3 | 299 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
48e614ac | 300 | var num_axes = this.dygraph_.numAxes(); |
2160ed4a DV |
301 | for (var i = 0; i < this.layout.yticks.length; i++) { |
302 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
303 | if (typeof(tick) == "function") return; |
304 | var x = this.area.x; | |
880a574f | 305 | var sgn = 1; |
48e614ac | 306 | var prec_axis = 'y1'; |
880a574f DV |
307 | if (tick[0] == 1) { // right-side y-axis |
308 | x = this.area.x + this.area.w; | |
309 | sgn = -1; | |
48e614ac | 310 | prec_axis = 'y2'; |
9012dd21 DV |
311 | } |
312 | var y = this.area.y + tick[1] * this.area.h; | |
920208fb PF |
313 | |
314 | /* Tick marks are currently clipped, so don't bother drawing them. | |
fbe31dc8 | 315 | context.beginPath(); |
528ce7e5 | 316 | context.moveTo(halfUp(x), halfDown(y)); |
0e23cfc6 | 317 | context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y)); |
fbe31dc8 DV |
318 | context.closePath(); |
319 | context.stroke(); | |
920208fb | 320 | */ |
fbe31dc8 | 321 | |
48e614ac | 322 | var label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null); |
423f5ed3 | 323 | var top = (y - this.attr_('axisLabelFontSize') / 2); |
fbe31dc8 DV |
324 | if (top < 0) top = 0; |
325 | ||
423f5ed3 | 326 | if (top + this.attr_('axisLabelFontSize') + 3 > this.height) { |
fbe31dc8 DV |
327 | label.style.bottom = "0px"; |
328 | } else { | |
329 | label.style.top = top + "px"; | |
330 | } | |
9012dd21 | 331 | if (tick[0] == 0) { |
423f5ed3 | 332 | label.style.left = (this.area.x - this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px"; |
9012dd21 DV |
333 | label.style.textAlign = "right"; |
334 | } else if (tick[0] == 1) { | |
335 | label.style.left = (this.area.x + this.area.w + | |
423f5ed3 | 336 | this.attr_('axisTickSize')) + "px"; |
9012dd21 DV |
337 | label.style.textAlign = "left"; |
338 | } | |
423f5ed3 | 339 | label.style.width = this.attr_('yAxisLabelWidth') + "px"; |
b0c3b730 | 340 | this.container.appendChild(label); |
fbe31dc8 | 341 | this.ylabels.push(label); |
2160ed4a | 342 | } |
fbe31dc8 DV |
343 | |
344 | // The lowest tick on the y-axis often overlaps with the leftmost | |
345 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
346 | // compensate if necessary. | |
347 | var bottomTick = this.ylabels[0]; | |
423f5ed3 | 348 | var fontSize = this.attr_('axisLabelFontSize'); |
fbe31dc8 DV |
349 | var bottom = parseInt(bottomTick.style.top) + fontSize; |
350 | if (bottom > this.height - fontSize) { | |
351 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
352 | fontSize / 2) + "px"; | |
353 | } | |
354 | } | |
355 | ||
528ce7e5 | 356 | // draw a vertical line on the left to separate the chart from the labels. |
fbe31dc8 | 357 | context.beginPath(); |
528ce7e5 DV |
358 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y)); |
359 | context.lineTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
360 | context.closePath(); |
361 | context.stroke(); | |
c1dbeb10 | 362 | |
528ce7e5 | 363 | // if there's a secondary y-axis, draw a vertical line for that, too. |
c1dbeb10 DV |
364 | if (this.dygraph_.numAxes() == 2) { |
365 | context.beginPath(); | |
528ce7e5 DV |
366 | context.moveTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y)); |
367 | context.lineTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
c1dbeb10 DV |
368 | context.closePath(); |
369 | context.stroke(); | |
370 | } | |
fbe31dc8 DV |
371 | } |
372 | ||
423f5ed3 | 373 | if (this.attr_('drawXAxis')) { |
fbe31dc8 | 374 | if (this.layout.xticks) { |
2160ed4a DV |
375 | for (var i = 0; i < this.layout.xticks.length; i++) { |
376 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
377 | if (typeof(dataset) == "function") return; |
378 | ||
379 | var x = this.area.x + tick[0] * this.area.w; | |
380 | var y = this.area.y + this.area.h; | |
920208fb PF |
381 | |
382 | /* Tick marks are currently clipped, so don't bother drawing them. | |
fbe31dc8 | 383 | context.beginPath(); |
528ce7e5 | 384 | context.moveTo(halfUp(x), halfDown(y)); |
423f5ed3 | 385 | context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize'))); |
fbe31dc8 DV |
386 | context.closePath(); |
387 | context.stroke(); | |
920208fb | 388 | */ |
fbe31dc8 | 389 | |
ba451526 | 390 | var label = makeDiv(tick[1], 'x'); |
fbe31dc8 | 391 | label.style.textAlign = "center"; |
423f5ed3 | 392 | label.style.top = (y + this.attr_('axisTickSize')) + 'px'; |
fbe31dc8 | 393 | |
423f5ed3 DV |
394 | var left = (x - this.attr_('axisLabelWidth')/2); |
395 | if (left + this.attr_('axisLabelWidth') > this.width) { | |
396 | left = this.width - this.attr_('xAxisLabelWidth'); | |
fbe31dc8 DV |
397 | label.style.textAlign = "right"; |
398 | } | |
399 | if (left < 0) { | |
400 | left = 0; | |
401 | label.style.textAlign = "left"; | |
402 | } | |
403 | ||
404 | label.style.left = left + "px"; | |
423f5ed3 | 405 | label.style.width = this.attr_('xAxisLabelWidth') + "px"; |
b0c3b730 | 406 | this.container.appendChild(label); |
fbe31dc8 | 407 | this.xlabels.push(label); |
2160ed4a | 408 | } |
fbe31dc8 DV |
409 | } |
410 | ||
411 | context.beginPath(); | |
528ce7e5 DV |
412 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); |
413 | context.lineTo(halfUp(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
414 | context.closePath(); |
415 | context.stroke(); | |
416 | } | |
417 | ||
418 | context.restore(); | |
6a1aa64f DV |
419 | }; |
420 | ||
fbe31dc8 | 421 | |
ad1798c2 DV |
422 | DygraphCanvasRenderer.prototype._renderChartLabels = function() { |
423 | // Generate divs for the chart title, xlabel and ylabel. | |
424 | // Space for these divs has already been taken away from the charting area in | |
425 | // the DygraphCanvasRenderer constructor. | |
426 | if (this.attr_('title')) { | |
427 | var div = document.createElement("div"); | |
428 | div.style.position = 'absolute'; | |
429 | div.style.top = '0px'; | |
430 | div.style.left = this.area.x + 'px'; | |
431 | div.style.width = this.area.w + 'px'; | |
432 | div.style.height = this.attr_('titleHeight') + 'px'; | |
433 | div.style.textAlign = 'center'; | |
b4202b3d | 434 | div.style.fontSize = (this.attr_('titleHeight') - 8) + 'px'; |
ad1798c2 | 435 | div.style.fontWeight = 'bold'; |
ca49434a DV |
436 | var class_div = document.createElement("div"); |
437 | class_div.className = 'dygraph-label dygraph-title'; | |
438 | class_div.innerHTML = this.attr_('title'); | |
439 | div.appendChild(class_div); | |
ad1798c2 DV |
440 | this.container.appendChild(div); |
441 | this.chartLabels.title = div; | |
442 | } | |
443 | ||
444 | if (this.attr_('xlabel')) { | |
445 | var div = document.createElement("div"); | |
446 | div.style.position = 'absolute'; | |
447 | div.style.bottom = 0; // TODO(danvk): this is lazy. Calculate style.top. | |
448 | div.style.left = this.area.x + 'px'; | |
449 | div.style.width = this.area.w + 'px'; | |
450 | div.style.height = this.attr_('xLabelHeight') + 'px'; | |
451 | div.style.textAlign = 'center'; | |
86cce9e8 | 452 | div.style.fontSize = (this.attr_('xLabelHeight') - 2) + 'px'; |
ca49434a DV |
453 | |
454 | var class_div = document.createElement("div"); | |
455 | class_div.className = 'dygraph-label dygraph-xlabel'; | |
456 | class_div.innerHTML = this.attr_('xlabel'); | |
457 | div.appendChild(class_div); | |
ad1798c2 DV |
458 | this.container.appendChild(div); |
459 | this.chartLabels.xlabel = div; | |
460 | } | |
461 | ||
462 | if (this.attr_('ylabel')) { | |
463 | var box = { | |
464 | left: 0, | |
465 | top: this.area.y, | |
466 | width: this.attr_('yLabelWidth'), | |
467 | height: this.area.h | |
468 | }; | |
ca49434a | 469 | // TODO(danvk): is this outer div actually necessary? |
ad1798c2 DV |
470 | var div = document.createElement("div"); |
471 | div.style.position = 'absolute'; | |
472 | div.style.left = box.left; | |
473 | div.style.top = box.top + 'px'; | |
474 | div.style.width = box.width + 'px'; | |
475 | div.style.height = box.height + 'px'; | |
86cce9e8 | 476 | div.style.fontSize = (this.attr_('yLabelWidth') - 2) + 'px'; |
ad1798c2 DV |
477 | |
478 | var inner_div = document.createElement("div"); | |
479 | inner_div.style.position = 'absolute'; | |
ad1798c2 DV |
480 | inner_div.style.width = box.height + 'px'; |
481 | inner_div.style.height = box.width + 'px'; | |
482 | inner_div.style.top = (box.height / 2 - box.width / 2) + 'px'; | |
483 | inner_div.style.left = (box.width / 2 - box.height / 2) + 'px'; | |
484 | inner_div.style.textAlign = 'center'; | |
b56b6993 DV |
485 | |
486 | // CSS rotation is an HTML5 feature which is not standardized. Hence every | |
487 | // browser has its own name for the CSS style. | |
ad1798c2 DV |
488 | inner_div.style.transform = 'rotate(-90deg)'; // HTML5 |
489 | inner_div.style.WebkitTransform = 'rotate(-90deg)'; // Safari/Chrome | |
490 | inner_div.style.MozTransform = 'rotate(-90deg)'; // Firefox | |
491 | inner_div.style.OTransform = 'rotate(-90deg)'; // Opera | |
52c47e80 | 492 | inner_div.style.msTransform = 'rotate(-90deg)'; // IE9 |
b56b6993 DV |
493 | |
494 | if (typeof(document.documentMode) !== 'undefined' && | |
495 | document.documentMode < 9) { | |
496 | // We're dealing w/ an old version of IE, so we have to rotate the text | |
497 | // using a BasicImage transform. This uses a different origin of rotation | |
498 | // than HTML5 rotation (top left of div vs. its center). | |
499 | inner_div.style.filter = | |
500 | 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'; | |
501 | inner_div.style.left = '0px'; | |
502 | inner_div.style.top = '0px'; | |
503 | } | |
ad1798c2 | 504 | |
ca49434a DV |
505 | var class_div = document.createElement("div"); |
506 | class_div.className = 'dygraph-label dygraph-ylabel'; | |
507 | class_div.innerHTML = this.attr_('ylabel'); | |
508 | ||
509 | inner_div.appendChild(class_div); | |
ad1798c2 DV |
510 | div.appendChild(inner_div); |
511 | this.container.appendChild(div); | |
512 | this.chartLabels.ylabel = div; | |
513 | } | |
514 | }; | |
515 | ||
516 | ||
ce49c2fa DV |
517 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
518 | var annotationStyle = { | |
519 | "position": "absolute", | |
423f5ed3 | 520 | "fontSize": this.attr_('axisLabelFontSize') + "px", |
ce49c2fa | 521 | "zIndex": 10, |
3bf2fa91 | 522 | "overflow": "hidden" |
ce49c2fa DV |
523 | }; |
524 | ||
ab5e5c75 DV |
525 | var bindEvt = function(eventName, classEventName, p, self) { |
526 | return function(e) { | |
527 | var a = p.annotation; | |
528 | if (a.hasOwnProperty(eventName)) { | |
529 | a[eventName](a, p, self.dygraph_, e); | |
530 | } else if (self.dygraph_.attr_(classEventName)) { | |
531 | self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e ); | |
532 | } | |
533 | }; | |
534 | } | |
535 | ||
ce49c2fa DV |
536 | // Get a list of point with annotations. |
537 | var points = this.layout.annotated_points; | |
538 | for (var i = 0; i < points.length; i++) { | |
539 | var p = points[i]; | |
e6d53148 DV |
540 | if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { |
541 | continue; | |
542 | } | |
543 | ||
ce5e8d36 DV |
544 | var a = p.annotation; |
545 | var tick_height = 6; | |
546 | if (a.hasOwnProperty("tickHeight")) { | |
547 | tick_height = a.tickHeight; | |
9a40897e DV |
548 | } |
549 | ||
ce49c2fa DV |
550 | var div = document.createElement("div"); |
551 | for (var name in annotationStyle) { | |
552 | if (annotationStyle.hasOwnProperty(name)) { | |
553 | div.style[name] = annotationStyle[name]; | |
554 | } | |
555 | } | |
ce5e8d36 DV |
556 | if (!a.hasOwnProperty('icon')) { |
557 | div.className = "dygraphDefaultAnnotation"; | |
558 | } | |
559 | if (a.hasOwnProperty('cssClass')) { | |
560 | div.className += " " + a.cssClass; | |
561 | } | |
562 | ||
a5ad69cc DV |
563 | var width = a.hasOwnProperty('width') ? a.width : 16; |
564 | var height = a.hasOwnProperty('height') ? a.height : 16; | |
ce5e8d36 DV |
565 | if (a.hasOwnProperty('icon')) { |
566 | var img = document.createElement("img"); | |
567 | img.src = a.icon; | |
33030f33 DV |
568 | img.width = width; |
569 | img.height = height; | |
ce5e8d36 DV |
570 | div.appendChild(img); |
571 | } else if (p.annotation.hasOwnProperty('shortText')) { | |
572 | div.appendChild(document.createTextNode(p.annotation.shortText)); | |
5c528fa2 | 573 | } |
ce5e8d36 | 574 | div.style.left = (p.canvasx - width / 2) + "px"; |
d14b9eed DV |
575 | if (a.attachAtBottom) { |
576 | div.style.top = (this.area.h - height - tick_height) + "px"; | |
577 | } else { | |
578 | div.style.top = (p.canvasy - height - tick_height) + "px"; | |
579 | } | |
ce5e8d36 DV |
580 | div.style.width = width + "px"; |
581 | div.style.height = height + "px"; | |
ce49c2fa DV |
582 | div.title = p.annotation.text; |
583 | div.style.color = this.colors[p.name]; | |
584 | div.style.borderColor = this.colors[p.name]; | |
e6d53148 | 585 | a.div = div; |
ab5e5c75 | 586 | |
9a40897e DV |
587 | Dygraph.addEvent(div, 'click', |
588 | bindEvt('clickHandler', 'annotationClickHandler', p, this)); | |
589 | Dygraph.addEvent(div, 'mouseover', | |
590 | bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); | |
591 | Dygraph.addEvent(div, 'mouseout', | |
592 | bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); | |
593 | Dygraph.addEvent(div, 'dblclick', | |
594 | bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); | |
ab5e5c75 | 595 | |
ce49c2fa DV |
596 | this.container.appendChild(div); |
597 | this.annotations.push(div); | |
9a40897e | 598 | |
2cf95fff | 599 | var ctx = this.elementContext; |
9a40897e DV |
600 | ctx.strokeStyle = this.colors[p.name]; |
601 | ctx.beginPath(); | |
d14b9eed DV |
602 | if (!a.attachAtBottom) { |
603 | ctx.moveTo(p.canvasx, p.canvasy); | |
604 | ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); | |
605 | } else { | |
606 | ctx.moveTo(p.canvasx, this.area.h); | |
607 | ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); | |
608 | } | |
9a40897e DV |
609 | ctx.closePath(); |
610 | ctx.stroke(); | |
ce49c2fa DV |
611 | } |
612 | }; | |
613 | ||
614 | ||
6a1aa64f DV |
615 | /** |
616 | * Overrides the CanvasRenderer method to draw error bars | |
617 | */ | |
285a6bda | 618 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
f9414b11 DV |
619 | var isNullOrNaN = function(x) { |
620 | return (x === null || isNaN(x)); | |
621 | }; | |
ccd9d7c2 | 622 | |
44c6bc29 | 623 | // TODO(danvk): use this.attr_ for many of these. |
2cf95fff | 624 | var context = this.elementContext; |
423f5ed3 | 625 | var fillAlpha = this.attr_('fillAlpha'); |
e4182459 | 626 | var errorBars = this.attr_("errorBars") || this.attr_("customBars"); |
44c6bc29 | 627 | var fillGraph = this.attr_("fillGraph"); |
b2c9222a DV |
628 | var stackedGraph = this.attr_("stackedGraph"); |
629 | var stepPlot = this.attr_("stepPlot"); | |
c3e1495b AR |
630 | var points = this.layout.points; |
631 | var pointsLength = points.length; | |
21d3323f DV |
632 | |
633 | var setNames = []; | |
ca43052c | 634 | for (var name in this.layout.datasets) { |
85b99f0b DV |
635 | if (this.layout.datasets.hasOwnProperty(name)) { |
636 | setNames.push(name); | |
637 | } | |
ca43052c | 638 | } |
21d3323f | 639 | var setCount = setNames.length; |
6a1aa64f | 640 | |
0e23cfc6 | 641 | // TODO(danvk): Move this mapping into Dygraph and get it out of here. |
f032c51d AV |
642 | this.colors = {} |
643 | for (var i = 0; i < setCount; i++) { | |
600d841a | 644 | this.colors[setNames[i]] = this.colorScheme_[i % this.colorScheme_.length]; |
f032c51d AV |
645 | } |
646 | ||
ff00d3e2 DV |
647 | // Update Points |
648 | // TODO(danvk): here | |
c3e1495b AR |
649 | for (var i = pointsLength; i--;) { |
650 | var point = points[i]; | |
6a1aa64f DV |
651 | point.canvasx = this.area.w * point.x + this.area.x; |
652 | point.canvasy = this.area.h * point.y + this.area.y; | |
653 | } | |
6a1aa64f DV |
654 | |
655 | // create paths | |
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]; | |
b2c9222a | 664 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
f032c51d | 665 | var color = this.colors[setName]; |
6a1aa64f DV |
666 | |
667 | // setup graphics context | |
80aaae18 | 668 | ctx.save(); |
56623f3b | 669 | var prevX = NaN; |
afdc483f | 670 | var prevY = NaN; |
6a1aa64f | 671 | var prevYs = [-1, -1]; |
ea4942ed | 672 | var yscale = axis.yscale; |
f474c2a3 DV |
673 | // should be same color as the lines but only 15% opaque. |
674 | var rgb = new RGBColor(color); | |
43af96e7 NK |
675 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
676 | fillAlpha + ')'; | |
f474c2a3 | 677 | ctx.fillStyle = err_color; |
05c9d0c4 | 678 | ctx.beginPath(); |
c3e1495b AR |
679 | for (var j = 0; j < pointsLength; j++) { |
680 | var point = points[j]; | |
6a1aa64f | 681 | if (point.name == setName) { |
e9fe4a2f | 682 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 683 | prevX = NaN; |
ae85914a | 684 | continue; |
5011e7a1 | 685 | } |
ce49c2fa | 686 | |
3637724f | 687 | // TODO(danvk): here |
afdc483f | 688 | if (stepPlot) { |
7f028980 | 689 | var newYs = [ point.y_bottom, point.y_top ]; |
afdc483f NN |
690 | prevY = point.y; |
691 | } else { | |
7f028980 | 692 | var newYs = [ point.y_bottom, point.y_top ]; |
afdc483f | 693 | } |
6a1aa64f DV |
694 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
695 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 696 | if (!isNaN(prevX)) { |
afdc483f | 697 | if (stepPlot) { |
47600757 | 698 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 699 | } else { |
47600757 | 700 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 701 | } |
5954ef32 DV |
702 | ctx.lineTo(point.canvasx, newYs[0]); |
703 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 704 | if (stepPlot) { |
47600757 | 705 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 706 | } else { |
47600757 | 707 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 708 | } |
5954ef32 DV |
709 | ctx.closePath(); |
710 | } | |
354e15ab | 711 | prevYs = newYs; |
5954ef32 DV |
712 | prevX = point.canvasx; |
713 | } | |
714 | } | |
715 | ctx.fill(); | |
716 | } | |
717 | } else if (fillGraph) { | |
354e15ab DE |
718 | var baseline = [] // for stacked graphs: baseline for filling |
719 | ||
720 | // process sets in reverse order (needed for stacked graphs) | |
721 | for (var i = setCount - 1; i >= 0; i--) { | |
5954ef32 | 722 | var setName = setNames[i]; |
f032c51d | 723 | var color = this.colors[setName]; |
b2c9222a | 724 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
ea4942ed DV |
725 | var axisY = 1.0 + axis.minyval * axis.yscale; |
726 | if (axisY < 0.0) axisY = 0.0; | |
727 | else if (axisY > 1.0) axisY = 1.0; | |
728 | axisY = this.area.h * axisY + this.area.y; | |
5954ef32 DV |
729 | |
730 | // setup graphics context | |
731 | ctx.save(); | |
56623f3b | 732 | var prevX = NaN; |
5954ef32 | 733 | var prevYs = [-1, -1]; |
ea4942ed | 734 | var yscale = axis.yscale; |
5954ef32 DV |
735 | // should be same color as the lines but only 15% opaque. |
736 | var rgb = new RGBColor(color); | |
43af96e7 NK |
737 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
738 | fillAlpha + ')'; | |
5954ef32 DV |
739 | ctx.fillStyle = err_color; |
740 | ctx.beginPath(); | |
c3e1495b AR |
741 | for (var j = 0; j < pointsLength; j++) { |
742 | var point = points[j]; | |
5954ef32 | 743 | if (point.name == setName) { |
e9fe4a2f | 744 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 745 | prevX = NaN; |
5954ef32 DV |
746 | continue; |
747 | } | |
354e15ab DE |
748 | var newYs; |
749 | if (stackedGraph) { | |
47927039 | 750 | var lastY = baseline[point.canvasx]; |
354e15ab DE |
751 | if (lastY === undefined) lastY = axisY; |
752 | baseline[point.canvasx] = point.canvasy; | |
753 | newYs = [ point.canvasy, lastY ]; | |
754 | } else { | |
755 | newYs = [ point.canvasy, axisY ]; | |
756 | } | |
56623f3b | 757 | if (!isNaN(prevX)) { |
05c9d0c4 | 758 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 759 | if (stepPlot) { |
47600757 | 760 | ctx.lineTo(point.canvasx, prevYs[0]); |
afdc483f | 761 | } else { |
47600757 | 762 | ctx.lineTo(point.canvasx, newYs[0]); |
afdc483f | 763 | } |
05c9d0c4 DV |
764 | ctx.lineTo(point.canvasx, newYs[1]); |
765 | ctx.lineTo(prevX, prevYs[1]); | |
766 | ctx.closePath(); | |
6a1aa64f | 767 | } |
354e15ab | 768 | prevYs = newYs; |
6a1aa64f DV |
769 | prevX = point.canvasx; |
770 | } | |
05c9d0c4 | 771 | } |
6a1aa64f DV |
772 | ctx.fill(); |
773 | } | |
80aaae18 DV |
774 | } |
775 | ||
f9414b11 | 776 | // Drawing the lines. |
c3e1495b AR |
777 | var firstIndexInSet = 0; |
778 | var afterLastIndexInSet = 0; | |
779 | var setLength = 0; | |
780 | for (var i = 0; i < setCount; i += 1) { | |
781 | setLength = this.layout.setPointsLengths[i]; | |
782 | afterLastIndexInSet += setLength; | |
80aaae18 | 783 | var setName = setNames[i]; |
f032c51d | 784 | var color = this.colors[setName]; |
227b93cc | 785 | var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); |
80aaae18 DV |
786 | |
787 | // setup graphics context | |
788 | context.save(); | |
227b93cc | 789 | var pointSize = this.dygraph_.attr_("pointSize", setName); |
80aaae18 | 790 | var prevX = null, prevY = null; |
227b93cc | 791 | var drawPoints = this.dygraph_.attr_("drawPoints", setName); |
c3e1495b | 792 | for (var j = firstIndexInSet; j < afterLastIndexInSet; j++) { |
80aaae18 | 793 | var point = points[j]; |
c3e1495b AR |
794 | if (isNullOrNaN(point.canvasy)) { |
795 | if (stepPlot && prevX != null) { | |
796 | // Draw a horizontal line to the start of the missing data | |
797 | ctx.beginPath(); | |
798 | ctx.strokeStyle = color; | |
799 | ctx.lineWidth = this.attr_('strokeWidth'); | |
800 | ctx.moveTo(prevX, prevY); | |
801 | ctx.lineTo(point.canvasx, prevY); | |
802 | ctx.stroke(); | |
803 | } | |
804 | // this will make us move to the next point, not draw a line to it. | |
805 | prevX = prevY = null; | |
806 | } else { | |
807 | // A point is "isolated" if it is non-null but both the previous | |
808 | // and next points are null. | |
809 | var isIsolated = (!prevX && (j == points.length - 1 || | |
810 | isNullOrNaN(points[j+1].canvasy))); | |
5f453f17 | 811 | if (prevX === null) { |
c3e1495b AR |
812 | prevX = point.canvasx; |
813 | prevY = point.canvasy; | |
814 | } else { | |
f9414b11 DV |
815 | // Skip over points that will be drawn in the same pixel. |
816 | if (Math.round(prevX) == Math.round(point.canvasx) && | |
817 | Math.round(prevY) == Math.round(point.canvasy)) { | |
818 | continue; | |
819 | } | |
c3e1495b AR |
820 | // TODO(antrob): skip over points that lie on a line that is already |
821 | // going to be drawn. There is no need to have more than 2 | |
822 | // consecutive points that are collinear. | |
823 | if (strokeWidth) { | |
0599d13b NN |
824 | ctx.beginPath(); |
825 | ctx.strokeStyle = color; | |
c3e1495b | 826 | ctx.lineWidth = strokeWidth; |
0599d13b | 827 | ctx.moveTo(prevX, prevY); |
c3e1495b AR |
828 | if (stepPlot) { |
829 | ctx.lineTo(point.canvasx, prevY); | |
830 | } | |
80aaae18 DV |
831 | prevX = point.canvasx; |
832 | prevY = point.canvasy; | |
c3e1495b AR |
833 | ctx.lineTo(prevX, prevY); |
834 | ctx.stroke(); | |
80aaae18 DV |
835 | } |
836 | } | |
5f453f17 DV |
837 | |
838 | if (drawPoints || isIsolated) { | |
839 | ctx.beginPath(); | |
840 | ctx.fillStyle = color; | |
841 | ctx.arc(point.canvasx, point.canvasy, pointSize, | |
842 | 0, 2 * Math.PI, false); | |
843 | ctx.fill(); | |
c3e1495b | 844 | } |
80aaae18 DV |
845 | } |
846 | } | |
c3e1495b | 847 | firstIndexInSet = afterLastIndexInSet; |
80aaae18 | 848 | } |
6a1aa64f | 849 | |
6a1aa64f DV |
850 | context.restore(); |
851 | }; |