Add a test case for rolling averages and fix two bugs that it uncovered
authorDan Vanderkam <danvk@google.com>
Tue, 9 Aug 2011 15:47:12 +0000 (11:47 -0400)
committerDan Vanderkam <danvk@google.com>
Tue, 9 Aug 2011 15:47:12 +0000 (11:47 -0400)
auto_tests/misc/local.html
auto_tests/tests/rolling_average.js [new file with mode: 0644]
dygraph.js

index 4727f08..1fba05c 100644 (file)
@@ -27,6 +27,7 @@
   <script type="text/javascript" src="../tests/custom_bars.js"></script>
   <script type="text/javascript" src="../tests/css.js"></script>
   <script type="text/javascript" src="../tests/selection.js"></script>
+  <script type="text/javascript" src="../tests/rolling_average.js"></script>
   <script type="text/javascript" src="../tests/update_options.js"></script>
 
 
diff --git a/auto_tests/tests/rolling_average.js b/auto_tests/tests/rolling_average.js
new file mode 100644 (file)
index 0000000..2dd157f
--- /dev/null
@@ -0,0 +1,87 @@
+/**
+ * @fileoverview Tests for rolling averages.
+ *
+ * @author danvk@google.com (Dan Vanderkam)
+ */
+var rollingAverageTestCase = TestCase("rolling-average");
+
+rollingAverageTestCase.prototype.setUp = function() {
+  document.body.innerHTML = "<div id='graph'></div>";
+};
+
+rollingAverageTestCase.prototype.tearDown = function() {
+};
+
+rollingAverageTestCase.prototype.getLegend = function() {
+  return document.getElementsByClassName("dygraph-legend")[0].textContent;
+};
+
+rollingAverageTestCase.prototype.testRollingAverage = function() {
+  var opts = {
+    width: 480,
+    height: 320,
+    rollPeriod: 1,
+    showRoller: true
+  };
+  var data = "X,Y\n" +
+      "0,0\n" +
+      "1,1\n" +
+      "2,2\n" +
+      "3,3\n"
+  ;
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+
+  g.setSelection(0); assertEquals("0: Y:0", this.getLegend());
+  g.setSelection(1); assertEquals("1: Y:1", this.getLegend());
+  g.setSelection(2); assertEquals("2: Y:2", this.getLegend());
+  g.setSelection(3); assertEquals("3: Y:3", this.getLegend());
+  assertEquals(1, g.rollPeriod());
+
+  g.updateOptions({rollPeriod: 2});
+  g.setSelection(0); assertEquals("0: Y:0", this.getLegend());
+  g.setSelection(1); assertEquals("1: Y:0.5", this.getLegend());
+  g.setSelection(2); assertEquals("2: Y:1.5", this.getLegend());
+  g.setSelection(3); assertEquals("3: Y:2.5", this.getLegend());
+  assertEquals(2, g.rollPeriod());
+
+  g.updateOptions({rollPeriod: 3});
+  g.setSelection(0); assertEquals("0: Y:0", this.getLegend());
+  g.setSelection(1); assertEquals("1: Y:0.5", this.getLegend());
+  g.setSelection(2); assertEquals("2: Y:1", this.getLegend());
+  g.setSelection(3); assertEquals("3: Y:2", this.getLegend());
+  assertEquals(3, g.rollPeriod());
+
+  g.updateOptions({rollPeriod: 4});
+  g.setSelection(0); assertEquals("0: Y:0", this.getLegend());
+  g.setSelection(1); assertEquals("1: Y:0.5", this.getLegend());
+  g.setSelection(2); assertEquals("2: Y:1", this.getLegend());
+  g.setSelection(3); assertEquals("3: Y:1.5", this.getLegend());
+  assertEquals(4, g.rollPeriod());
+};
+
+rollingAverageTestCase.prototype.testRollBoxDoesntDisapper = function() {
+  var opts = {
+    showRoller: true
+  };
+  var data = "X,Y\n" +
+      "0,0\n" +
+      "1,1\n" +
+      "2,2\n" +
+      "3,3\n"
+  ;
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+
+  var roll_box = graph.getElementsByTagName("input");
+  assertEquals(1, roll_box.length);
+  assertEquals("1", roll_box[0].value);
+
+  graph.style.width = "500px";
+  g.resize();
+  assertEquals(1, roll_box.length);
+  assertEquals("1", roll_box[0].value);
+};
+
index d80cc71..86a5b37 100644 (file)
@@ -2365,7 +2365,7 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) {
 Dygraph.prototype.rollingAverage = function(originalData, rollPeriod) {
   if (originalData.length < 2)
     return originalData;
-  var rollPeriod = Math.min(rollPeriod, originalData.length - 1);
+  var rollPeriod = Math.min(rollPeriod, originalData.length);
   var rollingData = [];
   var sigma = this.attr_("sigma");
 
@@ -3025,6 +3025,7 @@ Dygraph.prototype.resize = function(width, height) {
   if (old_width != this.width_ || old_height != this.height_) {
     // TODO(danvk): there should be a clear() method.
     this.maindiv_.innerHTML = "";
+    this.roller_ = null;
     this.attrs_.labelsDiv = null;
     this.createInterface_();
     this.predraw_();