2 * @fileoverview Tests for rolling averages.
4 * @author danvk@google.com (Dan Vanderkam)
6 var rollingAverageTestCase
= TestCase("rolling-average");
8 rollingAverageTestCase
.prototype.setUp
= function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 rollingAverageTestCase
.prototype.tearDown
= function() {
15 rollingAverageTestCase
.prototype.testRollingAverage
= function() {
29 var graph
= document
.getElementById("graph");
30 var g
= new Dygraph(graph
, data
, opts
);
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());
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());
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());
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());
60 rollingAverageTestCase
.prototype.testRollBoxDoesntDisapper
= function() {
71 var graph
= document
.getElementById("graph");
72 var g
= new Dygraph(graph
, data
, opts
);
74 var roll_box
= graph
.getElementsByTagName("input");
75 assertEquals(1, roll_box
.length
);
76 assertEquals("1", roll_box
[0].value
);
78 graph
.style
.width
= "500px";
80 assertEquals(1, roll_box
.length
);
81 assertEquals("1", roll_box
[0].value
);
84 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=426
85 rollingAverageTestCase
.prototype.testRollShortFractions
= function() {
90 var data1
= [ [1, [1, 10, 20]] ];
91 var data2
= [ [1, [1, 10, 20]],
95 var graph
= document
.getElementById("graph");
96 var g
= new Dygraph(graph
, data1
, opts
);
98 var rolled1
= g
.rollingAverage(data1
, 1);
99 var rolled2
= g
.rollingAverage(data2
, 1);
101 assertEquals(rolled1
[0], rolled2
[0]);