Merge pull request #310 from hulkholden/license
[dygraphs.git] / auto_tests / tests / rolling_average.js
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, 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 assertEquals(rolled1[0], rolled2[0]);
102 };
103
104 rollingAverageTestCase.prototype.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 = 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]);
123 };
124
125 rollingAverageTestCase.prototype.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 = this.getRolledData(g, data, 1, 2);
140 assertEquals([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 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]);
149 }
150 };
151
152 rollingAverageTestCase.prototype.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 = 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]);
171 };
172
173 rollingAverageTestCase.prototype.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 = this.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 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]));
200 }
201 };
202
203 rollingAverageTestCase.prototype.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 = this.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 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]));
230 }
231 };
232
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);
236 };