remove if statement
[dygraphs.git] / dygraph-canvas.js
index c3bf8bc..efae1cf 100644 (file)
@@ -257,22 +257,18 @@ DygraphCanvasRenderer.prototype._drawStyledLine = function(
   }
   var drawGapPoints = this.dygraph_.attr_('drawGapEdgePoints', setName);
 
-  ctx.save();
-
   var iter = Dygraph.createIterator(points, firstIndexInSet, setLength,
       DygraphCanvasRenderer._getIteratorPredicate(
           this.attr_("connectSeparatedPoints")));
 
   var stroking = strokePattern && (strokePattern.length >= 2);
 
-  var pointsOnLine;
-  var strategy;
+  ctx.save();
   if (stroking) {
     ctx.installPattern(strokePattern);
   }
 
-  strategy = trivialStrategy(ctx, color, strokeWidth);
-  pointsOnLine = this._drawSeries(ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, strategy);
+  var pointsOnLine = this._drawSeries(ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints, stepPlot, color);
   this._drawPointsOnLine(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize);
 
   if (stroking) {
@@ -282,29 +278,6 @@ DygraphCanvasRenderer.prototype._drawStyledLine = function(
   ctx.restore();
 };
 
-var trivialStrategy = function(ctx, color, strokeWidth) {
-  return new function() {
-    this.init = function() {
-      ctx.beginPath();
-      ctx.strokeStyle = color;
-      ctx.lineWidth = strokeWidth;
-    };
-    this.finish = function() {
-      ctx.stroke(); // should this include closePath?
-    };
-    this.startSegment = function() { };
-    this.endSegment = function() { };
-    this.drawLine = function(x1, y1, x2, y2) {
-      ctx.moveTo(x1, y1);
-      ctx.lineTo(x2, y2);
-    };
-    // don't skip pixels.
-    this.skipPixel = function() {
-      return false;
-    };
-  };
-};
-
 DygraphCanvasRenderer.prototype._drawPointsOnLine = function(ctx, pointsOnLine, drawPointCallback, setName, color, pointSize) {
   for (var idx = 0; idx < pointsOnLine.length; idx++) {
     var cb = pointsOnLine[idx];
@@ -317,7 +290,7 @@ DygraphCanvasRenderer.prototype._drawPointsOnLine = function(ctx, pointsOnLine,
 
 DygraphCanvasRenderer.prototype._drawSeries = function(
     ctx, iter, strokeWidth, pointSize, drawPoints, drawGapPoints,
-    stepPlot, strategy) {
+    stepPlot, color) {
 
   var prevCanvasX = null;
   var prevCanvasY = null;
@@ -327,16 +300,17 @@ DygraphCanvasRenderer.prototype._drawSeries = function(
   var pointsOnLine = []; // Array of [canvasx, canvasy] pairs.
   var first = true; // the first cycle through the while loop
 
-  strategy.init();
+  ctx.beginPath();
+  ctx.strokeStyle = color;
+  ctx.lineWidth = strokeWidth;
 
-  while(iter.hasNext) {
+  while (iter.hasNext) {
     point = iter.next();
     if (point.canvasy === null || point.canvasy != point.canvasy) {
       if (stepPlot && prevCanvasX !== null) {
         // Draw a horizontal line to the start of the missing data
-        strategy.startSegment();
-        strategy.drawLine(prevX, prevY, point.canvasx, prevY);
-        strategy.endSegment();
+        ctx.moveTo(prevX, prevY);
+        ctx.lineTo(point.canvasx, prevY);
       }
       prevCanvasX = prevCanvasY = null;
     } else {
@@ -354,17 +328,14 @@ DygraphCanvasRenderer.prototype._drawSeries = function(
         }
       }
       if (prevCanvasX !== null) {
-        if (strategy.skipPixel(prevCanvasX, prevCanvasY, point.canvasx, point.canvasy)) {
-          continue;
-        }
         if (strokeWidth) {
-          strategy.startSegment();
           if (stepPlot) {
-            strategy.drawLine(prevCanvasX, prevCanvasY, point.canvasx, prevCanvasY);
+            ctx.moveTo(prevCanvasX, prevCanvasY);
+            ctx.lineTo(point.canvasx, prevCanvasY);
             prevCanvasX = point.canvasx;
           }
-          strategy.drawLine(prevCanvasX, prevCanvasY, point.canvasx, point.canvasy);      
-          strategy.endSegment();
+          ctx.moveTo(prevCanvasX, prevCanvasY);
+          ctx.lineTo(point.canvasx, point.canvasy);
         }
       }
       if (drawPoints || isIsolated) {
@@ -375,7 +346,7 @@ DygraphCanvasRenderer.prototype._drawSeries = function(
     }
     first = false;
   }
-  strategy.finish();
+  ctx.stroke();
   return pointsOnLine;
 };
 
@@ -583,62 +554,60 @@ DygraphCanvasRenderer.prototype.drawFillBars_ = function(points) {
     ctx.beginPath();
     while(iter.hasNext) {
       var point = iter.next();
-      if (point.name == setName) { // TODO(klausw): this is always true
-        if (!Dygraph.isOK(point.y)) {
-          prevX = NaN;
-          continue;
-        }
-        if (stackedGraph) {
-          currBaseline = baseline[point.canvasx];
-          var lastY;
-          if (currBaseline === undefined) {
-            lastY = axisY;
+      if (!Dygraph.isOK(point.y)) {
+        prevX = NaN;
+        continue;
+      }
+      if (stackedGraph) {
+        currBaseline = baseline[point.canvasx];
+        var lastY;
+        if (currBaseline === undefined) {
+          lastY = axisY;
+        } else {
+          if(stepPlot) {
+            lastY = currBaseline[0];
           } else {
-            if(stepPlot) {
-              lastY = currBaseline[0];
-            } else {
-              lastY = currBaseline;
-            }
+            lastY = currBaseline;
           }
-          newYs = [ point.canvasy, lastY ];
+        }
+        newYs = [ point.canvasy, lastY ];
 
-          if(stepPlot) {
-            // Step plots must keep track of the top and bottom of
-            // the baseline at each point.
-            if(prevYs[0] === -1) {
-              baseline[point.canvasx] = [ point.canvasy, axisY ];
-            } else {
-              baseline[point.canvasx] = [ point.canvasy, prevYs[0] ];
-            }
+        if(stepPlot) {
+          // Step plots must keep track of the top and bottom of
+          // the baseline at each point.
+          if(prevYs[0] === -1) {
+            baseline[point.canvasx] = [ point.canvasy, axisY ];
           } else {
-            baseline[point.canvasx] = point.canvasy;
+            baseline[point.canvasx] = [ point.canvasy, prevYs[0] ];
           }
-
         } else {
-          newYs = [ point.canvasy, axisY ];
+          baseline[point.canvasx] = point.canvasy;
         }
-        if (!isNaN(prevX)) {
-          ctx.moveTo(prevX, prevYs[0]);
 
-          if (stepPlot) {
-            ctx.lineTo(point.canvasx, prevYs[0]);
-            if(currBaseline) {
-              // Draw to the bottom of the baseline
-              ctx.lineTo(point.canvasx, currBaseline[1]);
-            } else {
-              ctx.lineTo(point.canvasx, newYs[1]);
-            }
+      } else {
+        newYs = [ point.canvasy, axisY ];
+      }
+      if (!isNaN(prevX)) {
+        ctx.moveTo(prevX, prevYs[0]);
+
+        if (stepPlot) {
+          ctx.lineTo(point.canvasx, prevYs[0]);
+          if(currBaseline) {
+            // Draw to the bottom of the baseline
+            ctx.lineTo(point.canvasx, currBaseline[1]);
           } else {
-            ctx.lineTo(point.canvasx, newYs[0]);
             ctx.lineTo(point.canvasx, newYs[1]);
           }
-
-          ctx.lineTo(prevX, prevYs[1]);
-          ctx.closePath();
+        } else {
+          ctx.lineTo(point.canvasx, newYs[0]);
+          ctx.lineTo(point.canvasx, newYs[1]);
         }
-        prevYs = newYs;
-        prevX = point.canvasx;
+
+        ctx.lineTo(prevX, prevYs[1]);
+        ctx.closePath();
       }
+      prevYs = newYs;
+      prevX = point.canvasx;
     }
     ctx.fill();
   }