| 1 | /** |
| 2 | * @fileoverview Tests for rolling averages. |
| 3 | * |
| 4 | * @author danvk@google.com (Dan Vanderkam) |
| 5 | */ |
| 6 | var rollingAverageTestCase = TestCase("rolling-average"); |
| 7 | |
| 8 | rollingAverageTestCase.prototype.setUp = function() { |
| 9 | document.body.innerHTML = "<div id='graph'></div>"; |
| 10 | }; |
| 11 | |
| 12 | rollingAverageTestCase.prototype.tearDown = function() { |
| 13 | }; |
| 14 | |
| 15 | rollingAverageTestCase.prototype.testRollingAverage = function() { |
| 16 | var opts = { |
| 17 | width: 480, |
| 18 | height: 320, |
| 19 | rollPeriod: 1, |
| 20 | showRoller: true |
| 21 | }; |
| 22 | var data = "X,Y\n" + |
| 23 | "0,0\n" + |
| 24 | "1,1\n" + |
| 25 | "2,2\n" + |
| 26 | "3,3\n" |
| 27 | ; |
| 28 | |
| 29 | var graph = document.getElementById("graph"); |
| 30 | var g = new Dygraph(graph, data, opts); |
| 31 | |
| 32 | g.setSelection(0); assertEquals("0: Y: 0", Util.getLegend()); |
| 33 | g.setSelection(1); assertEquals("1: Y: 1", Util.getLegend()); |
| 34 | g.setSelection(2); assertEquals("2: Y: 2", Util.getLegend()); |
| 35 | g.setSelection(3); assertEquals("3: Y: 3", Util.getLegend()); |
| 36 | assertEquals(1, g.rollPeriod()); |
| 37 | |
| 38 | g.updateOptions({rollPeriod: 2}); |
| 39 | g.setSelection(0); assertEquals("0: Y: 0", Util.getLegend()); |
| 40 | g.setSelection(1); assertEquals("1: Y: 0.5", Util.getLegend()); |
| 41 | g.setSelection(2); assertEquals("2: Y: 1.5", Util.getLegend()); |
| 42 | g.setSelection(3); assertEquals("3: Y: 2.5", Util.getLegend()); |
| 43 | assertEquals(2, g.rollPeriod()); |
| 44 | |
| 45 | g.updateOptions({rollPeriod: 3}); |
| 46 | g.setSelection(0); assertEquals("0: Y: 0", Util.getLegend()); |
| 47 | g.setSelection(1); assertEquals("1: Y: 0.5", Util.getLegend()); |
| 48 | g.setSelection(2); assertEquals("2: Y: 1", Util.getLegend()); |
| 49 | g.setSelection(3); assertEquals("3: Y: 2", Util.getLegend()); |
| 50 | assertEquals(3, g.rollPeriod()); |
| 51 | |
| 52 | g.updateOptions({rollPeriod: 4}); |
| 53 | g.setSelection(0); assertEquals("0: Y: 0", Util.getLegend()); |
| 54 | g.setSelection(1); assertEquals("1: Y: 0.5", Util.getLegend()); |
| 55 | g.setSelection(2); assertEquals("2: Y: 1", Util.getLegend()); |
| 56 | g.setSelection(3); assertEquals("3: Y: 1.5", Util.getLegend()); |
| 57 | assertEquals(4, g.rollPeriod()); |
| 58 | }; |
| 59 | |
| 60 | rollingAverageTestCase.prototype.testRollBoxDoesntDisapper = function() { |
| 61 | var opts = { |
| 62 | showRoller: true |
| 63 | }; |
| 64 | var data = "X,Y\n" + |
| 65 | "0,0\n" + |
| 66 | "1,1\n" + |
| 67 | "2,2\n" + |
| 68 | "3,3\n" |
| 69 | ; |
| 70 | |
| 71 | var graph = document.getElementById("graph"); |
| 72 | var g = new Dygraph(graph, data, opts); |
| 73 | |
| 74 | var roll_box = graph.getElementsByTagName("input"); |
| 75 | assertEquals(1, roll_box.length); |
| 76 | assertEquals("1", roll_box[0].value); |
| 77 | |
| 78 | graph.style.width = "500px"; |
| 79 | g.resize(); |
| 80 | assertEquals(1, roll_box.length); |
| 81 | assertEquals("1", roll_box[0].value); |
| 82 | }; |
| 83 | |
| 84 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=426 |
| 85 | rollingAverageTestCase.prototype.testRollShortFractions = function() { |
| 86 | var opts = { |
| 87 | customBars: true, |
| 88 | labels: ['x', 'A'] |
| 89 | }; |
| 90 | var data1 = [ [1, [1, 10, 20]] ]; |
| 91 | var data2 = [ [1, [1, 10, 20]], |
| 92 | [2, [1, 20, 30]], |
| 93 | ]; |
| 94 | |
| 95 | var graph = document.getElementById("graph"); |
| 96 | var g = new Dygraph(graph, data1, opts); |
| 97 | |
| 98 | var rolled1 = g.rollingAverage(data1, 1); |
| 99 | var rolled2 = g.rollingAverage(data2, 1); |
| 100 | |
| 101 | assertEquals(rolled1[0], rolled2[0]); |
| 102 | }; |