check for null/undefined/NaN values, not zero values. add test
authorDan Vanderkam <danvk@google.com>
Sat, 12 Dec 2009 00:15:59 +0000 (16:15 -0800)
committerDan Vanderkam <danvk@google.com>
Sat, 12 Dec 2009 00:15:59 +0000 (16:15 -0800)
dygraph.js
tests/zero-series.html [new file with mode: 0644]

index bdfa1b1..d10ca38 100644 (file)
@@ -1268,7 +1268,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
       var y = data[1];
       rollingData[i] = [originalData[i][0], [y, y - data[0], data[2] - y]];
 
-      if (y && !isNaN(y)) {
+      if (y != null && !isNaN(y)) {
         low += data[0];
         mid += y;
         high += data[2];
@@ -1276,7 +1276,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
       }
       if (i - rollPeriod >= 0) {
         var prev = originalData[i - rollPeriod];
-        if (prev[1][1] && !isNaN(prev[1][1])) {
+        if (prev[1][1] != null && !isNaN(prev[1][1])) {
           low -= prev[1][0];
           mid -= prev[1][1];
           high -= prev[1][2];
@@ -1301,7 +1301,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
         var num_ok = 0;
         for (var j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
           var y = originalData[j][1];
-          if (!y || isNaN(y)) continue;
+          if (y == null || isNaN(y)) continue;
           num_ok++;
           sum += originalData[j][1];
         }
@@ -1319,7 +1319,7 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
         var num_ok = 0;
         for (var j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
           var y = originalData[j][1][0];
-          if (!y || isNaN(y)) continue;
+          if (y == null || isNaN(y)) continue;
           num_ok++;
           sum += originalData[j][1][0];
           variance += Math.pow(originalData[j][1][1], 2);
diff --git a/tests/zero-series.html b/tests/zero-series.html
new file mode 100644 (file)
index 0000000..4e09df7
--- /dev/null
@@ -0,0 +1,29 @@
+<html>
+  <head>
+    <title>zero series</title>
+    <!--[if IE]>
+    <script type="text/javascript" src="excanvas.js"></script>
+    <![endif]-->
+    <script type="text/javascript" src="../dygraph-combined.js"></script>
+    <script type="text/javascript" src="../dygraph-canvas.js"></script>
+    <script type="text/javascript" src="../dygraph.js"></script>
+  </head>
+  <body>
+    <p>Custom bars with a completely zero series. Both Y1 and Y2 should show.</p>
+
+    <div id="data" style="display:none">X,Y1,Y2
+1,1;2;3,0;0;0
+2,2;3;4,0;0;0
+3,1;3;5,0;0;0
+</div>
+
+    <div id="graphdiv" style="width:600px; height:300px;"></div>
+    <script type="text/javascript">
+      new Dygraph(document.getElementById("graphdiv"),
+          document.getElementById("data").innerHTML,
+          {
+            customBars: true
+          });
+    </script>
+  </body>
+</html>