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