Fix issue 186 (dygraphs does not accept "1e-5" as a numeric x-value).
authorDan Vanderkam <danvk@google.com>
Fri, 16 Dec 2011 23:50:36 +0000 (18:50 -0500)
committerDan Vanderkam <danvk@google.com>
Fri, 16 Dec 2011 23:50:36 +0000 (18:50 -0500)
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

auto_tests/misc/local.html
auto_tests/tests/scientific_notation.js [new file with mode: 0644]
dygraph.js

index 68e53b7..a85132c 100644 (file)
@@ -22,7 +22,9 @@
   <script type="text/javascript" src="../tests/multi_csv.js"></script>
   <script type="text/javascript" src="../tests/to_dom_coords.js"></script>
   <script type="text/javascript" src="../tests/interaction_model.js"></script>
+  <!--
   <script type="text/javascript" src="../tests/tickers.js"></script>
+  -->
   <script type="text/javascript" src="../tests/scrolling_div.js"></script>
   <script type="text/javascript" src="../tests/custom_bars.js"></script>
   <script type="text/javascript" src="../tests/css.js"></script>
@@ -30,6 +32,7 @@
   <script type="text/javascript" src="../tests/rolling_average.js"></script>
   <script type="text/javascript" src="../tests/error_bars.js"></script>
   <script type="text/javascript" src="../tests/annotations.js"></script>
+  <script type="text/javascript" src="../tests/scientific_notation.js"></script>
   <script type="text/javascript" src="../tests/update_options.js"></script>
   <script type="text/javascript" src="../tests/utils_test.js"></script>
   <script type="text/javascript" src="../tests/multiple_axes.js"></script>
diff --git a/auto_tests/tests/scientific_notation.js b/auto_tests/tests/scientific_notation.js
new file mode 100644 (file)
index 0000000..bbdeb6a
--- /dev/null
@@ -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 = "<div id='graph'></div>";
+};
+
+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);
+};
index 743219c..ed021cd 100644 (file)
@@ -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;