Fix & regression test for issue 392: connectSeparated and errorBars don't play nicely...
authorDan Vanderkam <danvdk@gmail.com>
Wed, 20 Feb 2013 04:08:40 +0000 (23:08 -0500)
committerDan Vanderkam <danvdk@gmail.com>
Wed, 20 Feb 2013 04:08:40 +0000 (23:08 -0500)
auto_tests/tests/error_bars.js
dygraph.js

index e57898b..7e50e42 100644 (file)
@@ -128,4 +128,51 @@ errorBarsTestCase.prototype.testErrorBarsCorrectColors = function() {
   assertEquals([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 125));
   assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 175));
   assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 225));
-}
+};
+
+
+// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=392
+errorBarsTestCase.prototype.testRollingAveragePreservesNaNs = function() {
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph,
+  [
+      [1, [null, null], [3,1]],
+      [2, [2, 1], [null, null]],
+      [3, [null, null], [5,1]],
+      [4, [4, 0.5], [null, null]],
+      [5, [null, null], [7,1]],
+      [6, [NaN, NaN], [null, null]],
+      [8, [8, 1], [null, null]],
+      [10, [10, 1], [null, null]]
+     ]
+        , {
+          labels: ['x', 'A', 'B' ],
+          connectSeparatedPoints: true,
+          drawPoints: true,
+          errorBars: true
+        }
+      );
+
+  var in_series = [
+    [1, [null, null]],
+    [2, [2, 1]],
+    [3, [null, null]],
+    [4, [4, 0.5]],
+    [5, [null, null]],
+    [6, [NaN, NaN]],
+    [8, [8, 1]],
+    [10, [10, 1]]
+  ];
+  assertEquals(null, in_series[4][1][0]);
+  assertEquals(null, in_series[4][1][1]);
+  assertNaN(in_series[5][1][0]);
+  assertNaN(in_series[5][1][1]);
+
+  var out_series = g.rollingAverage(in_series, 1);
+  assertNaN(out_series[5][1][0]);
+  assertNaN(out_series[5][1][1]);
+  assertNaN(out_series[5][1][2]);
+  assertEquals(null, out_series[4][1][0]);
+  assertEquals(null, out_series[4][1][1]);
+  assertEquals(null, out_series[4][1][1]);
+};
index 1f55870..9ac1181 100644 (file)
@@ -2896,7 +2896,10 @@ Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
           rollingData[i] = [originalData[i][0],
                             [sum / num_ok, sigma * stddev, sigma * stddev]];
         } else {
-          rollingData[i] = [originalData[i][0], [null, null, null]];
+          // This explicitly preserves NaNs to aid with "independent series".
+          // See testRollingAveragePreservesNaNs.
+          var v = (rollPeriod == 1) ? originalData[i][1][0] : null;
+          rollingData[i] = [originalData[i][0], [v, v, v]];
         }
       }
     }