Remove isAndroid checks (#784)
[dygraphs.git] / src / dygraph-canvas.js
index aed387b..7377239 100644 (file)
  * @constructor
  */
 
-var DygraphCanvasRenderer = (function() {
 /*global Dygraph:false */
 "use strict";
 
+import * as utils from './dygraph-utils';
+import Dygraph from './dygraph';
+
 
 /**
  * @constructor
@@ -57,7 +59,7 @@ var DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) {
   this.width = dygraph.width_;
 
   // --- check whether everything is ok before we return
-  if (!Dygraph.isCanvasSupported(this.element)) {
+  if (!utils.isCanvasSupported(this.element)) {
     throw "Canvas is not supported.";
   }
 
@@ -66,19 +68,15 @@ var DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) {
 
   // Set up a clipping area for the canvas (and the interaction canvas).
   // This ensures that we don't overdraw.
-  // on Android 3 and 4, setting a clipping area on a canvas prevents it from
-  // displaying anything.
-  if (!Dygraph.isAndroid()) {
-    var ctx = this.dygraph_.canvas_ctx_;
-    ctx.beginPath();
-    ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
-    ctx.clip();
+  var ctx = this.dygraph_.canvas_ctx_;
+  ctx.beginPath();
+  ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
+  ctx.clip();
 
-    ctx = this.dygraph_.hidden_ctx_;
-    ctx.beginPath();
-    ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
-    ctx.clip();
-  }
+  ctx = this.dygraph_.hidden_ctx_;
+  ctx.beginPath();
+  ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
+  ctx.clip();
 };
 
 /**
@@ -135,7 +133,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e,
   // TODO(konigsberg): Compute attributes outside this method call.
   var stepPlot = g.getBooleanOption("stepPlot", e.setName);
 
-  if (!Dygraph.isArrayLike(strokePattern)) {
+  if (!utils.isArrayLike(strokePattern)) {
     strokePattern = null;
   }
 
@@ -143,7 +141,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e,
 
   var points = e.points;
   var setName = e.setName;
-  var iter = Dygraph.createIterator(points, 0, points.length,
+  var iter = utils.createIterator(points, 0, points.length,
       DygraphCanvasRenderer._getIteratorPredicate(
           g.getBooleanOption("connectSeparatedPoints", setName)));
 
@@ -152,7 +150,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e,
   var ctx = e.drawingContext;
   ctx.save();
   if (stroking) {
-    ctx.installPattern(strokePattern);
+    if (ctx.setLineDash) ctx.setLineDash(strokePattern);
   }
 
   var pointsOnLine = DygraphCanvasRenderer._drawSeries(
@@ -161,7 +159,7 @@ DygraphCanvasRenderer._drawStyledLine = function(e,
       e, pointsOnLine, drawPointCallback, color, pointSize);
 
   if (stroking) {
-    ctx.uninstallPattern();
+    if (ctx.setLineDash) ctx.setLineDash([]);
   }
 
   ctx.restore();
@@ -218,18 +216,18 @@ DygraphCanvasRenderer._drawSeries = function(e,
       prevCanvasX = prevCanvasY = null;
     } else {
       isIsolated = false;
-      if (drawGapPoints || !prevCanvasX) {
+      if (drawGapPoints || prevCanvasX === null) {
         iter.nextIdx_ = i;
         iter.next();
         nextCanvasY = iter.hasNext ? iter.peek.canvasy : null;
 
         var isNextCanvasYNullOrNaN = nextCanvasY === null ||
             nextCanvasY != nextCanvasY;
-        isIsolated = (!prevCanvasX && isNextCanvasYNullOrNaN);
+        isIsolated = (prevCanvasX === null && isNextCanvasYNullOrNaN);
         if (drawGapPoints) {
           // Also consider a point to be "isolated" if it's adjacent to a
           // null point, excluding the graph edges.
-          if ((!first && !prevCanvasX) ||
+          if ((!first && prevCanvasX === null) ||
               (iter.hasNext && isNextCanvasYNullOrNaN)) {
             isIsolated = true;
           }
@@ -334,7 +332,7 @@ DygraphCanvasRenderer.prototype._renderLineChart = function(opt_seriesName, opt_
   // Determine which series have specialized plotters.
   var plotter_attr = this.dygraph_.getOption("plotter");
   var plotters = plotter_attr;
-  if (!Dygraph.isArrayLike(plotters)) {
+  if (!utils.isArrayLike(plotters)) {
     plotters = [plotters];
   }
 
@@ -425,7 +423,7 @@ DygraphCanvasRenderer._linePlotter = function(e) {
   // this code a bit nasty.
   var borderWidth = g.getNumericOption("strokeBorderWidth", setName);
   var drawPointCallback = g.getOption("drawPointCallback", setName) ||
-      Dygraph.Circles.DEFAULT;
+      utils.Circles.DEFAULT;
   var strokePattern = g.getOption("strokePattern", setName);
   var drawPoints = g.getBooleanOption("drawPoints", setName);
   var pointSize = g.getNumericOption("pointSize", setName);
@@ -475,7 +473,7 @@ DygraphCanvasRenderer._errorPlotter = function(e) {
   var stepPlot = g.getBooleanOption("stepPlot", setName);
   var points = e.points;
 
-  var iter = Dygraph.createIterator(points, 0, points.length,
+  var iter = utils.createIterator(points, 0, points.length,
       DygraphCanvasRenderer._getIteratorPredicate(
           g.getBooleanOption("connectSeparatedPoints", setName)));
 
@@ -486,7 +484,7 @@ DygraphCanvasRenderer._errorPlotter = function(e) {
   var prevY = NaN;
   var prevYs = [-1, -1];
   // should be same color as the lines but only 15% opaque.
-  var rgb = Dygraph.toRGB_(color);
+  var rgb = utils.toRGB_(color);
   var err_color =
       'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')';
   ctx.fillStyle = err_color;
@@ -542,7 +540,7 @@ DygraphCanvasRenderer._errorPlotter = function(e) {
  * Proxy for CanvasRenderingContext2D which drops moveTo/lineTo calls which are
  * superfluous. It accumulates all movements which haven't changed the x-value
  * and only applies the two with the most extreme y-values.
- * 
+ *
  * Calls to lineTo/moveTo must have non-decreasing x-values.
  */
 DygraphCanvasRenderer._fastCanvasProxy = function(context) {
@@ -748,7 +746,7 @@ DygraphCanvasRenderer._fillPlotter = function(e) {
     axisY = area.h * axisY + area.y;
 
     var points = sets[setIdx];
-    var iter = Dygraph.createIterator(points, 0, points.length,
+    var iter = utils.createIterator(points, 0, points.length,
         DygraphCanvasRenderer._getIteratorPredicate(
             g.getBooleanOption("connectSeparatedPoints", setName)));
 
@@ -757,7 +755,7 @@ DygraphCanvasRenderer._fillPlotter = function(e) {
     var prevYs = [-1, -1];
     var newYs;
     // should be same color as the lines but only 15% opaque.
-    var rgb = Dygraph.toRGB_(color);
+    var rgb = utils.toRGB_(color);
     var err_color =
         'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')';
     ctx.fillStyle = err_color;
@@ -782,7 +780,7 @@ DygraphCanvasRenderer._fillPlotter = function(e) {
     var point;
     while (iter.hasNext) {
       point = iter.next();
-      if (!Dygraph.isOK(point.y) && !stepPlot) {
+      if (!utils.isOK(point.y) && !stepPlot) {
         traceBackPath(ctx, prevX, prevYs[1], pathBack);
         pathBack = [];
         prevX = NaN;
@@ -866,6 +864,4 @@ DygraphCanvasRenderer._fillPlotter = function(e) {
   }
 };
 
-return DygraphCanvasRenderer;
-
-})();
+export default DygraphCanvasRenderer;