From: Dan Vanderkam Date: Wed, 21 Dec 2011 22:22:30 +0000 (-0500) Subject: Fix Issue 60: Add tests for one data point and zero data points X-Git-Tag: v1.0.0~393 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=395e98a32b32d83097a641afb297b755734074c4;p=dygraphs.git Fix Issue 60: Add tests for one data point and zero data points The zero point case is pathological, but at least it no longer crashes. Also includes some miscellaneous other cleanup. --- diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index a85132c..cb9e2f1 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -33,6 +33,7 @@ + diff --git a/auto_tests/tests/multiple_axes.js b/auto_tests/tests/multiple_axes.js index eb74f60..ed9b362 100644 --- a/auto_tests/tests/multiple_axes.js +++ b/auto_tests/tests/multiple_axes.js @@ -140,7 +140,7 @@ MultipleAxesTestCase.prototype.testTwoAxisVisibility = function() { data.push([1,2,2000]); data.push([2,4,1000]); - g = new Dygraph( + var g = new Dygraph( document.getElementById("graph"), data, { diff --git a/auto_tests/tests/pathological_cases.js b/auto_tests/tests/pathological_cases.js new file mode 100644 index 0000000..c134b5f --- /dev/null +++ b/auto_tests/tests/pathological_cases.js @@ -0,0 +1,37 @@ +/** + * @fileoverview Tests zero and one-point charts. + * These don't have to render nicely, they just have to not crash. + * + * @author dan@dygraphs.com (Dan Vanderkam) + */ +var pathologicalCasesTestCase = TestCase("pathological-cases"); + +pathologicalCasesTestCase.prototype.setUp = function() { + document.body.innerHTML = "
"; +}; + +pathologicalCasesTestCase.prototype.tearDown = function() { +}; + +pathologicalCasesTestCase.prototype.testZeroPoint = function() { + var opts = { + width: 480, + height: 320 + }; + var data = "X,Y\n"; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); +}; + +pathologicalCasesTestCase.prototype.testOnePoint = function() { + var opts = { + width: 480, + height: 320 + }; + var data = "X,Y\n" + + "1,2\n"; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); +}; diff --git a/dygraph.js b/dygraph.js index f9d5f5e..2dc4610 100644 --- a/dygraph.js +++ b/dygraph.js @@ -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,20 @@ 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. + * @return { Array } 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. @@ -1754,7 +1767,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'); @@ -1849,7 +1862,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); @@ -2369,7 +2382,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) { @@ -3085,7 +3098,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");