merge
[dygraphs.git] / dygraph.js
index aa6f24c..5e824ae 100644 (file)
@@ -162,6 +162,7 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
   this.dateWindow_ = attrs.dateWindow || null;
   this.valueRange_ = attrs.valueRange || null;
   this.wilsonInterval_ = attrs.wilsonInterval || true;
+  this.is_initial_draw_ = true;
 
   // Clear the div. This ensure that, if multiple dygraphs are passed the same
   // div, then only one will be drawn.
@@ -187,6 +188,7 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
     this.height_ = (this.height_ * self.innerHeight / 100) - 10;
   }
 
+  // TODO(danvk): set fillGraph to be part of attrs_ here, not user_attrs_.
   if (attrs['stackedGraph']) {
     attrs['fillGraph'] = true;
     // TODO(nikhilk): Add any other stackedGraph checks here.
@@ -808,23 +810,24 @@ Dygraph.prototype.mouseMove_ = function(event) {
     }
   }
 
-  // MERGE: check if this breaks compatibility.
   if (this.attr_("highlightCallback")) {
     var px = this.lastHighlightCallbackX;
     if (px !== null && lastx != px) {
+      // only fire if the selected point has changed.
       this.lastHighlightCallbackX = lastx;
-      this.attr_("highlightCallback")(event, lastx, this.selPoints_);
-      var callbackPoints = this.selPoints_.map(
-          function(p) { return {xval: p.xval, yval: p.yval, name: p.name} });
-      if (this.attr_("stackedGraph")) {
+      if (!this.attr_("stackedGraph")) {
+        this.attr_("highlightCallback")(event, lastx, this.selPoints_);
+      } else {
         // "unstack" the points.
+        var callbackPoints = this.selPoints_.map(
+            function(p) { return {xval: p.xval, yval: p.yval, name: p.name} });
         var cumulative_sum = 0;
         for (var j = callbackPoints.length - 1; j >= 0; j--) {
           callbackPoints[j].yval -= cumulative_sum;
           cumulative_sum += callbackPoints[j].yval;
         }
+        this.attr_("highlightCallback")(event, lastx, callbackPoints);
       }
-      this.attr_("highlightCallback")(event, lastx, callbackPoints);
     }
   }
 
@@ -907,10 +910,8 @@ Dygraph.prototype.hmsString_ = function(date) {
     return zeropad(d.getHours()) + ":" +
            zeropad(d.getMinutes()) + ":" +
            zeropad(d.getSeconds());
-  } else if (d.getMinutes()) {
-    return zeropad(d.getHours()) + ":" + zeropad(d.getMinutes());
   } else {
-    return zeropad(d.getHours());
+    return zeropad(d.getHours()) + ":" + zeropad(d.getMinutes());
   }
 }
 
@@ -1298,6 +1299,10 @@ Dygraph.prototype.extremeValues_ = function(series) {
  * @private
  */
 Dygraph.prototype.drawGraph_ = function(data) {
+  // This is used to set the second parameter to drawCallback, below.
+  var is_initial_draw = this.is_initial_draw_;
+  this.is_initial_draw_ = false;
+
   var minY = null, maxY = null;
   this.layout_.removeAllDatasets();
   this.setColors_();
@@ -1305,7 +1310,7 @@ Dygraph.prototype.drawGraph_ = function(data) {
 
   // For stacked series.
   var cumulative_y = [];
-  var datasets = [];
+  var stacked_datasets = [];
 
   // Loop over all fields in the dataset
 
@@ -1361,17 +1366,16 @@ Dygraph.prototype.drawGraph_ = function(data) {
         if (!maxY || cumulative_y[series[j][0]] > maxY)
           maxY = cumulative_y[series[j][0]];
       }
-      datasets.push([this.attr_("labels")[i], vals]);
+      stacked_datasets.push([this.attr_("labels")[i], vals]);
       //this.layout_.addDataset(this.attr_("labels")[i], vals);
     } else {
       this.layout_.addDataset(this.attr_("labels")[i], series);
     }
   }
 
-// MERGE: move up into the stackedGraph section.
-  if (datasets.length > 0) {
-    for (var i = (datasets.length - 1); i >= 0; i--) {
-      this.layout_.addDataset(datasets[i][0], datasets[i][1]);
+  if (stacked_datasets.length > 0) {
+    for (var i = (stacked_datasets.length - 1); i >= 0; i--) {
+      this.layout_.addDataset(stacked_datasets[i][0], stacked_datasets[i][1]);
     }
   }
 
@@ -1415,7 +1419,7 @@ Dygraph.prototype.drawGraph_ = function(data) {
                                          this.canvas_.height);
 
   if (this.attr_("drawCallback") !== null) {
-    this.attr_("drawCallback")(this);
+    this.attr_("drawCallback")(this, is_initial_draw);
   }
 };
 
@@ -1565,7 +1569,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
 Dygraph.dateParser = function(dateStr, self) {
   var dateStrSlashed;
   var d;
-  if (dateStr.length == 10 && dateStr.search("-") != -1) {  // e.g. '2009-07-12'
+  if (dateStr.search("-") != -1) {  // e.g. '2009-7-12' or '2009-07-12'
     dateStrSlashed = dateStr.replace("-", "/", "g");
     while (dateStrSlashed.search("-") != -1) {
       dateStrSlashed = dateStrSlashed.replace("-", "/");