fix infinity bug
authorDan Vanderkam <danvk@google.com>
Mon, 22 Nov 2010 15:46:47 +0000 (10:46 -0500)
committerDan Vanderkam <danvk@google.com>
Mon, 22 Nov 2010 15:46:47 +0000 (10:46 -0500)
dygraph.js
tests/gviz-infinity.html [new file with mode: 0644]

index c24f4f3..ca935a1 100644 (file)
@@ -2413,7 +2413,8 @@ Dygraph.prototype.parseCSV_ = function(data) {
   // Parse the x as a float or return null if it's not a number.
   var parseFloatOrNull = function(x) {
     var val = parseFloat(x);
-    return isNaN(val) ? null : val;
+    // isFinite() returns false for NaN and +/-Infinity.
+    return isFinite(val) ? val : null;
   };
 
   var xParser;
@@ -2645,6 +2646,11 @@ Dygraph.prototype.parseDataTable_ = function(data) {
     if (ret.length > 0 && row[0] < ret[ret.length - 1][0]) {
       outOfOrder = true;
     }
+
+    // Strip out infinities, which give dygraphs problems later on.
+    for (var j = 0; j < row.length; j++) {
+      if (!isFinite(row[j])) row[j] = null;
+    }
     ret.push(row);
   }
 
diff --git a/tests/gviz-infinity.html b/tests/gviz-infinity.html
new file mode 100644 (file)
index 0000000..9361a97
--- /dev/null
@@ -0,0 +1,62 @@
+<html>
+  <head>
+    <title>gviz</title>
+    <!--[if IE]>
+    <script type="text/javascript" src="../excanvas.js"></script>
+    <![endif]-->
+    <script type="text/javascript" src="../strftime/strftime-min.js"></script>
+    <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
+    <script type="text/javascript" src="../dygraph-canvas.js"></script>
+    <script type="text/javascript" src="../dygraph.js"></script>
+    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
+    <script type="text/javascript">
+      google.load('visualization', '1', {packages: ['linechart']});
+
+      function createDataTable(dateType) {
+        data = new google.visualization.DataTable();
+        data.addColumn(dateType, 'Date');
+        data.addColumn('number', 'Column A');
+        data.addColumn('number', 'Column B');
+        data.addRows(4);
+        data.setCell(0, 0, new Date("2009/07/01"));
+        data.setCell(0, 1, 1);
+        data.setCell(0, 2, 7);
+        data.setCell(1, 0, new Date("2009/07/08"));
+        data.setCell(1, 1, 2);
+        data.setCell(1, 2, 4);
+        data.setCell(2, 0, new Date("2009/07/15"));
+        data.setCell(2, 1, 3);
+        data.setCell(2, 2, Infinity);
+        data.setCell(3, 0, new Date("2009/07/22"));
+        data.setCell(3, 1, 4);
+        data.setCell(3, 2, 0);
+        return data;
+      }
+
+      function drawVisualization() {
+        data = createDataTable('date');
+        new google.visualization.LineChart(
+            document.getElementById('gviz')).draw(data, null);
+
+        new Dygraph.GVizChart(document.getElementById('dygraphs'))
+          .draw(data, { });
+
+        data = createDataTable('datetime');
+        new Dygraph.GVizChart(
+            document.getElementById('dygraphs_datetime')).draw(data, {
+            });
+      }
+
+      google.setOnLoadCallback(drawVisualization);
+    </script>
+  </head>
+  <body>
+    <p>This tests that infinite values don't break dygraphs.</p>
+    <p>gviz line chart:</p>
+    <div id="gviz" style="width:600px; height:300px;"></div>
+
+    <p>same data drawn using dygraphs:</p>
+    date column:
+    <div id="dygraphs" style="width:600px; height:300px;"></div>
+  </body>
+</html>