Fix issue 237 (standard date/time string)
[dygraphs.git] / dygraph.js
index 49843a4..a5e1b56 100644 (file)
@@ -341,7 +341,6 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
   this.fractions_ = attrs.fractions || false;
   this.dateWindow_ = attrs.dateWindow || null;
 
-  this.wilsonInterval_ = attrs.wilsonInterval || true;
   this.is_initial_draw_ = true;
   this.annotations_ = [];
 
@@ -737,7 +736,7 @@ Dygraph.prototype.toPercentXCoord = function(x) {
  * @return { Integer } The number of columns.
  */
 Dygraph.prototype.numColumns = function() {
-  return this.rawData_[0].length;
+  return this.rawData_[0] ? this.rawData_[0].length : this.attr_("labels").length;
 };
 
 /**
@@ -749,6 +748,21 @@ Dygraph.prototype.numRows = function() {
 };
 
 /**
+ * Returns the full range of the x-axis, as determined by the most extreme
+ * values in the data set. Not affected by zooming, visibility, etc.
+ * TODO(danvk): merge w/ xAxisExtremes
+ * @return { Array<Number> } A [low, high] pair
+ * @private
+ */
+Dygraph.prototype.fullXRange_ = function() {
+  if (this.numRows() > 0) {
+    return [this.rawData_[0][0], this.rawData_[this.numRows() - 1][0]];
+  } else {
+    return [0, 1];
+  }
+}
+
+/**
  * Returns the value in the given row and column. If the row and column exceed
  * the bounds on the data, returns null. Also returns null if the value is
  * missing.
@@ -814,14 +828,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);
@@ -1762,7 +1768,7 @@ Dygraph.prototype.addXTicks_ = function() {
   if (this.dateWindow_) {
     range = [this.dateWindow_[0], this.dateWindow_[1]];
   } else {
-    range = [this.rawData_[0][0], this.rawData_[this.rawData_.length - 1][0]];
+    range = this.fullXRange_();
   }
 
   var xAxisOptionsView = this.optionsViewForAxis_('x');
@@ -1857,7 +1863,7 @@ Dygraph.prototype.predraw_ = function() {
   // Convert the raw data (a 2D array) into the internal format and compute
   // rolling averages.
   this.rolledSeries_ = [null];  // x-axis is the first series and it's special
-  for (var i = 1; i < this.rawData_[0].length; i++) {
+  for (var i = 1; i < this.numColumns(); i++) {
     var connectSeparatedPoints = this.attr_('connectSeparatedPoints', i);
     var logScale = this.attr_('logscale', i);
     var series = this.extractSeries_(this.rawData_, i, logScale, connectSeparatedPoints);
@@ -1939,8 +1945,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;
@@ -2153,17 +2161,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++) {
@@ -2227,7 +2224,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) {
@@ -2240,9 +2241,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;
@@ -2382,7 +2383,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
       var date = originalData[i][0];
       var value = den ? num / den : 0.0;
       if (this.attr_("errorBars")) {
-        if (this.wilsonInterval_) {
+        if (this.attr_("wilsonInterval")) {
           // For more details on this confidence interval, see:
           // http://en.wikipedia.org/wiki/Binomial_confidence_interval
           if (den) {
@@ -3098,7 +3099,7 @@ Dygraph.prototype.visibility = function() {
   if (!this.attr_("visibility")) {
     this.attrs_["visibility"] = [];
   }
-  while (this.attr_("visibility").length < this.rawData_[0].length - 1) {
+  while (this.attr_("visibility").length < this.numColumns() - 1) {
     this.attr_("visibility").push(true);
   }
   return this.attr_("visibility");