4b68da20057ab90397f9355ebf0c83cfcaaf2d81
[dygraphs.git] / rolling_average.js
1 /**
2 * @fileoverview Tests for rolling averages.
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
6
7 import Dygraph from '../../src/dygraph';
8
9 import Util from './Util';
10
11 describe("rolling-average", function() {
12
13 cleanupAfterEach();
14
15 it('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); 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());
37
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());
44
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());
51
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());
58 });
59
60 it('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 assert.equal(1, roll_box.length);
76 assert.equal("1", roll_box[0].value);
77
78 graph.style.width = "500px";
79 g.resize();
80 assert.equal(1, roll_box.length);
81 assert.equal("1", roll_box[0].value);
82 });
83
84 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=426
85 it('testRollShortFractions', function() {
86 var opts = {
87 customBars: true,
88 labels: ['x', 'A', 'B']
89 };
90 var data1 = [ [1, 10, [1, 20]] ];
91 var data2 = [ [1, 10, [1, 20]],
92 [2, 20, [1, 30]],
93 ];
94
95 var graph = document.getElementById("graph");
96 var g = new Dygraph(graph, data2, opts);
97
98 var rolled1 = g.dataHandler_.rollingAverage(data1, 1, g);
99 var rolled2 = g.dataHandler_.rollingAverage(data2, 1, g);
100
101 assert.deepEqual(rolled1[0], rolled2[0]);
102 });
103
104 it('testRollCustomBars', function() {
105 var opts = {
106 customBars: true,
107 rollPeriod: 2,
108 labels: ['x', 'A']
109 };
110 var data = [ [1, [1, 10, 20]],
111 [2, [1, 20, 30]],
112 [3, [1, 30, 40]],
113 [4, [1, 40, 50]]
114 ];
115
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]);
123 });
124
125 it('testRollErrorBars', function() {
126 var opts = {
127 errorBars: true,
128 rollPeriod: 2,
129 labels: ['x', 'A']
130 };
131 var data = [ [1, [10, 1]],
132 [2, [20, 1]],
133 [3, [30, 1]],
134 [4, [40, 1]]
135 ];
136
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]);
141
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");
149 }
150 });
151
152 it('testRollFractions', function() {
153 var opts = {
154 fractions: true,
155 rollPeriod: 2,
156 labels: ['x', 'A']
157 };
158 var data = [ [1, [1, 10]],
159 [2, [2, 10]],
160 [3, [3, 10]],
161 [4, [4, 10]]
162 ];
163
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]);
171 });
172
173 it('testRollFractionsBars', function() {
174 var opts = {
175 fractions: true,
176 errorBars: true,
177 wilsonInterval: false,
178 rollPeriod: 2,
179 labels: ['x', 'A']
180 };
181 var data = [ [1, [1, 10]],
182 [2, [2, 10]],
183 [3, [3, 10]],
184 [4, [4, 10]]
185 ];
186
187 var graph = document.getElementById("graph");
188 var g = new Dygraph(graph, data, opts);
189 var rolled = getRolledData(g, data, 1, 2);
190
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];
195
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");
200 }
201 });
202
203 it('testRollFractionsBarsWilson', function() {
204 var opts = {
205 fractions: true,
206 errorBars: true,
207 wilsonInterval: true,
208 rollPeriod: 2,
209 labels: ['x', 'A']
210 };
211 var data = [ [1, [1, 10]],
212 [2, [2, 10]],
213 [3, [3, 10]],
214 [4, [4, 10]]
215 ];
216
217 var graph = document.getElementById("graph");
218 var g = new Dygraph(graph, data, opts);
219 var rolled = getRolledData(g, data, 1, 2);
220
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];
225
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");
230 }
231 });
232
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);
236 };
237
238 });