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 | |
70be5ed1 | 65 | this.area = layout.getPlotArea(); |
423f5ed3 DV |
66 | this.container.style.position = "relative"; |
67 | this.container.style.width = this.width + "px"; | |
68 | ||
69 | // Set up a clipping area for the canvas (and the interaction canvas). | |
70 | // This ensures that we don't overdraw. | |
920208fb PF |
71 | if (this.dygraph_.isUsingExcanvas_) { |
72 | this._createIEClipArea(); | |
73 | } else { | |
971870e5 DV |
74 | // on Android 3 and 4, setting a clipping area on a canvas prevents it from |
75 | // displaying anything. | |
76 | if (!Dygraph.isAndroid()) { | |
77 | var ctx = this.dygraph_.canvas_ctx_; | |
78 | ctx.beginPath(); | |
79 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
80 | ctx.clip(); | |
920208fb | 81 | |
971870e5 DV |
82 | ctx = this.dygraph_.hidden_ctx_; |
83 | ctx.beginPath(); | |
84 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
85 | ctx.clip(); | |
86 | } | |
920208fb | 87 | } |
423f5ed3 DV |
88 | }; |
89 | ||
90 | DygraphCanvasRenderer.prototype.attr_ = function(x) { | |
91 | return this.dygraph_.attr_(x); | |
92 | }; | |
93 | ||
8cfe592f DV |
94 | /** |
95 | * Clears out all chart content and DOM elements. | |
96 | * This is called immediately before render() on every frame, including | |
97 | * during zooms and pans. | |
98 | * @private | |
99 | */ | |
fbe31dc8 | 100 | DygraphCanvasRenderer.prototype.clear = function() { |
758a629f | 101 | var context; |
fbe31dc8 DV |
102 | if (this.isIE) { |
103 | // VML takes a while to start up, so we just poll every this.IEDelay | |
104 | try { | |
105 | if (this.clearDelay) { | |
106 | this.clearDelay.cancel(); | |
107 | this.clearDelay = null; | |
108 | } | |
758a629f | 109 | context = this.elementContext; |
fbe31dc8 DV |
110 | } |
111 | catch (e) { | |
76171648 | 112 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
758a629f DV |
113 | // this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
114 | // this.clearDelay.addCallback(bind(this.clear, this)); | |
fbe31dc8 DV |
115 | return; |
116 | } | |
117 | } | |
118 | ||
758a629f | 119 | context = this.elementContext; |
fbe31dc8 | 120 | context.clearRect(0, 0, this.width, this.height); |
fbe31dc8 DV |
121 | }; |
122 | ||
8cfe592f DV |
123 | /** |
124 | * Checks whether the browser supports the <canvas> tag. | |
125 | * @private | |
126 | */ | |
fbe31dc8 DV |
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 | /** | |
8cfe592f DV |
148 | * This method is responsible for drawing everything on the chart, including |
149 | * lines, error bars, fills and axes. | |
150 | * It is called immediately after clear() on every frame, including during pans | |
151 | * and zooms. | |
152 | * @private | |
6a1aa64f | 153 | */ |
285a6bda | 154 | DygraphCanvasRenderer.prototype.render = function() { |
2ce09b19 | 155 | this._renderLineChart(); |
fbe31dc8 DV |
156 | }; |
157 | ||
920208fb PF |
158 | DygraphCanvasRenderer.prototype._createIEClipArea = function() { |
159 | var className = 'dygraph-clip-div'; | |
160 | var graphDiv = this.dygraph_.graphDiv; | |
161 | ||
162 | // Remove old clip divs. | |
163 | for (var i = graphDiv.childNodes.length-1; i >= 0; i--) { | |
164 | if (graphDiv.childNodes[i].className == className) { | |
165 | graphDiv.removeChild(graphDiv.childNodes[i]); | |
166 | } | |
167 | } | |
168 | ||
169 | // Determine background color to give clip divs. | |
170 | var backgroundColor = document.bgColor; | |
171 | var element = this.dygraph_.graphDiv; | |
172 | while (element != document) { | |
173 | var bgcolor = element.currentStyle.backgroundColor; | |
174 | if (bgcolor && bgcolor != 'transparent') { | |
175 | backgroundColor = bgcolor; | |
176 | break; | |
177 | } | |
178 | element = element.parentNode; | |
179 | } | |
180 | ||
181 | function createClipDiv(area) { | |
758a629f | 182 | if (area.w === 0 || area.h === 0) { |
920208fb PF |
183 | return; |
184 | } | |
185 | var elem = document.createElement('div'); | |
186 | elem.className = className; | |
187 | elem.style.backgroundColor = backgroundColor; | |
188 | elem.style.position = 'absolute'; | |
189 | elem.style.left = area.x + 'px'; | |
190 | elem.style.top = area.y + 'px'; | |
191 | elem.style.width = area.w + 'px'; | |
192 | elem.style.height = area.h + 'px'; | |
193 | graphDiv.appendChild(elem); | |
194 | } | |
195 | ||
196 | var plotArea = this.area; | |
197 | // Left side | |
758a629f DV |
198 | createClipDiv({ |
199 | x:0, y:0, | |
200 | w:plotArea.x, | |
201 | h:this.height | |
202 | }); | |
203 | ||
920208fb | 204 | // Top |
758a629f DV |
205 | createClipDiv({ |
206 | x: plotArea.x, y: 0, | |
207 | w: this.width - plotArea.x, | |
208 | h: plotArea.y | |
209 | }); | |
210 | ||
920208fb | 211 | // Right side |
758a629f DV |
212 | createClipDiv({ |
213 | x: plotArea.x + plotArea.w, y: 0, | |
214 | w: this.width-plotArea.x - plotArea.w, | |
215 | h: this.height | |
216 | }); | |
217 | ||
920208fb | 218 | // Bottom |
758a629f DV |
219 | createClipDiv({ |
220 | x: plotArea.x, | |
221 | y: plotArea.y + plotArea.h, | |
222 | w: this.width - plotArea.x, | |
223 | h: this.height - plotArea.h - plotArea.y | |
224 | }); | |
225 | }; | |
fbe31dc8 | 226 | |
fbe31dc8 | 227 | |
ccb0001c | 228 | /** |
8722284b RK |
229 | * Returns a predicate to be used with an iterator, which will |
230 | * iterate over points appropriately, depending on whether | |
231 | * connectSeparatedPoints is true. When it's false, the predicate will | |
232 | * skip over points with missing yVals. | |
ccb0001c | 233 | */ |
8722284b RK |
234 | DygraphCanvasRenderer._getIteratorPredicate = function(connectSeparatedPoints) { |
235 | return connectSeparatedPoints ? DygraphCanvasRenderer._predicateThatSkipsEmptyPoints : null; | |
236 | } | |
237 | ||
238 | DygraphCanvasRenderer._predicateThatSkipsEmptyPoints = | |
239 | function(array, idx) { return array[idx].yval !== null; } | |
04c104d7 | 240 | |
857a6931 | 241 | DygraphCanvasRenderer.prototype._drawStyledLine = function( |
5469113b KW |
242 | ctx, i, setName, color, strokeWidth, strokePattern, drawPoints, |
243 | drawPointCallback, pointSize) { | |
99a77a04 | 244 | // TODO(konigsberg): Compute attributes outside this method call. |
857a6931 KW |
245 | var stepPlot = this.attr_("stepPlot"); |
246 | var firstIndexInSet = this.layout.setPointsOffsets[i]; | |
247 | var setLength = this.layout.setPointsLengths[i]; | |
857a6931 | 248 | var points = this.layout.points; |
857a6931 KW |
249 | if (!Dygraph.isArrayLike(strokePattern)) { |
250 | strokePattern = null; | |
251 | } | |
a5a50727 | 252 | var drawGapPoints = this.dygraph_.attr_('drawGapEdgePoints', setName); |
857a6931 | 253 | |
b843b52c | 254 | ctx.save(); |
7d1afbb9 | 255 | |
7d1afbb9 | 256 | var iter = Dygraph.createIterator(points, firstIndexInSet, setLength, |
8722284b | 257 | DygraphCanvasRenderer._getIteratorPredicate(this.attr_("connectSeparatedPoints"))); |
7d1afbb9 | 258 | |
31f8e58b RK |
259 | var pointsOnLine; |
260 | var strategy; | |
261 | if (!strokePattern || strokePattern.length <= 1) { | |
262 | strategy = trivialStrategy(ctx, color, strokeWidth); | |
b843b52c | 263 | } else { |
31f8e58b | 264 | strategy = nonTrivialStrategy(this, ctx, color, strokeWidth, strokePattern); |
b843b52c | 265 | } |
31f8e58b RK |
266 | pointsOnLine = this._drawSeries(ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, strategy); |
267 | this._drawPointsOnLine(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize); | |
268 | ||
b843b52c RK |
269 | ctx.restore(); |
270 | }; | |
271 | ||
31f8e58b RK |
272 | var nonTrivialStrategy = function(renderer, ctx, color, strokeWidth, strokePattern) { |
273 | return new function() { | |
274 | this.init = function() { }; | |
275 | this.finish = function() { }; | |
276 | this.startSegment = function() { | |
277 | ctx.beginPath(); | |
278 | ctx.strokeStyle = color; | |
279 | ctx.lineWidth = strokeWidth; | |
280 | }; | |
281 | this.endSegment = function() { | |
282 | ctx.stroke(); // should this include closePath? | |
283 | }; | |
284 | this.drawLine = function(x1, y1, x2, y2) { | |
285 | renderer._dashedLine(ctx, x1, y1, x2, y2, strokePattern); | |
286 | }; | |
287 | this.skipPixel = function(prevX, prevY, curX, curY) { | |
288 | // TODO(konigsberg): optimize with http://jsperf.com/math-round-vs-hack/6 ? | |
289 | return (Math.round(prevX) == Math.round(curX) && | |
290 | Math.round(prevY) == Math.round(curY)); | |
291 | }; | |
292 | }; | |
293 | }; | |
294 | ||
295 | var trivialStrategy = function(ctx, color, strokeWidth) { | |
296 | return new function() { | |
297 | this.init = function() { | |
298 | ctx.beginPath(); | |
299 | ctx.strokeStyle = color; | |
300 | ctx.lineWidth = strokeWidth; | |
301 | }; | |
302 | this.finish = function() { | |
303 | ctx.stroke(); // should this include closePath? | |
304 | }; | |
305 | this.startSegment = function() { }; | |
306 | this.endSegment = function() { }; | |
307 | this.drawLine = function(x1, y1, x2, y2) { | |
308 | ctx.moveTo(x1, y1); | |
309 | ctx.lineTo(x2, y2); | |
310 | }; | |
311 | // don't skip pixels. | |
312 | this.skipPixel = function() { | |
313 | return false; | |
314 | }; | |
315 | }; | |
316 | }; | |
317 | ||
a401ca8a RK |
318 | DygraphCanvasRenderer.prototype._drawPointsOnLine = function(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize) { |
319 | for (var idx = 0; idx < pointsOnLine.length; idx++) { | |
320 | var cb = pointsOnLine[idx]; | |
321 | ctx.save(); | |
322 | drawPointCallback( | |
323 | this.dygraph_, setName, ctx, cb[0], cb[1], color, pointSize); | |
324 | ctx.restore(); | |
325 | } | |
326 | } | |
327 | ||
31f8e58b RK |
328 | DygraphCanvasRenderer.prototype._drawSeries = function( |
329 | ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, | |
330 | stepPlot, strategy) { | |
331 | ||
31f8e58b RK |
332 | var prevCanvasX = null; |
333 | var prevCanvasY = null; | |
334 | var nextCanvasY = null; | |
335 | var isIsolated; // true if this point is isolated (no line segments) | |
336 | var point; // the point being processed in the while loop | |
b843b52c | 337 | var pointsOnLine = []; // Array of [canvasx, canvasy] pairs. |
31f8e58b RK |
338 | var first = true; // the first cycle through the while loop |
339 | ||
340 | strategy.init(); | |
341 | ||
7d1afbb9 RK |
342 | while(iter.hasNext()) { |
343 | point = iter.next(); | |
a02978e2 | 344 | if (point.canvasy === null || point.canvasy != point.canvasy) { |
31f8e58b | 345 | if (stepPlot && prevCanvasX !== null) { |
857a6931 | 346 | // Draw a horizontal line to the start of the missing data |
31f8e58b RK |
347 | strategy.startSegment(); |
348 | strategy.drawLine(prevX, prevY, point.canvasx, prevY); | |
349 | strategy.endSegment(); | |
857a6931 | 350 | } |
31f8e58b | 351 | prevCanvasX = prevCanvasY = null; |
857a6931 | 352 | } else { |
31f8e58b | 353 | nextCanvasY = iter.hasNext() ? iter.peek().canvasy : null; |
a02978e2 RK |
354 | // TODO: we calculate isNullOrNaN for this point, and the next, and then, when |
355 | // we iterate, test for isNullOrNaN again. Why bother? | |
356 | var isNextCanvasYNullOrNaN = nextCanvasY === null || nextCanvasY != nextCanvasY; | |
357 | isIsolated = (!prevCanvasX && isNextCanvasYNullOrNaN); | |
19b84fe7 | 358 | if (drawGapPoints) { |
31f8e58b | 359 | // Also consider a point to be "isolated" if it's adjacent to a |
19b84fe7 | 360 | // null point, excluding the graph edges. |
31f8e58b | 361 | if ((!first && !prevCanvasX) || |
a02978e2 | 362 | (iter.hasNext() && isNextCanvasYNullOrNaN)) { |
19b84fe7 KW |
363 | isIsolated = true; |
364 | } | |
365 | } | |
31f8e58b RK |
366 | if (prevCanvasX !== null) { |
367 | if (strategy.skipPixel(prevCanvasX, prevCanvasY, point.canvasx, point.canvasy)) { | |
857a6931 KW |
368 | continue; |
369 | } | |
857a6931 | 370 | if (strokeWidth) { |
31f8e58b | 371 | strategy.startSegment(); |
857a6931 | 372 | if (stepPlot) { |
31f8e58b RK |
373 | strategy.drawLine(prevCanvasX, prevCanvasY, point.canvasx, prevCanvasY); |
374 | prevCanvasX = point.canvasx; | |
857a6931 | 375 | } |
31f8e58b RK |
376 | strategy.drawLine(prevCanvasX, prevCanvasY, point.canvasx, point.canvasy); |
377 | strategy.endSegment(); | |
b843b52c RK |
378 | } |
379 | } | |
b843b52c RK |
380 | if (drawPoints || isIsolated) { |
381 | pointsOnLine.push([point.canvasx, point.canvasy]); | |
382 | } | |
31f8e58b RK |
383 | prevCanvasX = point.canvasx; |
384 | prevCanvasY = point.canvasy; | |
b843b52c | 385 | } |
7d1afbb9 | 386 | first = false; |
b843b52c | 387 | } |
31f8e58b RK |
388 | strategy.finish(); |
389 | return pointsOnLine; | |
857a6931 KW |
390 | }; |
391 | ||
392 | DygraphCanvasRenderer.prototype._drawLine = function(ctx, i) { | |
393 | var setNames = this.layout.setNames; | |
394 | var setName = setNames[i]; | |
395 | ||
396 | var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); | |
397 | var borderWidth = this.dygraph_.attr_("strokeBorderWidth", setName); | |
5469113b KW |
398 | var drawPointCallback = this.dygraph_.attr_("drawPointCallback", setName) || |
399 | Dygraph.Circles.DEFAULT; | |
99a77a04 | 400 | |
857a6931 | 401 | if (borderWidth && strokeWidth) { |
5469113b | 402 | this._drawStyledLine(ctx, i, setName, |
857a6931 KW |
403 | this.dygraph_.attr_("strokeBorderColor", setName), |
404 | strokeWidth + 2 * borderWidth, | |
405 | this.dygraph_.attr_("strokePattern", setName), | |
406 | this.dygraph_.attr_("drawPoints", setName), | |
5469113b | 407 | drawPointCallback, |
857a6931 KW |
408 | this.dygraph_.attr_("pointSize", setName)); |
409 | } | |
410 | ||
5469113b | 411 | this._drawStyledLine(ctx, i, setName, |
857a6931 KW |
412 | this.colors[setName], |
413 | strokeWidth, | |
414 | this.dygraph_.attr_("strokePattern", setName), | |
415 | this.dygraph_.attr_("drawPoints", setName), | |
5469113b | 416 | drawPointCallback, |
857a6931 KW |
417 | this.dygraph_.attr_("pointSize", setName)); |
418 | }; | |
ce49c2fa | 419 | |
6a1aa64f | 420 | /** |
758a629f DV |
421 | * Actually draw the lines chart, including error bars. |
422 | * TODO(danvk): split this into several smaller functions. | |
423 | * @private | |
6a1aa64f | 424 | */ |
285a6bda | 425 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
44c6bc29 | 426 | // TODO(danvk): use this.attr_ for many of these. |
857a6931 | 427 | var ctx = this.elementContext; |
423f5ed3 | 428 | var fillAlpha = this.attr_('fillAlpha'); |
e4182459 | 429 | var errorBars = this.attr_("errorBars") || this.attr_("customBars"); |
44c6bc29 | 430 | var fillGraph = this.attr_("fillGraph"); |
b2c9222a DV |
431 | var stackedGraph = this.attr_("stackedGraph"); |
432 | var stepPlot = this.attr_("stepPlot"); | |
c3e1495b AR |
433 | var points = this.layout.points; |
434 | var pointsLength = points.length; | |
8722284b | 435 | var point, i, prevX, prevY, prevYs, color, setName, newYs, err_color, rgb, yscale, axis; |
21d3323f | 436 | |
82c6fe4d | 437 | var setNames = this.layout.setNames; |
21d3323f | 438 | var setCount = setNames.length; |
6a1aa64f | 439 | |
ee53deb9 | 440 | this.colors = this.dygraph_.colorsMap_; |
f032c51d | 441 | |
ff00d3e2 DV |
442 | // Update Points |
443 | // TODO(danvk): here | |
b843b52c RK |
444 | // |
445 | // TODO(bhs): this loop is a hot-spot for high-point-count charts. These | |
446 | // transformations can be pushed into the canvas via linear transformation | |
447 | // matrices. | |
758a629f DV |
448 | for (i = pointsLength; i--;) { |
449 | point = points[i]; | |
6a1aa64f DV |
450 | point.canvasx = this.area.w * point.x + this.area.x; |
451 | point.canvasy = this.area.h * point.y + this.area.y; | |
452 | } | |
6a1aa64f DV |
453 | |
454 | // create paths | |
80aaae18 | 455 | if (errorBars) { |
857a6931 | 456 | ctx.save(); |
6a834bbb DV |
457 | if (fillGraph) { |
458 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
459 | } | |
460 | ||
758a629f DV |
461 | for (i = 0; i < setCount; i++) { |
462 | setName = setNames[i]; | |
463 | axis = this.dygraph_.axisPropertiesForSeries(setName); | |
464 | color = this.colors[setName]; | |
6a1aa64f | 465 | |
04c104d7 KW |
466 | var firstIndexInSet = this.layout.setPointsOffsets[i]; |
467 | var setLength = this.layout.setPointsLengths[i]; | |
04c104d7 | 468 | |
8722284b RK |
469 | var iter = Dygraph.createIterator(points, firstIndexInSet, setLength, |
470 | DygraphCanvasRenderer._getIteratorPredicate(this.attr_("connectSeparatedPoints"))); | |
04c104d7 | 471 | |
6a1aa64f | 472 | // setup graphics context |
758a629f DV |
473 | prevX = NaN; |
474 | prevY = NaN; | |
475 | prevYs = [-1, -1]; | |
476 | yscale = axis.yscale; | |
f474c2a3 | 477 | // should be same color as the lines but only 15% opaque. |
758a629f DV |
478 | rgb = new RGBColor(color); |
479 | err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + | |
43af96e7 | 480 | fillAlpha + ')'; |
f474c2a3 | 481 | ctx.fillStyle = err_color; |
05c9d0c4 | 482 | ctx.beginPath(); |
8722284b RK |
483 | while (iter.hasNext()) { |
484 | point = iter.next(); | |
04c104d7 | 485 | if (point.name == setName) { // TODO(klausw): this is always true |
e9fe4a2f | 486 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 487 | prevX = NaN; |
ae85914a | 488 | continue; |
5011e7a1 | 489 | } |
ce49c2fa | 490 | |
3637724f | 491 | // TODO(danvk): here |
afdc483f | 492 | if (stepPlot) { |
758a629f | 493 | newYs = [ point.y_bottom, point.y_top ]; |
afdc483f NN |
494 | prevY = point.y; |
495 | } else { | |
758a629f | 496 | newYs = [ point.y_bottom, point.y_top ]; |
afdc483f | 497 | } |
6a1aa64f DV |
498 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
499 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 500 | if (!isNaN(prevX)) { |
afdc483f | 501 | if (stepPlot) { |
47600757 | 502 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 503 | } else { |
47600757 | 504 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 505 | } |
5954ef32 DV |
506 | ctx.lineTo(point.canvasx, newYs[0]); |
507 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 508 | if (stepPlot) { |
47600757 | 509 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 510 | } else { |
47600757 | 511 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 512 | } |
5954ef32 DV |
513 | ctx.closePath(); |
514 | } | |
354e15ab | 515 | prevYs = newYs; |
5954ef32 DV |
516 | prevX = point.canvasx; |
517 | } | |
518 | } | |
519 | ctx.fill(); | |
520 | } | |
857a6931 | 521 | ctx.restore(); |
5954ef32 | 522 | } else if (fillGraph) { |
857a6931 | 523 | ctx.save(); |
349dd9ba DW |
524 | var baseline = {}; // for stacked graphs: baseline for filling |
525 | var currBaseline; | |
354e15ab DE |
526 | |
527 | // process sets in reverse order (needed for stacked graphs) | |
758a629f DV |
528 | for (i = setCount - 1; i >= 0; i--) { |
529 | setName = setNames[i]; | |
530 | color = this.colors[setName]; | |
531 | axis = this.dygraph_.axisPropertiesForSeries(setName); | |
ea4942ed DV |
532 | var axisY = 1.0 + axis.minyval * axis.yscale; |
533 | if (axisY < 0.0) axisY = 0.0; | |
534 | else if (axisY > 1.0) axisY = 1.0; | |
535 | axisY = this.area.h * axisY + this.area.y; | |
04c104d7 KW |
536 | var firstIndexInSet = this.layout.setPointsOffsets[i]; |
537 | var setLength = this.layout.setPointsLengths[i]; | |
04c104d7 | 538 | |
8722284b RK |
539 | var iter = Dygraph.createIterator(points, firstIndexInSet, setLength, |
540 | DygraphCanvasRenderer._getIteratorPredicate(this.attr_("connectSeparatedPoints"))); | |
5954ef32 DV |
541 | |
542 | // setup graphics context | |
758a629f DV |
543 | prevX = NaN; |
544 | prevYs = [-1, -1]; | |
545 | yscale = axis.yscale; | |
5954ef32 | 546 | // should be same color as the lines but only 15% opaque. |
758a629f DV |
547 | rgb = new RGBColor(color); |
548 | err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + | |
43af96e7 | 549 | fillAlpha + ')'; |
5954ef32 DV |
550 | ctx.fillStyle = err_color; |
551 | ctx.beginPath(); | |
8722284b RK |
552 | while(iter.hasNext()) { |
553 | point = iter.next(); | |
04c104d7 | 554 | if (point.name == setName) { // TODO(klausw): this is always true |
e9fe4a2f | 555 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 556 | prevX = NaN; |
5954ef32 DV |
557 | continue; |
558 | } | |
354e15ab | 559 | if (stackedGraph) { |
349dd9ba DW |
560 | currBaseline = baseline[point.canvasx]; |
561 | var lastY; | |
562 | if (currBaseline === undefined) { | |
563 | lastY = axisY; | |
564 | } else { | |
565 | if(stepPlot) { | |
566 | lastY = currBaseline[0]; | |
567 | } else { | |
568 | lastY = currBaseline; | |
569 | } | |
570 | } | |
354e15ab | 571 | newYs = [ point.canvasy, lastY ]; |
b843b52c | 572 | |
349dd9ba DW |
573 | if(stepPlot) { |
574 | // Step plots must keep track of the top and bottom of | |
575 | // the baseline at each point. | |
576 | if(prevYs[0] === -1) { | |
577 | baseline[point.canvasx] = [ point.canvasy, axisY ]; | |
578 | } else { | |
579 | baseline[point.canvasx] = [ point.canvasy, prevYs[0] ]; | |
580 | } | |
581 | } else { | |
582 | baseline[point.canvasx] = point.canvasy; | |
583 | } | |
b843b52c | 584 | |
354e15ab DE |
585 | } else { |
586 | newYs = [ point.canvasy, axisY ]; | |
587 | } | |
56623f3b | 588 | if (!isNaN(prevX)) { |
05c9d0c4 | 589 | ctx.moveTo(prevX, prevYs[0]); |
b843b52c | 590 | |
afdc483f | 591 | if (stepPlot) { |
47600757 | 592 | ctx.lineTo(point.canvasx, prevYs[0]); |
349dd9ba DW |
593 | if(currBaseline) { |
594 | // Draw to the bottom of the baseline | |
595 | ctx.lineTo(point.canvasx, currBaseline[1]); | |
596 | } else { | |
597 | ctx.lineTo(point.canvasx, newYs[1]); | |
598 | } | |
afdc483f | 599 | } else { |
47600757 | 600 | ctx.lineTo(point.canvasx, newYs[0]); |
349dd9ba | 601 | ctx.lineTo(point.canvasx, newYs[1]); |
afdc483f | 602 | } |
b843b52c | 603 | |
05c9d0c4 DV |
604 | ctx.lineTo(prevX, prevYs[1]); |
605 | ctx.closePath(); | |
6a1aa64f | 606 | } |
354e15ab | 607 | prevYs = newYs; |
6a1aa64f DV |
608 | prevX = point.canvasx; |
609 | } | |
05c9d0c4 | 610 | } |
6a1aa64f DV |
611 | ctx.fill(); |
612 | } | |
857a6931 | 613 | ctx.restore(); |
80aaae18 DV |
614 | } |
615 | ||
f9414b11 | 616 | // Drawing the lines. |
758a629f | 617 | for (i = 0; i < setCount; i += 1) { |
857a6931 | 618 | this._drawLine(ctx, i); |
80aaae18 | 619 | } |
6a1aa64f | 620 | }; |
79253bd0 | 621 | |
622 | /** | |
623 | * This does dashed lines onto a canvas for a given pattern. You must call | |
624 | * ctx.stroke() after to actually draw it, much line ctx.lineTo(). It remembers | |
625 | * the state of the line in regards to where we left off on drawing the pattern. | |
626 | * You can draw a dashed line in several function calls and the pattern will be | |
627 | * continous as long as you didn't call this function with a different pattern | |
628 | * in between. | |
629 | * @param ctx The canvas 2d context to draw on. | |
630 | * @param x The start of the line's x coordinate. | |
631 | * @param y The start of the line's y coordinate. | |
632 | * @param x2 The end of the line's x coordinate. | |
633 | * @param y2 The end of the line's y coordinate. | |
634 | * @param pattern The dash pattern to draw, an array of integers where even | |
635 | * index is drawn and odd index is not drawn (Ex. [10, 2, 5, 2], 10 is drawn 5 | |
636 | * is drawn, 2 is the space between.). A null pattern, array of length one, or | |
637 | * empty array will do just a solid line. | |
638 | * @private | |
639 | */ | |
640 | DygraphCanvasRenderer.prototype._dashedLine = function(ctx, x, y, x2, y2, pattern) { | |
641 | // Original version http://stackoverflow.com/questions/4576724/dotted-stroke-in-canvas | |
642 | // Modified by Russell Valentine to keep line history and continue the pattern | |
643 | // where it left off. | |
644 | var dx, dy, len, rot, patternIndex, segment; | |
645 | ||
646 | // If we don't have a pattern or it is an empty array or of size one just | |
647 | // do a solid line. | |
648 | if (!pattern || pattern.length <= 1) { | |
649 | ctx.moveTo(x, y); | |
650 | ctx.lineTo(x2, y2); | |
651 | return; | |
652 | } | |
653 | ||
654 | // If we have a different dash pattern than the last time this was called we | |
655 | // reset our dash history and start the pattern from the begging | |
656 | // regardless of state of the last pattern. | |
657 | if (!Dygraph.compareArrays(pattern, this._dashedLineToHistoryPattern)) { | |
658 | this._dashedLineToHistoryPattern = pattern; | |
659 | this._dashedLineToHistory = [0, 0]; | |
660 | } | |
661 | ctx.save(); | |
662 | ||
663 | // Calculate transformation parameters | |
664 | dx = (x2-x); | |
665 | dy = (y2-y); | |
666 | len = Math.sqrt(dx*dx + dy*dy); | |
667 | rot = Math.atan2(dy, dx); | |
668 | ||
669 | // Set transformation | |
670 | ctx.translate(x, y); | |
671 | ctx.moveTo(0, 0); | |
672 | ctx.rotate(rot); | |
673 | ||
674 | // Set last pattern index we used for this pattern. | |
675 | patternIndex = this._dashedLineToHistory[0]; | |
676 | x = 0; | |
677 | while (len > x) { | |
678 | // Get the length of the pattern segment we are dealing with. | |
679 | segment = pattern[patternIndex]; | |
680 | // If our last draw didn't complete the pattern segment all the way we | |
681 | // will try to finish it. Otherwise we will try to do the whole segment. | |
682 | if (this._dashedLineToHistory[1]) { | |
683 | x += this._dashedLineToHistory[1]; | |
684 | } else { | |
685 | x += segment; | |
686 | } | |
687 | if (x > len) { | |
688 | // We were unable to complete this pattern index all the way, keep | |
689 | // where we are the history so our next draw continues where we left off | |
690 | // in the pattern. | |
691 | this._dashedLineToHistory = [patternIndex, x-len]; | |
692 | x = len; | |
693 | } else { | |
694 | // We completed this patternIndex, we put in the history that we are on | |
695 | // the beginning of the next segment. | |
696 | this._dashedLineToHistory = [(patternIndex+1)%pattern.length, 0]; | |
697 | } | |
698 | ||
699 | // We do a line on a even pattern index and just move on a odd pattern index. | |
700 | // The move is the empty space in the dash. | |
701 | if(patternIndex % 2 === 0) { | |
702 | ctx.lineTo(x, 0); | |
703 | } else { | |
704 | ctx.moveTo(x, 0); | |
705 | } | |
706 | // If we are not done, next loop process the next pattern segment, or the | |
707 | // first segment again if we are at the end of the pattern. | |
708 | patternIndex = (patternIndex+1) % pattern.length; | |
709 | } | |
710 | ctx.restore(); | |
711 | }; |