Fix issue 131: setVisibility() broken with multiple axes
[dygraphs.git] / dygraph.js
index 743219c..f9d5f5e 100644 (file)
@@ -43,6 +43,8 @@
 
  */
 
+"use strict";
+
 /**
  * Creates an interactive, zoomable chart.
  *
@@ -57,7 +59,7 @@
  * whether the input data contains error ranges. For a complete list of
  * options, see http://dygraphs.com/options.html.
  */
-Dygraph = function(div, data, opts) {
+var Dygraph = function(div, data, opts) {
   if (arguments.length > 0) {
     if (arguments.length == 4) {
       // Old versions of dygraphs took in the series labels as a constructor
@@ -812,14 +814,6 @@ Dygraph.prototype.createInterface_ = function() {
     this.rangeSelector_.addToGraph(this.graphDiv, this.layout_);
   }
 
-  // Create the grapher
-  this.layout_ = new DygraphLayout(this);
-
-  if (this.rangeSelector_) {
-    // This needs to happen after the graph canvases are added to the div and the layout object is created.
-    this.rangeSelector_.addToGraph(this.graphDiv, this.layout_);
-  }
-
   var dygraph = this;
   Dygraph.addEvent(this.mouseEventElement_, 'mousemove', function(e) {
     dygraph.mouseMove_(e);
@@ -1937,8 +1931,10 @@ Dygraph.prototype.gatherDatasets_ = function(rolledSeries, dateWindow) {
 
     if (bars) {
       for (var j=0; j<series.length; j++) {
-        val = [series[j][0], series[j][1][0], series[j][1][1], series[j][1][2]];
-        series[j] = val;
+        series[j] = [series[j][0],
+                     series[j][1][0],
+                     series[j][1][1],
+                     series[j][1][2]];
       }
     } else if (this.attr_("stackedGraph")) {
       var l = series.length;
@@ -2151,17 +2147,6 @@ Dygraph.prototype.computeYAxes_ = function() {
     }
   }
 
-  // Now we remove series from seriesToAxisMap_ which are not visible. We do
-  // this last so that hiding the first series doesn't destroy the axis
-  // properties of the primary axis.
-  var seriesToAxisFiltered = {};
-  var vis = this.visibility();
-  for (var i = 1; i < labels.length; i++) {
-    var s = labels[i];
-    if (vis[i - 1]) seriesToAxisFiltered[s] = this.seriesToAxisMap_[s];
-  }
-  this.seriesToAxisMap_ = seriesToAxisFiltered;
-
   if (valueWindows != undefined) {
     // Restore valueWindow settings.
     for (var index = 0; index < valueWindows.length; index++) {
@@ -2225,7 +2210,11 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) {
       var minY = Infinity;  // extremes[series[0]][0];
       var maxY = -Infinity;  // extremes[series[0]][1];
       var extremeMinY, extremeMaxY;
+
       for (var j = 0; j < series.length; j++) {
+        // this skips invisible series
+        if (!extremes.hasOwnProperty(series[j])) continue;
+
         // Only use valid extremes to stop null data series' from corrupting the scale.
         extremeMinY = extremes[series[j]][0];
         if (extremeMinY != null) {
@@ -2238,9 +2227,9 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) {
       }
       if (axis.includeZero && minY > 0) minY = 0;
 
-      // Ensure we have a valid scale, otherwise defualt to zero for safety.
+      // Ensure we have a valid scale, otherwise default to [0, 1] for safety.
       if (minY == Infinity) minY = 0;
-      if (maxY == -Infinity) maxY = 0;
+      if (maxY == -Infinity) maxY = 1;
 
       // Add some padding and round up to an integer to be human-friendly.
       var span = maxY - minY;
@@ -2494,7 +2483,8 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
  */
 Dygraph.prototype.detectTypeFromString_ = function(str) {
   var isDate = false;
-  if (str.indexOf('-') > 0 ||
+  var dashPos = str.indexOf('-');  // could be 2006-01-01 _or_ 1.0e-2
+  if ((dashPos > 0 && (str[dashPos-1] != 'e' && str[dashPos-1] != 'E')) ||
       str.indexOf('/') >= 0 ||
       isNaN(parseFloat(str))) {
     isDate = true;
@@ -3201,4 +3191,4 @@ Dygraph.addAnnotationRule = function() {
 }
 
 // Older pages may still use this name.
-DateGraph = Dygraph;
+var DateGraph = Dygraph;