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, 10, [1, 20]] ];
91 var data2
= [ [1, 10, [1, 20]],
95 var graph
= document
.getElementById("graph");
96 var g
= new Dygraph(graph
, data2
, opts
);
98 var rolled1
= g
.dataHandler_
.rollingAverage(data1
, 1, g
);
99 var rolled2
= g
.dataHandler_
.rollingAverage(data2
, 1, g
);
101 assertEquals(rolled1
[0], rolled2
[0]);
104 rollingAverageTestCase
.prototype.testRollCustomBars
= function() {
110 var data
= [ [1, [1, 10, 20]],
116 var graph
= document
.getElementById("graph");
117 var g
= new Dygraph(graph
, data
, opts
);
118 var rolled
= this.getRolledData(g
, data
, 1, 2);
119 assertEquals([1, 10, [1, 20]], rolled
[0]);
120 assertEquals([2, 15, [1, 25]], rolled
[1]);
121 assertEquals([3, 25, [1, 35]], rolled
[2]);
122 assertEquals([4, 35, [1, 45]], rolled
[3]);
125 rollingAverageTestCase
.prototype.testRollErrorBars
= function() {
131 var data
= [ [1, [10, 1]],
137 var graph
= document
.getElementById("graph");
138 var g
= new Dygraph(graph
, data
, opts
);
139 var rolled
= this.getRolledData(g
, data
, 1, 2);
140 assertEquals([1, 10, [8, 12]], rolled
[0]);
142 // variance = sqrt( pow(error) * rollPeriod)
143 var variance
= Math
.sqrt(2);
144 for (var i
=1;i
<data
.length
;i
++) {
145 var value
= data
[i
][1][0] - 5;
146 assertEquals("unexpected rolled average", value
, rolled
[i
][1]);
147 assertEquals("unexpected rolled min", value
- variance
, rolled
[i
][2][0]);
148 assertEquals("unexpected rolled max", value
+ variance
, rolled
[i
][2][1]);
152 rollingAverageTestCase
.prototype.testRollFractions
= function() {
158 var data
= [ [1, [1, 10]],
164 var graph
= document
.getElementById("graph");
165 var g
= new Dygraph(graph
, data
, opts
);
166 var rolled
= this.getRolledData(g
, data
, 1, 2);
167 assertEquals([1, 10], rolled
[0]);
168 assertEquals([2, 15], rolled
[1]);
169 assertEquals([3, 25], rolled
[2]);
170 assertEquals([4, 35], rolled
[3]);
173 rollingAverageTestCase
.prototype.testRollFractionsBars
= function() {
177 wilsonInterval
: false,
181 var data
= [ [1, [1, 10]],
187 var graph
= document
.getElementById("graph");
188 var g
= new Dygraph(graph
, data
, opts
);
189 var rolled
= this.getRolledData(g
, data
, 1, 2);
191 // precalculated rounded values expected
192 var values
= [10, 15, 25, 35];
193 var lows
= [-9, -1, 6, 14];
194 var highs
= [29, 31, 44, 56];
196 for (var i
=0;i
<data
.length
;i
++) {
197 assertEquals("unexpected rolled average", values
[i
], Math
.round(rolled
[i
][1]));
198 assertEquals("unexpected rolled min", lows
[i
], Math
.round(rolled
[i
][2][0]));
199 assertEquals("unexpected rolled max", highs
[i
], Math
.round(rolled
[i
][2][1]));
203 rollingAverageTestCase
.prototype.testRollFractionsBarsWilson
= function() {
207 wilsonInterval
: true,
211 var data
= [ [1, [1, 10]],
217 var graph
= document
.getElementById("graph");
218 var g
= new Dygraph(graph
, data
, opts
);
219 var rolled
= this.getRolledData(g
, data
, 1, 2);
221 //precalculated rounded values expected
222 var values
= [10, 15, 25, 35];
223 var lows
= [2, 5, 11, 18];
224 var highs
= [41, 37, 47, 57];
226 for (var i
=0;i
<data
.length
;i
++) {
227 assertEquals("unexpected rolled average", values
[i
], Math
.round(rolled
[i
][1]));
228 assertEquals("unexpected rolled min", lows
[i
], Math
.round(rolled
[i
][2][0]));
229 assertEquals("unexpected rolled max", highs
[i
], Math
.round(rolled
[i
][2][1]));
233 rollingAverageTestCase
.prototype.getRolledData
= function(g
, data
, seriesIdx
, rollPeriod
){
234 var options
= g
.attributes_
;
235 return g
.dataHandler_
.rollingAverage(g
.dataHandler_
.extractSeries(data
, seriesIdx
, options
), rollPeriod
, options
);