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