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