Merge pull request #229 from clocksmith/master
authorRobert Konigsberg <konigsberg@gmail.com>
Tue, 26 Feb 2013 18:37:06 +0000 (10:37 -0800)
committerRobert Konigsberg <konigsberg@gmail.com>
Tue, 26 Feb 2013 18:37:06 +0000 (10:37 -0800)
Fix the bug intorduced from adding ypad and ypadCompat to no longer return NaN values for the yAxisRange. Test added to guarantee that the underlay callback is called even when there are no series and that the yAxisRange is valid.

auto_tests/tests/callback.js
dygraph.js

index e44e397..535bd0e 100644 (file)
@@ -565,3 +565,49 @@ CallbackTestCase.prototype.testHighlightCallbackRow = function() {
   assertEquals(2, highlightRow);
   assertEquals('2: Y: 3 Z: 4', Util.getLegend());
 };
+
+/**
+ * Test that underlay callback is called even when there are no series,
+ * and that the y axis ranges are not NaN.
+ */
+CallbackTestCase.prototype.underlayCallback_noSeries = function() {
+  var called = false;
+  var yMin, yMax;
+
+  var callback = function(canvas, area, g) {
+    called = true;
+    yMin = g.yAxisRange(0)[0];
+    yMax = g.yAxisRange(0)[1];
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, "\n", {
+      underlayCallback: callback
+    });
+
+  assertTrue(called);
+  assertFalse(isNaN(yMin));
+  assertFalse(isNaN(yMax));
+};
+
+/**
+ * Test that underlay callback receives the correct y-axis range.
+ */
+CallbackTestCase.prototype.underlayCallback_yAxisRange = function() {
+  var called = false;
+  var yMin, yMax;
+
+  var callback = function(canvas, area, g) {
+    yMin = g.yAxisRange(0)[0];
+    yMax = g.yAxisRange(0)[1];
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, "\n", {
+      valueRange: [0,10],
+      underlayCallback: callback
+    });
+
+  assertEquals(0, yMin));
+  assertEquals(10, yMax));
+};
index b2c9ba8..c1a9bce 100644 (file)
@@ -2594,6 +2594,28 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) {
     var includeZero = this.attributes_.getForAxis("includeZero", i);
     series = this.attributes_.seriesForAxis(i);
 
+    // Add some padding. This supports two Y padding operation modes:
+    //
+    // - backwards compatible (yRangePad not set):
+    //   10% padding for automatic Y ranges, but not for user-supplied
+    //   ranges, and move a close-to-zero edge to zero except if
+    //   avoidMinZero is set, since drawing at the edge results in
+    //   invisible lines. Unfortunately lines drawn at the edge of a
+    //   user-supplied range will still be invisible. If logscale is
+    //   set, add a variable amount of padding at the top but
+    //   none at the bottom.
+    //
+    // - new-style (yRangePad set by the user):
+    //   always add the specified Y padding.
+    //
+    ypadCompat = true;
+    ypad = 0.1; // add 10%
+    if (this.attr_('yRangePad') !== null) {
+      ypadCompat = false;
+      // Convert pixel padding to ratio
+      ypad = this.attr_('yRangePad') / this.plotter_.area.h;
+    }
+
     if (series.length === 0) {
       // If no series are defined or visible then use a reasonable default
       axis.extremeRange = [0, 1];
@@ -2640,28 +2662,6 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) {
         }
       }
 
-      // Add some padding. This supports two Y padding operation modes:
-      //
-      // - backwards compatible (yRangePad not set):
-      //   10% padding for automatic Y ranges, but not for user-supplied
-      //   ranges, and move a close-to-zero edge to zero except if
-      //   avoidMinZero is set, since drawing at the edge results in
-      //   invisible lines. Unfortunately lines drawn at the edge of a
-      //   user-supplied range will still be invisible. If logscale is
-      //   set, add a variable amount of padding at the top but
-      //   none at the bottom.
-      //
-      // - new-style (yRangePad set by the user):
-      //   always add the specified Y padding.
-      //
-      ypadCompat = true;
-      ypad = 0.1; // add 10%
-      if (this.attr_('yRangePad') !== null) {
-        ypadCompat = false;
-        // Convert pixel padding to ratio
-        ypad = this.attr_('yRangePad') / this.plotter_.area.h;
-      }
-
       var maxAxisY, minAxisY;
       if (logscale) {
         if (ypadCompat) {