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