From 77b5e09d8097138dfd4871db7a1240c787b916af Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Tue, 9 Aug 2011 11:47:12 -0400 Subject: [PATCH] Add a test case for rolling averages and fix two bugs that it uncovered --- auto_tests/misc/local.html | 1 + auto_tests/tests/rolling_average.js | 87 +++++++++++++++++++++++++++++++++++++ dygraph.js | 3 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 auto_tests/tests/rolling_average.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 4727f08..1fba05c 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -27,6 +27,7 @@ + diff --git a/auto_tests/tests/rolling_average.js b/auto_tests/tests/rolling_average.js new file mode 100644 index 0000000..2dd157f --- /dev/null +++ b/auto_tests/tests/rolling_average.js @@ -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 = "
"; +}; + +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); +}; + diff --git a/dygraph.js b/dygraph.js index d80cc71..86a5b37 100644 --- a/dygraph.js +++ b/dygraph.js @@ -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_(); -- 2.7.4