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