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 | 27 | /*jshint globalstrict: true */ |
a96b8ba3 | 28 | /*global Dygraph:false,RGBColorParser: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 | * | |
7b00a3cd | 42 | * @param {Dygraph} dygraph The chart to which this renderer belongs. |
48fc4786 | 43 | * @param {HTMLCanvasElement} element The <canvas> DOM element on which to draw. |
8cfe592f DV |
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 | |
48fc4786 | 61 | // NOTE(konigsberg): isIE is never defined in this object. Bug of some sort. |
fbe31dc8 DV |
62 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) |
63 | throw "Canvas is not supported."; | |
64 | ||
65 | // internal state | |
70be5ed1 | 66 | this.area = layout.getPlotArea(); |
423f5ed3 DV |
67 | this.container.style.position = "relative"; |
68 | this.container.style.width = this.width + "px"; | |
69 | ||
70 | // Set up a clipping area for the canvas (and the interaction canvas). | |
71 | // This ensures that we don't overdraw. | |
920208fb PF |
72 | if (this.dygraph_.isUsingExcanvas_) { |
73 | this._createIEClipArea(); | |
74 | } else { | |
971870e5 DV |
75 | // on Android 3 and 4, setting a clipping area on a canvas prevents it from |
76 | // displaying anything. | |
77 | if (!Dygraph.isAndroid()) { | |
78 | var ctx = this.dygraph_.canvas_ctx_; | |
79 | ctx.beginPath(); | |
80 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
81 | ctx.clip(); | |
920208fb | 82 | |
971870e5 DV |
83 | ctx = this.dygraph_.hidden_ctx_; |
84 | ctx.beginPath(); | |
85 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
86 | ctx.clip(); | |
87 | } | |
920208fb | 88 | } |
423f5ed3 DV |
89 | }; |
90 | ||
38e3d209 DV |
91 | /** |
92 | * This just forwards to dygraph.attr_. | |
93 | * TODO(danvk): remove this? | |
94 | * @private | |
95 | */ | |
96 | DygraphCanvasRenderer.prototype.attr_ = function(name, opt_seriesName) { | |
97 | return this.dygraph_.attr_(name, opt_seriesName); | |
423f5ed3 DV |
98 | }; |
99 | ||
8cfe592f DV |
100 | /** |
101 | * Clears out all chart content and DOM elements. | |
102 | * This is called immediately before render() on every frame, including | |
103 | * during zooms and pans. | |
104 | * @private | |
105 | */ | |
fbe31dc8 | 106 | DygraphCanvasRenderer.prototype.clear = function() { |
758a629f | 107 | var context; |
fbe31dc8 DV |
108 | if (this.isIE) { |
109 | // VML takes a while to start up, so we just poll every this.IEDelay | |
110 | try { | |
111 | if (this.clearDelay) { | |
112 | this.clearDelay.cancel(); | |
113 | this.clearDelay = null; | |
114 | } | |
758a629f | 115 | context = this.elementContext; |
fbe31dc8 DV |
116 | } |
117 | catch (e) { | |
76171648 | 118 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
758a629f DV |
119 | // this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
120 | // this.clearDelay.addCallback(bind(this.clear, this)); | |
fbe31dc8 DV |
121 | return; |
122 | } | |
123 | } | |
124 | ||
758a629f | 125 | context = this.elementContext; |
fbe31dc8 | 126 | context.clearRect(0, 0, this.width, this.height); |
fbe31dc8 DV |
127 | }; |
128 | ||
8cfe592f DV |
129 | /** |
130 | * Checks whether the browser supports the <canvas> tag. | |
131 | * @private | |
132 | */ | |
fbe31dc8 DV |
133 | DygraphCanvasRenderer.isSupported = function(canvasName) { |
134 | var canvas = null; | |
135 | try { | |
758a629f | 136 | if (typeof(canvasName) == 'undefined' || canvasName === null) { |
b0c3b730 | 137 | canvas = document.createElement("canvas"); |
758a629f | 138 | } else { |
b0c3b730 | 139 | canvas = canvasName; |
758a629f DV |
140 | } |
141 | canvas.getContext("2d"); | |
fbe31dc8 DV |
142 | } |
143 | catch (e) { | |
144 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
145 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
146 | if ((!ie) || (ie[1] < 6) || (opera)) | |
147 | return false; | |
148 | return true; | |
149 | } | |
150 | return true; | |
6a1aa64f | 151 | }; |
6a1aa64f DV |
152 | |
153 | /** | |
8cfe592f DV |
154 | * This method is responsible for drawing everything on the chart, including |
155 | * lines, error bars, fills and axes. | |
156 | * It is called immediately after clear() on every frame, including during pans | |
157 | * and zooms. | |
158 | * @private | |
6a1aa64f | 159 | */ |
285a6bda | 160 | DygraphCanvasRenderer.prototype.render = function() { |
38e3d209 DV |
161 | // attaches point.canvas{x,y} |
162 | this._updatePoints(); | |
163 | ||
164 | // actually draws the chart. | |
2ce09b19 | 165 | this._renderLineChart(); |
fbe31dc8 DV |
166 | }; |
167 | ||
920208fb PF |
168 | DygraphCanvasRenderer.prototype._createIEClipArea = function() { |
169 | var className = 'dygraph-clip-div'; | |
170 | var graphDiv = this.dygraph_.graphDiv; | |
171 | ||
172 | // Remove old clip divs. | |
173 | for (var i = graphDiv.childNodes.length-1; i >= 0; i--) { | |
174 | if (graphDiv.childNodes[i].className == className) { | |
175 | graphDiv.removeChild(graphDiv.childNodes[i]); | |
176 | } | |
177 | } | |
178 | ||
179 | // Determine background color to give clip divs. | |
180 | var backgroundColor = document.bgColor; | |
181 | var element = this.dygraph_.graphDiv; | |
182 | while (element != document) { | |
183 | var bgcolor = element.currentStyle.backgroundColor; | |
184 | if (bgcolor && bgcolor != 'transparent') { | |
185 | backgroundColor = bgcolor; | |
186 | break; | |
187 | } | |
188 | element = element.parentNode; | |
189 | } | |
190 | ||
191 | function createClipDiv(area) { | |
758a629f | 192 | if (area.w === 0 || area.h === 0) { |
920208fb PF |
193 | return; |
194 | } | |
195 | var elem = document.createElement('div'); | |
196 | elem.className = className; | |
197 | elem.style.backgroundColor = backgroundColor; | |
198 | elem.style.position = 'absolute'; | |
199 | elem.style.left = area.x + 'px'; | |
200 | elem.style.top = area.y + 'px'; | |
201 | elem.style.width = area.w + 'px'; | |
202 | elem.style.height = area.h + 'px'; | |
203 | graphDiv.appendChild(elem); | |
204 | } | |
205 | ||
206 | var plotArea = this.area; | |
207 | // Left side | |
758a629f DV |
208 | createClipDiv({ |
209 | x:0, y:0, | |
210 | w:plotArea.x, | |
211 | h:this.height | |
212 | }); | |
213 | ||
920208fb | 214 | // Top |
758a629f DV |
215 | createClipDiv({ |
216 | x: plotArea.x, y: 0, | |
217 | w: this.width - plotArea.x, | |
218 | h: plotArea.y | |
219 | }); | |
220 | ||
920208fb | 221 | // Right side |
758a629f DV |
222 | createClipDiv({ |
223 | x: plotArea.x + plotArea.w, y: 0, | |
224 | w: this.width-plotArea.x - plotArea.w, | |
225 | h: this.height | |
226 | }); | |
227 | ||
920208fb | 228 | // Bottom |
758a629f DV |
229 | createClipDiv({ |
230 | x: plotArea.x, | |
231 | y: plotArea.y + plotArea.h, | |
232 | w: this.width - plotArea.x, | |
233 | h: this.height - plotArea.h - plotArea.y | |
234 | }); | |
235 | }; | |
fbe31dc8 | 236 | |
fbe31dc8 | 237 | |
ccb0001c | 238 | /** |
8722284b RK |
239 | * Returns a predicate to be used with an iterator, which will |
240 | * iterate over points appropriately, depending on whether | |
241 | * connectSeparatedPoints is true. When it's false, the predicate will | |
242 | * skip over points with missing yVals. | |
ccb0001c | 243 | */ |
8722284b | 244 | DygraphCanvasRenderer._getIteratorPredicate = function(connectSeparatedPoints) { |
42a9ebb8 DV |
245 | return connectSeparatedPoints ? |
246 | DygraphCanvasRenderer._predicateThatSkipsEmptyPoints : | |
247 | null; | |
0f20de1c | 248 | }; |
8722284b RK |
249 | |
250 | DygraphCanvasRenderer._predicateThatSkipsEmptyPoints = | |
0f20de1c DV |
251 | function(array, idx) { |
252 | return array[idx].yval !== null; | |
253 | }; | |
04c104d7 | 254 | |
9f6db80e | 255 | /** |
38e3d209 DV |
256 | * Draws a line with the styles passed in and calls all the drawPointCallbacks. |
257 | * @param {Object} e The dictionary passed to the plotter function. | |
9f6db80e DV |
258 | * @private |
259 | */ | |
38e3d209 DV |
260 | DygraphCanvasRenderer._drawStyledLine = function(e, |
261 | color, strokeWidth, strokePattern, drawPoints, | |
5469113b | 262 | drawPointCallback, pointSize) { |
38e3d209 | 263 | var g = e.dygraph; |
99a77a04 | 264 | // TODO(konigsberg): Compute attributes outside this method call. |
0ce5b19b | 265 | var stepPlot = g.getOption("stepPlot", e.setName); |
2f56cd46 | 266 | |
857a6931 KW |
267 | if (!Dygraph.isArrayLike(strokePattern)) { |
268 | strokePattern = null; | |
269 | } | |
270 | ||
38e3d209 DV |
271 | var drawGapPoints = g.getOption('drawGapEdgePoints', e.setName); |
272 | ||
273 | var points = e.points; | |
b85358e2 | 274 | var setName = e.setName; |
a12a78ae | 275 | var iter = Dygraph.createIterator(points, 0, points.length, |
9f6db80e | 276 | DygraphCanvasRenderer._getIteratorPredicate( |
b85358e2 | 277 | g.getOption("connectSeparatedPoints", setName))); |
7d1afbb9 | 278 | |
fb63bf1b DV |
279 | var stroking = strokePattern && (strokePattern.length >= 2); |
280 | ||
38e3d209 | 281 | var ctx = e.drawingContext; |
0140347d | 282 | ctx.save(); |
fb63bf1b DV |
283 | if (stroking) { |
284 | ctx.installPattern(strokePattern); | |
b843b52c | 285 | } |
fb63bf1b | 286 | |
38e3d209 DV |
287 | var pointsOnLine = DygraphCanvasRenderer._drawSeries( |
288 | e, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, color); | |
289 | DygraphCanvasRenderer._drawPointsOnLine( | |
290 | e, pointsOnLine, drawPointCallback, color, pointSize); | |
31f8e58b | 291 | |
fb63bf1b DV |
292 | if (stroking) { |
293 | ctx.uninstallPattern(); | |
294 | } | |
b843b52c | 295 | |
fb63bf1b | 296 | ctx.restore(); |
31f8e58b RK |
297 | }; |
298 | ||
38e3d209 DV |
299 | /** |
300 | * This does the actual drawing of lines on the canvas, for just one series. | |
301 | * Returns a list of [canvasx, canvasy] pairs for points for which a | |
302 | * drawPointCallback should be fired. These include isolated points, or all | |
303 | * points if drawPoints=true. | |
304 | * @param {Object} e The dictionary passed to the plotter function. | |
305 | * @private | |
306 | */ | |
307 | DygraphCanvasRenderer._drawSeries = function(e, | |
308 | iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, color) { | |
31f8e58b | 309 | |
31f8e58b RK |
310 | var prevCanvasX = null; |
311 | var prevCanvasY = null; | |
312 | var nextCanvasY = null; | |
313 | var isIsolated; // true if this point is isolated (no line segments) | |
314 | var point; // the point being processed in the while loop | |
b843b52c | 315 | var pointsOnLine = []; // Array of [canvasx, canvasy] pairs. |
31f8e58b RK |
316 | var first = true; // the first cycle through the while loop |
317 | ||
38e3d209 | 318 | var ctx = e.drawingContext; |
0140347d DV |
319 | ctx.beginPath(); |
320 | ctx.strokeStyle = color; | |
321 | ctx.lineWidth = strokeWidth; | |
31f8e58b | 322 | |
239454e2 | 323 | // NOTE: we break the iterator's encapsulation here for about a 25% speedup. |
c560c848 DV |
324 | var arr = iter.array_; |
325 | var limit = iter.end_; | |
326 | var predicate = iter.predicate_; | |
327 | ||
328 | for (var i = iter.start_; i < limit; i++) { | |
329 | point = arr[i]; | |
330 | if (predicate) { | |
331 | while (i < limit && !predicate(arr, i)) { | |
0f20de1c DV |
332 | i++; |
333 | } | |
c560c848 DV |
334 | if (i == limit) break; |
335 | point = arr[i]; | |
0f20de1c DV |
336 | } |
337 | ||
a02978e2 | 338 | if (point.canvasy === null || point.canvasy != point.canvasy) { |
31f8e58b | 339 | if (stepPlot && prevCanvasX !== null) { |
857a6931 | 340 | // Draw a horizontal line to the start of the missing data |
42a9ebb8 DV |
341 | ctx.moveTo(prevCanvasX, prevCanvasY); |
342 | ctx.lineTo(point.canvasx, prevCanvasY); | |
857a6931 | 343 | } |
31f8e58b | 344 | prevCanvasX = prevCanvasY = null; |
857a6931 | 345 | } else { |
0f20de1c DV |
346 | isIsolated = false; |
347 | if (drawGapPoints || !prevCanvasX) { | |
0f20de1c | 348 | iter.nextIdx_ = i; |
0cd1ad15 | 349 | iter.next(); |
82f9b10f | 350 | nextCanvasY = iter.hasNext ? iter.peek.canvasy : null; |
0f20de1c | 351 | |
0f20de1c DV |
352 | var isNextCanvasYNullOrNaN = nextCanvasY === null || |
353 | nextCanvasY != nextCanvasY; | |
354 | isIsolated = (!prevCanvasX && isNextCanvasYNullOrNaN); | |
355 | if (drawGapPoints) { | |
356 | // Also consider a point to be "isolated" if it's adjacent to a | |
357 | // null point, excluding the graph edges. | |
358 | if ((!first && !prevCanvasX) || | |
359 | (iter.hasNext && isNextCanvasYNullOrNaN)) { | |
360 | isIsolated = true; | |
361 | } | |
19b84fe7 KW |
362 | } |
363 | } | |
0f20de1c | 364 | |
31f8e58b | 365 | if (prevCanvasX !== null) { |
857a6931 | 366 | if (strokeWidth) { |
857a6931 | 367 | if (stepPlot) { |
0140347d DV |
368 | ctx.moveTo(prevCanvasX, prevCanvasY); |
369 | ctx.lineTo(point.canvasx, prevCanvasY); | |
857a6931 | 370 | } |
239454e2 | 371 | |
0140347d | 372 | ctx.lineTo(point.canvasx, point.canvasy); |
b843b52c | 373 | } |
9f636500 DV |
374 | } else { |
375 | ctx.moveTo(point.canvasx, point.canvasy); | |
b843b52c | 376 | } |
b843b52c | 377 | if (drawPoints || isIsolated) { |
b616fad1 | 378 | pointsOnLine.push([point.canvasx, point.canvasy, point.idx]); |
b843b52c | 379 | } |
31f8e58b RK |
380 | prevCanvasX = point.canvasx; |
381 | prevCanvasY = point.canvasy; | |
b843b52c | 382 | } |
7d1afbb9 | 383 | first = false; |
b843b52c | 384 | } |
0140347d | 385 | ctx.stroke(); |
31f8e58b | 386 | return pointsOnLine; |
857a6931 KW |
387 | }; |
388 | ||
38e3d209 DV |
389 | /** |
390 | * This fires the drawPointCallback functions, which draw dots on the points by | |
391 | * default. This gets used when the "drawPoints" option is set, or when there | |
392 | * are isolated points. | |
393 | * @param {Object} e The dictionary passed to the plotter function. | |
394 | * @private | |
395 | */ | |
396 | DygraphCanvasRenderer._drawPointsOnLine = function( | |
397 | e, pointsOnLine, drawPointCallback, color, pointSize) { | |
398 | var ctx = e.drawingContext; | |
399 | for (var idx = 0; idx < pointsOnLine.length; idx++) { | |
400 | var cb = pointsOnLine[idx]; | |
401 | ctx.save(); | |
402 | drawPointCallback( | |
ba697462 | 403 | e.dygraph, e.setName, ctx, cb[0], cb[1], color, pointSize, cb[2]); |
38e3d209 | 404 | ctx.restore(); |
857a6931 | 405 | } |
42a9ebb8 | 406 | }; |
ce49c2fa | 407 | |
6a1aa64f | 408 | /** |
38e3d209 | 409 | * Attaches canvas coordinates to the points array. |
758a629f | 410 | * @private |
6a1aa64f | 411 | */ |
38e3d209 | 412 | DygraphCanvasRenderer.prototype._updatePoints = function() { |
ff00d3e2 DV |
413 | // Update Points |
414 | // TODO(danvk): here | |
b843b52c RK |
415 | // |
416 | // TODO(bhs): this loop is a hot-spot for high-point-count charts. These | |
417 | // transformations can be pushed into the canvas via linear transformation | |
418 | // matrices. | |
e60234cd DV |
419 | // NOTE(danvk): this is trickier than it sounds at first. The transformation |
420 | // needs to be done before the .moveTo() and .lineTo() calls, but must be | |
421 | // undone before the .stroke() call to ensure that the stroke width is | |
422 | // unaffected. An alternative is to reduce the stroke width in the | |
423 | // transformed coordinate space, but you can't specify different values for | |
424 | // each dimension (as you can with .scale()). The speedup here is ~12%. | |
a12a78ae | 425 | var sets = this.layout.points; |
38e3d209 | 426 | for (var i = sets.length; i--;) { |
a12a78ae DV |
427 | var points = sets[i]; |
428 | for (var j = points.length; j--;) { | |
429 | var point = points[j]; | |
430 | point.canvasx = this.area.w * point.x + this.area.x; | |
431 | point.canvasy = this.area.h * point.y + this.area.y; | |
432 | } | |
6a1aa64f | 433 | } |
38e3d209 | 434 | }; |
6a1aa64f | 435 | |
38e3d209 DV |
436 | /** |
437 | * Add canvas Actually draw the lines chart, including error bars. | |
38e3d209 DV |
438 | * |
439 | * This function can only be called if DygraphLayout's points array has been | |
440 | * updated with canvas{x,y} attributes, i.e. by | |
441 | * DygraphCanvasRenderer._updatePoints. | |
48fc4786 RK |
442 | * |
443 | * @param {string=} opt_seriesName when specified, only that series will | |
34655aba RK |
444 | * be drawn. (This is used for expedited redrawing with highlightSeriesOpts) |
445 | * @param {CanvasRenderingContext2D} opt_ctx when specified, the drawing | |
446 | * context. However, lines are typically drawn on the object's | |
447 | * elementContext. | |
38e3d209 DV |
448 | * @private |
449 | */ | |
450 | DygraphCanvasRenderer.prototype._renderLineChart = function(opt_seriesName, opt_ctx) { | |
451 | var ctx = opt_ctx || this.elementContext; | |
38e3d209 | 452 | var i; |
6a834bbb | 453 | |
38e3d209 DV |
454 | var sets = this.layout.points; |
455 | var setNames = this.layout.setNames; | |
42a9ebb8 | 456 | var setName; |
38e3d209 DV |
457 | |
458 | this.colors = this.dygraph_.colorsMap_; | |
459 | ||
460 | // Determine which series have specialized plotters. | |
461 | var plotter_attr = this.attr_("plotter"); | |
462 | var plotters = plotter_attr; | |
463 | if (!Dygraph.isArrayLike(plotters)) { | |
464 | plotters = [plotters]; | |
80aaae18 DV |
465 | } |
466 | ||
38e3d209 DV |
467 | var setPlotters = {}; // series name -> plotter fn. |
468 | for (i = 0; i < setNames.length; i++) { | |
42a9ebb8 | 469 | setName = setNames[i]; |
38e3d209 DV |
470 | var setPlotter = this.attr_("plotter", setName); |
471 | if (setPlotter == plotter_attr) continue; // not specialized. | |
472 | ||
473 | setPlotters[setName] = setPlotter; | |
474 | } | |
475 | ||
476 | for (i = 0; i < plotters.length; i++) { | |
477 | var plotter = plotters[i]; | |
478 | var is_last = (i == plotters.length - 1); | |
479 | ||
480 | for (var j = 0; j < sets.length; j++) { | |
42a9ebb8 | 481 | setName = setNames[j]; |
4b2e41a4 | 482 | if (opt_seriesName && setName != opt_seriesName) continue; |
38e3d209 DV |
483 | |
484 | var points = sets[j]; | |
485 | ||
486 | // Only throw in the specialized plotters on the last iteration. | |
487 | var p = plotter; | |
488 | if (setName in setPlotters) { | |
489 | if (is_last) { | |
490 | p = setPlotters[setName]; | |
491 | } else { | |
492 | // Don't use the standard plotters in this case. | |
493 | continue; | |
494 | } | |
495 | } | |
496 | ||
497 | var color = this.colors[setName]; | |
498 | var strokeWidth = this.dygraph_.getOption("strokeWidth", setName); | |
499 | ||
500 | ctx.save(); | |
501 | ctx.strokeStyle = color; | |
502 | ctx.lineWidth = strokeWidth; | |
503 | p({ | |
504 | points: points, | |
505 | setName: setName, | |
506 | drawingContext: ctx, | |
507 | color: color, | |
508 | strokeWidth: strokeWidth, | |
509 | dygraph: this.dygraph_, | |
510 | axis: this.dygraph_.axisPropertiesForSeries(setName), | |
511 | plotArea: this.area, | |
512 | seriesIndex: j, | |
513 | seriesCount: sets.length, | |
3c080cd0 | 514 | singleSeriesName: opt_seriesName, |
38e3d209 DV |
515 | allSeriesPoints: sets |
516 | }); | |
517 | ctx.restore(); | |
518 | } | |
519 | } | |
520 | }; | |
521 | ||
522 | /** | |
523 | * Standard plotters. These may be used by clients via Dygraph.Plotters. | |
524 | * See comments there for more details. | |
525 | */ | |
526 | DygraphCanvasRenderer._Plotters = { | |
527 | linePlotter: function(e) { | |
528 | DygraphCanvasRenderer._linePlotter(e); | |
529 | }, | |
530 | ||
531 | fillPlotter: function(e) { | |
532 | DygraphCanvasRenderer._fillPlotter(e); | |
533 | }, | |
534 | ||
535 | errorPlotter: function(e) { | |
536 | DygraphCanvasRenderer._errorPlotter(e); | |
80aaae18 | 537 | } |
6a1aa64f | 538 | }; |
79253bd0 | 539 | |
01a14b85 | 540 | /** |
38e3d209 DV |
541 | * Plotter which draws the central lines for a series. |
542 | * @private | |
543 | */ | |
544 | DygraphCanvasRenderer._linePlotter = function(e) { | |
545 | var g = e.dygraph; | |
546 | var setName = e.setName; | |
547 | var strokeWidth = e.strokeWidth; | |
548 | ||
549 | // TODO(danvk): Check if there's any performance impact of just calling | |
550 | // getOption() inside of _drawStyledLine. Passing in so many parameters makes | |
551 | // this code a bit nasty. | |
552 | var borderWidth = g.getOption("strokeBorderWidth", setName); | |
553 | var drawPointCallback = g.getOption("drawPointCallback", setName) || | |
554 | Dygraph.Circles.DEFAULT; | |
555 | var strokePattern = g.getOption("strokePattern", setName); | |
556 | var drawPoints = g.getOption("drawPoints", setName); | |
557 | var pointSize = g.getOption("pointSize", setName); | |
558 | ||
559 | if (borderWidth && strokeWidth) { | |
560 | DygraphCanvasRenderer._drawStyledLine(e, | |
561 | g.getOption("strokeBorderColor", setName), | |
562 | strokeWidth + 2 * borderWidth, | |
563 | strokePattern, | |
564 | drawPoints, | |
565 | drawPointCallback, | |
566 | pointSize | |
567 | ); | |
568 | } | |
569 | ||
570 | DygraphCanvasRenderer._drawStyledLine(e, | |
571 | e.color, | |
572 | strokeWidth, | |
573 | strokePattern, | |
574 | drawPoints, | |
575 | drawPointCallback, | |
576 | pointSize | |
577 | ); | |
42a9ebb8 | 578 | }; |
38e3d209 DV |
579 | |
580 | /** | |
01a14b85 DV |
581 | * Draws the shaded error bars/confidence intervals for each series. |
582 | * This happens before the center lines are drawn, since the center lines | |
583 | * need to be drawn on top of the error bars for all series. | |
01a14b85 DV |
584 | * @private |
585 | */ | |
38e3d209 DV |
586 | DygraphCanvasRenderer._errorPlotter = function(e) { |
587 | var g = e.dygraph; | |
e2d8db3a | 588 | var setName = e.setName; |
38e3d209 DV |
589 | var errorBars = g.getOption("errorBars") || g.getOption("customBars"); |
590 | if (!errorBars) return; | |
591 | ||
e2d8db3a | 592 | var fillGraph = g.getOption("fillGraph", setName); |
38e3d209 DV |
593 | if (fillGraph) { |
594 | g.warn("Can't use fillGraph option with error bars"); | |
595 | } | |
6a6439da | 596 | |
38e3d209 DV |
597 | var ctx = e.drawingContext; |
598 | var color = e.color; | |
599 | var fillAlpha = g.getOption('fillAlpha', setName); | |
0ce5b19b | 600 | var stepPlot = g.getOption("stepPlot", setName); |
38e3d209 | 601 | var points = e.points; |
6a6439da | 602 | |
38e3d209 DV |
603 | var iter = Dygraph.createIterator(points, 0, points.length, |
604 | DygraphCanvasRenderer._getIteratorPredicate( | |
b85358e2 | 605 | g.getOption("connectSeparatedPoints", setName))); |
6a6439da | 606 | |
38e3d209 | 607 | var newYs; |
6a6439da | 608 | |
38e3d209 DV |
609 | // setup graphics context |
610 | var prevX = NaN; | |
611 | var prevY = NaN; | |
612 | var prevYs = [-1, -1]; | |
38e3d209 | 613 | // should be same color as the lines but only 15% opaque. |
a96b8ba3 | 614 | var rgb = new RGBColorParser(color); |
38e3d209 DV |
615 | var err_color = |
616 | 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')'; | |
617 | ctx.fillStyle = err_color; | |
618 | ctx.beginPath(); | |
cf89eeed DV |
619 | |
620 | var isNullUndefinedOrNaN = function(x) { | |
621 | return (x === null || | |
622 | x === undefined || | |
623 | isNaN(x)); | |
624 | }; | |
625 | ||
38e3d209 DV |
626 | while (iter.hasNext) { |
627 | var point = iter.next(); | |
cf89eeed DV |
628 | if ((!stepPlot && isNullUndefinedOrNaN(point.y)) || |
629 | (stepPlot && !isNaN(prevY) && isNullUndefinedOrNaN(prevY))) { | |
38e3d209 DV |
630 | prevX = NaN; |
631 | continue; | |
632 | } | |
6a6439da | 633 | |
38e3d209 DV |
634 | if (stepPlot) { |
635 | newYs = [ point.y_bottom, point.y_top ]; | |
636 | prevY = point.y; | |
637 | } else { | |
638 | newYs = [ point.y_bottom, point.y_top ]; | |
639 | } | |
640 | newYs[0] = e.plotArea.h * newYs[0] + e.plotArea.y; | |
641 | newYs[1] = e.plotArea.h * newYs[1] + e.plotArea.y; | |
642 | if (!isNaN(prevX)) { | |
a5701188 | 643 | if (stepPlot) { |
38e3d209 | 644 | ctx.moveTo(prevX, prevYs[0]); |
82dd90c5 | 645 | ctx.lineTo(point.canvasx, prevYs[0]); |
646 | ctx.lineTo(point.canvasx, prevYs[1]); | |
38e3d209 | 647 | } else { |
82dd90c5 | 648 | ctx.moveTo(prevX, prevYs[0]); |
649 | ctx.lineTo(point.canvasx, newYs[0]); | |
650 | ctx.lineTo(point.canvasx, newYs[1]); | |
6a6439da | 651 | } |
82dd90c5 | 652 | ctx.lineTo(prevX, prevYs[1]); |
38e3d209 | 653 | ctx.closePath(); |
6a6439da | 654 | } |
38e3d209 DV |
655 | prevYs = newYs; |
656 | prevX = point.canvasx; | |
6a6439da | 657 | } |
38e3d209 | 658 | ctx.fill(); |
42a9ebb8 | 659 | }; |
6a6439da | 660 | |
79253bd0 | 661 | /** |
01a14b85 DV |
662 | * Draws the shaded regions when "fillGraph" is set. Not to be confused with |
663 | * error bars. | |
664 | * | |
38e3d209 DV |
665 | * For stacked charts, it's more convenient to handle all the series |
666 | * simultaneously. So this plotter plots all the points on the first series | |
667 | * it's asked to draw, then ignores all the other series. | |
668 | * | |
01a14b85 DV |
669 | * @private |
670 | */ | |
38e3d209 | 671 | DygraphCanvasRenderer._fillPlotter = function(e) { |
3c080cd0 KW |
672 | // Skip if we're drawing a single series for interactive highlight overlay. |
673 | if (e.singleSeriesName) return; | |
674 | ||
38e3d209 DV |
675 | // We'll handle all the series at once, not one-by-one. |
676 | if (e.seriesIndex !== 0) return; | |
677 | ||
e2d8db3a | 678 | var g = e.dygraph; |
38e3d209 | 679 | var setNames = g.getLabels().slice(1); // remove x-axis |
e2d8db3a | 680 | |
38e3d209 DV |
681 | // getLabels() includes names for invisible series, which are not included in |
682 | // allSeriesPoints. We remove those to make the two match. | |
683 | // TODO(danvk): provide a simpler way to get this information. | |
684 | for (var i = setNames.length; i >= 0; i--) { | |
685 | if (!g.visibility()[i]) setNames.splice(i, 1); | |
686 | } | |
687 | ||
e2d8db3a DV |
688 | var anySeriesFilled = (function() { |
689 | for (var i = 0; i < setNames.length; i++) { | |
690 | if (g.getOption("fillGraph", setNames[i])) return true; | |
691 | } | |
692 | return false; | |
693 | })(); | |
694 | ||
695 | if (!anySeriesFilled) return; | |
696 | ||
697 | var ctx = e.drawingContext; | |
698 | var area = e.plotArea; | |
699 | var sets = e.allSeriesPoints; | |
700 | var setCount = sets.length; | |
701 | ||
38e3d209 | 702 | var fillAlpha = g.getOption('fillAlpha'); |
38e3d209 DV |
703 | var stackedGraph = g.getOption("stackedGraph"); |
704 | var colors = g.getColors(); | |
01a14b85 | 705 | |
30a5cfc6 KW |
706 | // For stacked graphs, track the baseline for filling. |
707 | // | |
708 | // The filled areas below graph lines are trapezoids with two | |
709 | // vertical edges. The top edge is the line segment being drawn, and | |
710 | // the baseline is the bottom edge. Each baseline corresponds to the | |
711 | // top line segment from the previous stacked line. In the case of | |
712 | // step plots, the trapezoids are rectangles. | |
713 | var baseline = {}; | |
01a14b85 | 714 | var currBaseline; |
104d87c5 | 715 | var prevStepPlot; // for different line drawing modes (line/step) per series |
01a14b85 DV |
716 | |
717 | // process sets in reverse order (needed for stacked graphs) | |
9e85a8f4 DV |
718 | for (var setIdx = setCount - 1; setIdx >= 0; setIdx--) { |
719 | var setName = setNames[setIdx]; | |
e2d8db3a | 720 | if (!g.getOption('fillGraph', setName)) continue; |
104d87c5 | 721 | |
722 | var stepPlot = g.getOption('stepPlot', setName); | |
38e3d209 DV |
723 | var color = colors[setIdx]; |
724 | var axis = g.axisPropertiesForSeries(setName); | |
01a14b85 DV |
725 | var axisY = 1.0 + axis.minyval * axis.yscale; |
726 | if (axisY < 0.0) axisY = 0.0; | |
727 | else if (axisY > 1.0) axisY = 1.0; | |
38e3d209 | 728 | axisY = area.h * axisY + area.y; |
01a14b85 | 729 | |
38e3d209 | 730 | var points = sets[setIdx]; |
9e85a8f4 | 731 | var iter = Dygraph.createIterator(points, 0, points.length, |
01a14b85 | 732 | DygraphCanvasRenderer._getIteratorPredicate( |
b85358e2 | 733 | g.getOption("connectSeparatedPoints", setName))); |
01a14b85 DV |
734 | |
735 | // setup graphics context | |
736 | var prevX = NaN; | |
737 | var prevYs = [-1, -1]; | |
738 | var newYs; | |
01a14b85 | 739 | // should be same color as the lines but only 15% opaque. |
a96b8ba3 | 740 | var rgb = new RGBColorParser(color); |
01a14b85 DV |
741 | var err_color = |
742 | 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')'; | |
743 | ctx.fillStyle = err_color; | |
744 | ctx.beginPath(); | |
12b879f4 DV |
745 | var last_x, is_first = true; |
746 | while (iter.hasNext) { | |
01a14b85 | 747 | var point = iter.next(); |
16febe6b DV |
748 | if (!Dygraph.isOK(point.y)) { |
749 | prevX = NaN; | |
30a5cfc6 KW |
750 | if (point.y_stacked !== null && !isNaN(point.y_stacked)) { |
751 | baseline[point.canvasx] = area.h * point.y_stacked + area.y; | |
752 | } | |
16febe6b DV |
753 | continue; |
754 | } | |
755 | if (stackedGraph) { | |
12b879f4 DV |
756 | if (!is_first && last_x == point.xval) { |
757 | continue; | |
758 | } else { | |
759 | is_first = false; | |
760 | last_x = point.xval; | |
761 | } | |
762 | ||
16febe6b DV |
763 | currBaseline = baseline[point.canvasx]; |
764 | var lastY; | |
765 | if (currBaseline === undefined) { | |
766 | lastY = axisY; | |
767 | } else { | |
104d87c5 | 768 | if(prevStepPlot) { |
16febe6b | 769 | lastY = currBaseline[0]; |
01a14b85 | 770 | } else { |
16febe6b | 771 | lastY = currBaseline; |
01a14b85 | 772 | } |
16febe6b DV |
773 | } |
774 | newYs = [ point.canvasy, lastY ]; | |
01a14b85 | 775 | |
16febe6b DV |
776 | if(stepPlot) { |
777 | // Step plots must keep track of the top and bottom of | |
778 | // the baseline at each point. | |
779 | if(prevYs[0] === -1) { | |
780 | baseline[point.canvasx] = [ point.canvasy, axisY ]; | |
01a14b85 | 781 | } else { |
16febe6b | 782 | baseline[point.canvasx] = [ point.canvasy, prevYs[0] ]; |
01a14b85 | 783 | } |
01a14b85 | 784 | } else { |
16febe6b | 785 | baseline[point.canvasx] = point.canvasy; |
01a14b85 | 786 | } |
01a14b85 | 787 | |
16febe6b DV |
788 | } else { |
789 | newYs = [ point.canvasy, axisY ]; | |
790 | } | |
791 | if (!isNaN(prevX)) { | |
792 | ctx.moveTo(prevX, prevYs[0]); | |
104d87c5 | 793 | |
794 | // Move to top fill point | |
16febe6b DV |
795 | if (stepPlot) { |
796 | ctx.lineTo(point.canvasx, prevYs[0]); | |
16febe6b DV |
797 | } else { |
798 | ctx.lineTo(point.canvasx, newYs[0]); | |
104d87c5 | 799 | } |
800 | // Move to bottom fill point | |
801 | if (prevStepPlot && currBaseline) { | |
802 | // Draw to the bottom of the baseline | |
803 | ctx.lineTo(point.canvasx, currBaseline[1]); | |
804 | } else { | |
16febe6b | 805 | ctx.lineTo(point.canvasx, newYs[1]); |
01a14b85 | 806 | } |
16febe6b DV |
807 | |
808 | ctx.lineTo(prevX, prevYs[1]); | |
809 | ctx.closePath(); | |
01a14b85 | 810 | } |
16febe6b DV |
811 | prevYs = newYs; |
812 | prevX = point.canvasx; | |
01a14b85 | 813 | } |
104d87c5 | 814 | prevStepPlot = stepPlot; |
01a14b85 DV |
815 | ctx.fill(); |
816 | } | |
817 | }; |