From: Dan Vanderkam Date: Fri, 1 Feb 2013 17:21:51 +0000 (-0800) Subject: Merge pull request #199 from witsa/master X-Git-Tag: v1.0.0~105 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=7071b20650f42804b37ecec270bc99729403dd0c;hp=81ce45298e149139aff0ddfa77b768365c683444;p=dygraphs.git Merge pull request #199 from witsa/master some robustness and partial auto value range --- diff --git a/auto_tests/tests/range_tests.js b/auto_tests/tests/range_tests.js index c2ef95e..83a2505 100644 --- a/auto_tests/tests/range_tests.js +++ b/auto_tests/tests/range_tests.js @@ -70,6 +70,30 @@ RangeTestCase.prototype.testRangeSetOperations = function() { assertEquals([12, 18], g.xAxisRange()); assertEquals([10, 40], g.yAxisRange(0)); + g.updateOptions({ valueRange: [10, NaN] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 40], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, null] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 40], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, undefined] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 44.2], g.yAxisRange(0)); + + g.updateOptions({ valueRange: [10, 40] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 40], g.yAxisRange(0)); + g.updateOptions({ }); assertEquals([12, 18], g.xAxisRange()); assertEquals([10, 40], g.yAxisRange(0)); @@ -162,6 +186,84 @@ RangeTestCase.prototype.testIncludeZeroIncludesZero = function() { assertEquals([450, 1050], g.yAxisRange(0)); } + +/** + * Verify that includeZero range is properly specified per axis. + */ +RangeTestCase.prototype.testIncludeZeroPerAxis = function() { + var g = new Dygraph("graph", + 'X,A,B\n'+ + '0,50,50\n'+ + '50,110,110\n', + { + drawPoints: true, + pointSize:5, + series:{ + A: { + axis: 'y', + pointSize: 10 + }, + B: { + axis: 'y2' + } + }, + axes: { + 'y2': { includeZero: true } + } + }); + + + assertEquals([44, 116], g.yAxisRange(0)); + assertEquals([0, 121], g.yAxisRange(1)); + + g.updateOptions({ + axes: { + 'y2': { includeZero: false } + } + }); + + assertEquals([44, 116], g.yAxisRange(1)); +} + + +/** + * Verify that includeZero range is properly specified per axis with old axis options. + */ +RangeTestCase.prototype.testIncludeZeroPerAxisOld = function() { + var g = new Dygraph("graph", + 'X,A,B\n' + + '0,50,50\n' + + '50,110,110\n', + { + drawPoints: true, + pointSize: 5, + + A: { + pointSize: 10 + }, + B: { + axis: {} + }, + axes: { + 'y': { includeZero: true }, + 'y2': { includeZero: false } + } + }); + + assertEquals([0, 121], g.yAxisRange(0)); + assertEquals([44, 116], g.yAxisRange(1)); + + g.updateOptions({ + axes: { + 'y': { includeZero: false }, + 'y2': { includeZero: true } + } + }); + + assertEquals([44, 116], g.yAxisRange(0)); + assertEquals([0, 121], g.yAxisRange(1)); +} + /** * Verify that very large Y ranges don't break things. */ diff --git a/dygraph-options-reference.js b/dygraph-options-reference.js index 98cf22f..0f601d1 100644 --- a/dygraph-options-reference.js +++ b/dygraph-options-reference.js @@ -421,7 +421,7 @@ Dygraph.OPTIONS_REFERENCE = // "labels": ["Axis display"], "type": "Array of two numbers", "example": "[10, 110]", - "description": "Explicitly set the vertical range of the graph to [low, high]. This may be set on a per-axis basis to define each y-axis separately." + "description": "Explicitly set the vertical range of the graph to [low, high]. This may be set on a per-axis basis to define each y-axis separately. If either limit is unspecified, it will be calculated automatically (e.g. [null, 30] to automatically calculate just the lower bound)" }, "labelsDivWidth": { "default": "250", diff --git a/dygraph.js b/dygraph.js index cd15aad..941badb 100644 --- a/dygraph.js +++ b/dygraph.js @@ -993,10 +993,13 @@ Dygraph.prototype.destroy = function() { } }; - for (var idx = 0; idx < this.registeredEvents_.length; idx++) { - var reg = this.registeredEvents_[idx]; - Dygraph.removeEvent(reg.elem, reg.type, reg.fn); + if (this.registeredEvents_) { + for (var idx = 0; idx < this.registeredEvents_.length; idx++) { + var reg = this.registeredEvents_[idx]; + Dygraph.removeEvent(reg.elem, reg.type, reg.fn); + } } + this.registeredEvents_ = []; // remove mouse event handlers (This may not be necessary anymore) @@ -2500,6 +2503,10 @@ Dygraph.prototype.axisPropertiesForSeries = function(series) { * This fills in the valueRange and ticks fields in each entry of this.axes_. */ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { + + var isNullUndefinedOrNaN = function(num) { + return isNaN(parseFloat(num)); + }; var series; var numAxes = this.attributes_.numAxes(); @@ -2572,7 +2579,10 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { axis.computedValueRange = [axis.valueWindow[0], axis.valueWindow[1]]; } else if (axis.valueRange) { // This is a user-set value range for this axis. - axis.computedValueRange = [axis.valueRange[0], axis.valueRange[1]]; + axis.computedValueRange = [ + isNullUndefinedOrNaN(axis.valueRange[0]) ? axis.extremeRange[0] : axis.valueRange[0], + isNullUndefinedOrNaN(axis.valueRange[1]) ? axis.extremeRange[1] : axis.valueRange[1] + ]; } else { axis.computedValueRange = axis.extremeRange; }