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