clean up
[dygraphs.git] / dygraph-canvas.js
CommitLineData
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 49var 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
90DygraphCanvasRenderer.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 100DygraphCanvasRenderer.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
127DygraphCanvasRenderer.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 154DygraphCanvasRenderer.prototype.render = function() {
2ce09b19 155 this._renderLineChart();
fbe31dc8
DV
156};
157
920208fb
PF
158DygraphCanvasRenderer.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 234DygraphCanvasRenderer._getIteratorPredicate = function(connectSeparatedPoints) {
9f6db80e
DV
235 return connectSeparatedPoints
236 ? DygraphCanvasRenderer._predicateThatSkipsEmptyPoints
237 : null;
0f20de1c 238};
8722284b
RK
239
240DygraphCanvasRenderer._predicateThatSkipsEmptyPoints =
0f20de1c
DV
241 function(array, idx) {
242 return array[idx].yval !== null;
243};
04c104d7 244
9f6db80e
DV
245/**
246 *
247 * @private
248 */
857a6931 249DygraphCanvasRenderer.prototype._drawStyledLine = function(
5469113b
KW
250 ctx, i, setName, color, strokeWidth, strokePattern, drawPoints,
251 drawPointCallback, pointSize) {
99a77a04 252 // TODO(konigsberg): Compute attributes outside this method call.
857a6931
KW
253 var stepPlot = this.attr_("stepPlot");
254 var firstIndexInSet = this.layout.setPointsOffsets[i];
255 var setLength = this.layout.setPointsLengths[i];
857a6931 256 var points = this.layout.points;
857a6931
KW
257 if (!Dygraph.isArrayLike(strokePattern)) {
258 strokePattern = null;
259 }
a5a50727 260 var drawGapPoints = this.dygraph_.attr_('drawGapEdgePoints', setName);
857a6931 261
7d1afbb9 262 var iter = Dygraph.createIterator(points, firstIndexInSet, setLength,
9f6db80e
DV
263 DygraphCanvasRenderer._getIteratorPredicate(
264 this.attr_("connectSeparatedPoints")));
7d1afbb9 265
fb63bf1b
DV
266 var stroking = strokePattern && (strokePattern.length >= 2);
267
0140347d 268 ctx.save();
fb63bf1b
DV
269 if (stroking) {
270 ctx.installPattern(strokePattern);
b843b52c 271 }
fb63bf1b 272
0140347d 273 var pointsOnLine = this._drawSeries(ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, color);
31f8e58b
RK
274 this._drawPointsOnLine(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize);
275
fb63bf1b
DV
276 if (stroking) {
277 ctx.uninstallPattern();
278 }
b843b52c 279
fb63bf1b 280 ctx.restore();
31f8e58b
RK
281};
282
a401ca8a
RK
283DygraphCanvasRenderer.prototype._drawPointsOnLine = function(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize) {
284 for (var idx = 0; idx < pointsOnLine.length; idx++) {
285 var cb = pointsOnLine[idx];
286 ctx.save();
287 drawPointCallback(
288 this.dygraph_, setName, ctx, cb[0], cb[1], color, pointSize);
289 ctx.restore();
290 }
291}
292
31f8e58b
RK
293DygraphCanvasRenderer.prototype._drawSeries = function(
294 ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints,
0140347d 295 stepPlot, color) {
31f8e58b 296
31f8e58b
RK
297 var prevCanvasX = null;
298 var prevCanvasY = null;
299 var nextCanvasY = null;
300 var isIsolated; // true if this point is isolated (no line segments)
301 var point; // the point being processed in the while loop
b843b52c 302 var pointsOnLine = []; // Array of [canvasx, canvasy] pairs.
31f8e58b
RK
303 var first = true; // the first cycle through the while loop
304
0140347d
DV
305 ctx.beginPath();
306 ctx.strokeStyle = color;
307 ctx.lineWidth = strokeWidth;
31f8e58b 308
0f20de1c 309 for (var i = iter.start_; i < iter.end_; i++) {
0f20de1c
DV
310 point = iter.array_[i];
311 if (iter.predicate_) {
312 while (i < iter.end_ && !iter.predicate_(iter.array_, i)) {
313 i++;
314 }
315 if (i == iter.end_) break;
537fe03a 316 point = iter.array_[i];
0f20de1c
DV
317 }
318
a02978e2 319 if (point.canvasy === null || point.canvasy != point.canvasy) {
31f8e58b 320 if (stepPlot && prevCanvasX !== null) {
857a6931 321 // Draw a horizontal line to the start of the missing data
0140347d
DV
322 ctx.moveTo(prevX, prevY);
323 ctx.lineTo(point.canvasx, prevY);
857a6931 324 }
31f8e58b 325 prevCanvasX = prevCanvasY = null;
857a6931 326 } else {
0f20de1c
DV
327 isIsolated = false;
328 if (drawGapPoints || !prevCanvasX) {
0f20de1c
DV
329 iter.nextIdx_ = i;
330 var peek = iter.next();
82f9b10f 331 nextCanvasY = iter.hasNext ? iter.peek.canvasy : null;
0f20de1c 332
0f20de1c
DV
333 var isNextCanvasYNullOrNaN = nextCanvasY === null ||
334 nextCanvasY != nextCanvasY;
335 isIsolated = (!prevCanvasX && isNextCanvasYNullOrNaN);
336 if (drawGapPoints) {
337 // Also consider a point to be "isolated" if it's adjacent to a
338 // null point, excluding the graph edges.
339 if ((!first && !prevCanvasX) ||
340 (iter.hasNext && isNextCanvasYNullOrNaN)) {
341 isIsolated = true;
342 }
19b84fe7
KW
343 }
344 }
0f20de1c 345
31f8e58b 346 if (prevCanvasX !== null) {
857a6931 347 if (strokeWidth) {
857a6931 348 if (stepPlot) {
0140347d
DV
349 ctx.moveTo(prevCanvasX, prevCanvasY);
350 ctx.lineTo(point.canvasx, prevCanvasY);
31f8e58b 351 prevCanvasX = point.canvasx;
857a6931 352 }
0140347d
DV
353 ctx.moveTo(prevCanvasX, prevCanvasY);
354 ctx.lineTo(point.canvasx, point.canvasy);
b843b52c
RK
355 }
356 }
b843b52c
RK
357 if (drawPoints || isIsolated) {
358 pointsOnLine.push([point.canvasx, point.canvasy]);
359 }
31f8e58b
RK
360 prevCanvasX = point.canvasx;
361 prevCanvasY = point.canvasy;
b843b52c 362 }
7d1afbb9 363 first = false;
b843b52c 364 }
0140347d 365 ctx.stroke();
31f8e58b 366 return pointsOnLine;
857a6931
KW
367};
368
369DygraphCanvasRenderer.prototype._drawLine = function(ctx, i) {
370 var setNames = this.layout.setNames;
371 var setName = setNames[i];
372
373 var strokeWidth = this.dygraph_.attr_("strokeWidth", setName);
374 var borderWidth = this.dygraph_.attr_("strokeBorderWidth", setName);
5469113b
KW
375 var drawPointCallback = this.dygraph_.attr_("drawPointCallback", setName) ||
376 Dygraph.Circles.DEFAULT;
99a77a04 377
857a6931 378 if (borderWidth && strokeWidth) {
5469113b 379 this._drawStyledLine(ctx, i, setName,
857a6931
KW
380 this.dygraph_.attr_("strokeBorderColor", setName),
381 strokeWidth + 2 * borderWidth,
382 this.dygraph_.attr_("strokePattern", setName),
383 this.dygraph_.attr_("drawPoints", setName),
5469113b 384 drawPointCallback,
857a6931
KW
385 this.dygraph_.attr_("pointSize", setName));
386 }
387
5469113b 388 this._drawStyledLine(ctx, i, setName,
857a6931
KW
389 this.colors[setName],
390 strokeWidth,
391 this.dygraph_.attr_("strokePattern", setName),
392 this.dygraph_.attr_("drawPoints", setName),
5469113b 393 drawPointCallback,
857a6931
KW
394 this.dygraph_.attr_("pointSize", setName));
395};
ce49c2fa 396
6a1aa64f 397/**
758a629f 398 * Actually draw the lines chart, including error bars.
758a629f 399 * @private
6a1aa64f 400 */
285a6bda 401DygraphCanvasRenderer.prototype._renderLineChart = function() {
857a6931 402 var ctx = this.elementContext;
e4182459 403 var errorBars = this.attr_("errorBars") || this.attr_("customBars");
44c6bc29 404 var fillGraph = this.attr_("fillGraph");
01a14b85 405 var i;
21d3323f 406
82c6fe4d 407 var setNames = this.layout.setNames;
21d3323f 408 var setCount = setNames.length;
6a1aa64f 409
ee53deb9 410 this.colors = this.dygraph_.colorsMap_;
f032c51d 411
ff00d3e2
DV
412 // Update Points
413 // TODO(danvk): here
b843b52c
RK
414 //
415 // TODO(bhs): this loop is a hot-spot for high-point-count charts. These
416 // transformations can be pushed into the canvas via linear transformation
417 // matrices.
01a14b85
DV
418 var points = this.layout.points;
419 for (i = points.length; i--;) {
420 var point = points[i];
6a1aa64f
DV
421 point.canvasx = this.area.w * point.x + this.area.x;
422 point.canvasy = this.area.h * point.y + this.area.y;
423 }
6a1aa64f 424
01a14b85
DV
425 // Draw any "fills", i.e. error bars or the filled area under a series.
426 // These must all be drawn before any lines, so that the main lines of a
427 // series are drawn on top.
80aaae18 428 if (errorBars) {
6a834bbb
DV
429 if (fillGraph) {
430 this.dygraph_.warn("Can't use fillGraph option with error bars");
431 }
432
01a14b85 433 ctx.save();
6a6439da 434 this.drawErrorBars_(points);
857a6931 435 ctx.restore();
5954ef32 436 } else if (fillGraph) {
857a6931 437 ctx.save();
01a14b85 438 this.drawFillBars_(points);
857a6931 439 ctx.restore();
80aaae18
DV
440 }
441
f9414b11 442 // Drawing the lines.
758a629f 443 for (i = 0; i < setCount; i += 1) {
857a6931 444 this._drawLine(ctx, i);
80aaae18 445 }
6a1aa64f 446};
79253bd0 447
01a14b85
DV
448/**
449 * Draws the shaded error bars/confidence intervals for each series.
450 * This happens before the center lines are drawn, since the center lines
451 * need to be drawn on top of the error bars for all series.
452 *
453 * @private
454 */
6a6439da
DV
455DygraphCanvasRenderer.prototype.drawErrorBars_ = function(points) {
456 var ctx = this.elementContext;
457 var setNames = this.layout.setNames;
458 var setCount = setNames.length;
459 var fillAlpha = this.attr_('fillAlpha');
01a14b85 460 var stepPlot = this.attr_('stepPlot');
6a6439da
DV
461
462 var newYs;
463
464 for (var i = 0; i < setCount; i++) {
465 var setName = setNames[i];
466 var axis = this.dygraph_.axisPropertiesForSeries(setName);
467 var color = this.colors[setName];
468
469 var firstIndexInSet = this.layout.setPointsOffsets[i];
470 var setLength = this.layout.setPointsLengths[i];
471
472 var iter = Dygraph.createIterator(points, firstIndexInSet, setLength,
01a14b85
DV
473 DygraphCanvasRenderer._getIteratorPredicate(
474 this.attr_("connectSeparatedPoints")));
6a6439da
DV
475
476 // setup graphics context
477 var prevX = NaN;
478 var prevY = NaN;
479 var prevYs = [-1, -1];
480 var yscale = axis.yscale;
481 // should be same color as the lines but only 15% opaque.
482 var rgb = new RGBColor(color);
483 var err_color =
484 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')';
485 ctx.fillStyle = err_color;
486 ctx.beginPath();
487 while (iter.hasNext) {
488 var point = iter.next();
a5701188
DV
489 if (!Dygraph.isOK(point.y)) {
490 prevX = NaN;
491 continue;
492 }
6a6439da 493
a5701188
DV
494 // TODO(danvk): here
495 if (stepPlot) {
496 newYs = [ point.y_bottom, point.y_top ];
497 prevY = point.y;
498 } else {
499 newYs = [ point.y_bottom, point.y_top ];
500 }
501 newYs[0] = this.area.h * newYs[0] + this.area.y;
502 newYs[1] = this.area.h * newYs[1] + this.area.y;
503 if (!isNaN(prevX)) {
6a6439da 504 if (stepPlot) {
a5701188 505 ctx.moveTo(prevX, newYs[0]);
6a6439da 506 } else {
a5701188 507 ctx.moveTo(prevX, prevYs[0]);
6a6439da 508 }
a5701188
DV
509 ctx.lineTo(point.canvasx, newYs[0]);
510 ctx.lineTo(point.canvasx, newYs[1]);
511 if (stepPlot) {
512 ctx.lineTo(prevX, newYs[1]);
513 } else {
514 ctx.lineTo(prevX, prevYs[1]);
6a6439da 515 }
a5701188 516 ctx.closePath();
6a6439da 517 }
a5701188
DV
518 prevYs = newYs;
519 prevX = point.canvasx;
6a6439da
DV
520 }
521 ctx.fill();
522 }
523};
524
79253bd0 525/**
01a14b85
DV
526 * Draws the shaded regions when "fillGraph" is set. Not to be confused with
527 * error bars.
528 *
529 * @private
530 */
531DygraphCanvasRenderer.prototype.drawFillBars_ = function(points) {
532 var ctx = this.elementContext;
533 var setNames = this.layout.setNames;
534 var setCount = setNames.length;
535 var fillAlpha = this.attr_('fillAlpha');
536 var stepPlot = this.attr_('stepPlot');
537 var stackedGraph = this.attr_("stackedGraph");
538
539 var baseline = {}; // for stacked graphs: baseline for filling
540 var currBaseline;
541
542 // process sets in reverse order (needed for stacked graphs)
543 for (var i = setCount - 1; i >= 0; i--) {
544 var setName = setNames[i];
545 var color = this.colors[setName];
546 var axis = this.dygraph_.axisPropertiesForSeries(setName);
547 var axisY = 1.0 + axis.minyval * axis.yscale;
548 if (axisY < 0.0) axisY = 0.0;
549 else if (axisY > 1.0) axisY = 1.0;
550 axisY = this.area.h * axisY + this.area.y;
551 var firstIndexInSet = this.layout.setPointsOffsets[i];
552 var setLength = this.layout.setPointsLengths[i];
553
554 var iter = Dygraph.createIterator(points, firstIndexInSet, setLength,
555 DygraphCanvasRenderer._getIteratorPredicate(
556 this.attr_("connectSeparatedPoints")));
557
558 // setup graphics context
559 var prevX = NaN;
560 var prevYs = [-1, -1];
561 var newYs;
562 var yscale = axis.yscale;
563 // should be same color as the lines but only 15% opaque.
564 var rgb = new RGBColor(color);
565 var err_color =
566 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')';
567 ctx.fillStyle = err_color;
568 ctx.beginPath();
569 while(iter.hasNext) {
570 var point = iter.next();
16febe6b
DV
571 if (!Dygraph.isOK(point.y)) {
572 prevX = NaN;
573 continue;
574 }
575 if (stackedGraph) {
576 currBaseline = baseline[point.canvasx];
577 var lastY;
578 if (currBaseline === undefined) {
579 lastY = axisY;
580 } else {
581 if(stepPlot) {
582 lastY = currBaseline[0];
01a14b85 583 } else {
16febe6b 584 lastY = currBaseline;
01a14b85 585 }
16febe6b
DV
586 }
587 newYs = [ point.canvasy, lastY ];
01a14b85 588
16febe6b
DV
589 if(stepPlot) {
590 // Step plots must keep track of the top and bottom of
591 // the baseline at each point.
592 if(prevYs[0] === -1) {
593 baseline[point.canvasx] = [ point.canvasy, axisY ];
01a14b85 594 } else {
16febe6b 595 baseline[point.canvasx] = [ point.canvasy, prevYs[0] ];
01a14b85 596 }
01a14b85 597 } else {
16febe6b 598 baseline[point.canvasx] = point.canvasy;
01a14b85 599 }
01a14b85 600
16febe6b
DV
601 } else {
602 newYs = [ point.canvasy, axisY ];
603 }
604 if (!isNaN(prevX)) {
605 ctx.moveTo(prevX, prevYs[0]);
606
607 if (stepPlot) {
608 ctx.lineTo(point.canvasx, prevYs[0]);
609 if(currBaseline) {
610 // Draw to the bottom of the baseline
611 ctx.lineTo(point.canvasx, currBaseline[1]);
01a14b85 612 } else {
01a14b85
DV
613 ctx.lineTo(point.canvasx, newYs[1]);
614 }
16febe6b
DV
615 } else {
616 ctx.lineTo(point.canvasx, newYs[0]);
617 ctx.lineTo(point.canvasx, newYs[1]);
01a14b85 618 }
16febe6b
DV
619
620 ctx.lineTo(prevX, prevYs[1]);
621 ctx.closePath();
01a14b85 622 }
16febe6b
DV
623 prevYs = newYs;
624 prevX = point.canvasx;
01a14b85
DV
625 }
626 ctx.fill();
627 }
628};