From: Dan Vanderkam Date: Fri, 16 Dec 2011 23:50:36 +0000 (-0500) Subject: Fix issue 186 (dygraphs does not accept "1e-5" as a numeric x-value). X-Git-Tag: v1.0.0~406 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=0842b24b38096009a743ee1d28c25c4714fa2123;p=dygraphs.git Fix issue 186 (dygraphs does not accept "1e-5" as a numeric x-value). This turned out to be because the "-" was causing it to be treated as a date (e.g. "2006-01-01"). Bug: http://code.google.com/p/dygraphs/issues/detail?id=186 --- diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 68e53b7..a85132c 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -22,7 +22,9 @@ + @@ -30,6 +32,7 @@ + diff --git a/auto_tests/tests/scientific_notation.js b/auto_tests/tests/scientific_notation.js new file mode 100644 index 0000000..bbdeb6a --- /dev/null +++ b/auto_tests/tests/scientific_notation.js @@ -0,0 +1,75 @@ +/** + * @fileoverview Tests input data which uses scientific notation. + * This is a regression test for + * http://code.google.com/p/dygraphs/issues/detail?id=186 + * + * @author danvk@google.com (Dan Vanderkam) + */ +var scientificNotationTestCase = TestCase("scientific-notation"); + +scientificNotationTestCase.prototype.setUp = function() { + document.body.innerHTML = "
"; +}; + +scientificNotationTestCase.prototype.tearDown = function() { +}; + +function getXValues(g) { + var xs = []; + for (var i = 0; i < g.numRows(); i++) { + xs.push(g.getValue(i, 0)); + } + return xs; +} + +scientificNotationTestCase.prototype.testScientificInput = function() { + var data = "X,Y\n" + + "1.0e1,-1\n" + + "2.0e1,0\n" + + "3.0e1,1\n" + + "4.0e1,0\n" + ; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, {}); + assertEqualsDelta([10, 20, 30, 40], getXValues(g), 1e-6); +}; + +scientificNotationTestCase.prototype.testScientificInputPlus = function() { + var data = "X,Y\n" + + "1.0e+1,-1\n" + + "2.0e+1,0\n" + + "3.0e+1,1\n" + + "4.0e+1,0\n" + ; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, {}); + assertEqualsDelta([10, 20, 30, 40], getXValues(g), 1e-6); +}; + +scientificNotationTestCase.prototype.testScientificInputMinus = function() { + var data = "X,Y\n" + + "1.0e-1,-1\n" + + "2.0e-1,0\n" + + "3.0e-1,1\n" + + "4.0e-1,0\n" + ; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, {}); + assertEqualsDelta([0.1, 0.2, 0.3, 0.4], getXValues(g), 1e-6); +}; + +scientificNotationTestCase.prototype.testScientificInputMinusCap = function() { + var data = "X,Y\n" + + "1.0E-1,-1\n" + + "2.0E-1,0\n" + + "3.0E-1,1\n" + + "4.0E-1,0\n" + ; + + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, {}); + assertEqualsDelta([0.1, 0.2, 0.3, 0.4], getXValues(g), 1e-6); +}; diff --git a/dygraph.js b/dygraph.js index 743219c..ed021cd 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2494,7 +2494,8 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) { */ Dygraph.prototype.detectTypeFromString_ = function(str) { var isDate = false; - if (str.indexOf('-') > 0 || + var dashPos = str.indexOf('-'); // could be 2006-01-01 _or_ 1.0e-2 + if ((dashPos > 0 && (str[dashPos-1] != 'e' && str[dashPos-1] != 'E')) || str.indexOf('/') >= 0 || isNaN(parseFloat(str))) { isDate = true;