2 * @fileoverview Tests for rolling averages.
4 * @author danvk@google.com (Dan Vanderkam)
7 import Dygraph from
'../../src/dygraph';
9 import Util from
'./Util';
11 describe("rolling-average", function() {
15 it('testRollingAverage', function() {
29 var graph
= document
.getElementById("graph");
30 var g
= new Dygraph(graph
, data
, opts
);
32 g
.setSelection(0); assert
.equal("0: Y: 0", Util
.getLegend());
33 g
.setSelection(1); assert
.equal("1: Y: 1", Util
.getLegend());
34 g
.setSelection(2); assert
.equal("2: Y: 2", Util
.getLegend());
35 g
.setSelection(3); assert
.equal("3: Y: 3", Util
.getLegend());
36 assert
.equal(1, g
.rollPeriod());
38 g
.updateOptions({rollPeriod
: 2});
39 g
.setSelection(0); assert
.equal("0: Y: 0", Util
.getLegend());
40 g
.setSelection(1); assert
.equal("1: Y: 0.5", Util
.getLegend());
41 g
.setSelection(2); assert
.equal("2: Y: 1.5", Util
.getLegend());
42 g
.setSelection(3); assert
.equal("3: Y: 2.5", Util
.getLegend());
43 assert
.equal(2, g
.rollPeriod());
45 g
.updateOptions({rollPeriod
: 3});
46 g
.setSelection(0); assert
.equal("0: Y: 0", Util
.getLegend());
47 g
.setSelection(1); assert
.equal("1: Y: 0.5", Util
.getLegend());
48 g
.setSelection(2); assert
.equal("2: Y: 1", Util
.getLegend());
49 g
.setSelection(3); assert
.equal("3: Y: 2", Util
.getLegend());
50 assert
.equal(3, g
.rollPeriod());
52 g
.updateOptions({rollPeriod
: 4});
53 g
.setSelection(0); assert
.equal("0: Y: 0", Util
.getLegend());
54 g
.setSelection(1); assert
.equal("1: Y: 0.5", Util
.getLegend());
55 g
.setSelection(2); assert
.equal("2: Y: 1", Util
.getLegend());
56 g
.setSelection(3); assert
.equal("3: Y: 1.5", Util
.getLegend());
57 assert
.equal(4, g
.rollPeriod());
60 it('testRollBoxDoesntDisapper', function() {
71 var graph
= document
.getElementById("graph");
72 var g
= new Dygraph(graph
, data
, opts
);
74 var roll_box
= graph
.getElementsByTagName("input");
75 assert
.equal(1, roll_box
.length
);
76 assert
.equal("1", roll_box
[0].value
);
78 graph
.style
.width
= "500px";
80 assert
.equal(1, roll_box
.length
);
81 assert
.equal("1", roll_box
[0].value
);
84 // Regression test for http://code.google.com/p/dygraphs/issues/detail
?id
=426
85 it('testRollShortFractions', function() {
88 labels
: ['x', 'A', 'B']
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 assert
.deepEqual(rolled1
[0], rolled2
[0]);
104 it('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
= getRolledData(g
, data
, 1, 2);
119 assert
.deepEqual([1, 10, [1, 20]], rolled
[0]);
120 assert
.deepEqual([2, 15, [1, 25]], rolled
[1]);
121 assert
.deepEqual([3, 25, [1, 35]], rolled
[2]);
122 assert
.deepEqual([4, 35, [1, 45]], rolled
[3]);
125 it('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
= getRolledData(g
, data
, 1, 2);
140 assert
.deepEqual([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 assert
.equal(value
, rolled
[i
][1], "unexpected rolled average");
147 assert
.equal(value
- variance
, rolled
[i
][2][0], "unexpected rolled min");
148 assert
.equal(value
+ variance
, rolled
[i
][2][1], "unexpected rolled max");
152 it('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
= getRolledData(g
, data
, 1, 2);
167 assert
.deepEqual([1, 10], rolled
[0]);
168 assert
.deepEqual([2, 15], rolled
[1]);
169 assert
.deepEqual([3, 25], rolled
[2]);
170 assert
.deepEqual([4, 35], rolled
[3]);
173 it('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
= 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 assert
.equal(values
[i
], Math
.round(rolled
[i
][1]), "unexpected rolled average");
198 assert
.equal(lows
[i
], Math
.round(rolled
[i
][2][0]), "unexpected rolled min");
199 assert
.equal(highs
[i
], Math
.round(rolled
[i
][2][1]), "unexpected rolled max");
203 it('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
= 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 assert
.equal(values
[i
], Math
.round(rolled
[i
][1]), "unexpected rolled average");
228 assert
.equal(lows
[i
], Math
.round(rolled
[i
][2][0]), "unexpected rolled min");
229 assert
.equal(highs
[i
], Math
.round(rolled
[i
][2][1]), "unexpected rolled max");
233 var getRolledData
= function(g
, data
, seriesIdx
, rollPeriod
){
234 var options
= g
.attributes_
;
235 return g
.dataHandler_
.rollingAverage(g
.dataHandler_
.extractSeries(data
, seriesIdx
, options
), rollPeriod
, options
);