Commit | Line | Data |
---|---|---|
77b5e09d DV |
1 | /** |
2 | * @fileoverview Tests for rolling averages. | |
3 | * | |
4 | * @author danvk@google.com (Dan Vanderkam) | |
5 | */ | |
77b5e09d | 6 | |
e8c70e4e DV |
7 | import Dygraph from '../../src/dygraph'; |
8 | ||
9 | import Util from './Util'; | |
10 | ||
11 | describe("rolling-average", function() { | |
77b5e09d | 12 | |
e8c70e4e | 13 | cleanupAfterEach(); |
77b5e09d | 14 | |
89fdcedb | 15 | it('testRollingAverage', function() { |
77b5e09d DV |
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 | ||
89fdcedb DV |
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()); | |
77b5e09d DV |
37 | |
38 | g.updateOptions({rollPeriod: 2}); | |
89fdcedb DV |
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()); | |
77b5e09d DV |
44 | |
45 | g.updateOptions({rollPeriod: 3}); | |
89fdcedb DV |
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()); | |
77b5e09d DV |
51 | |
52 | g.updateOptions({rollPeriod: 4}); | |
89fdcedb DV |
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() { | |
77b5e09d DV |
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"); | |
89fdcedb DV |
75 | assert.equal(1, roll_box.length); |
76 | assert.equal("1", roll_box[0].value); | |
77b5e09d DV |
77 | |
78 | graph.style.width = "500px"; | |
79 | g.resize(); | |
89fdcedb DV |
80 | assert.equal(1, roll_box.length); |
81 | assert.equal("1", roll_box[0].value); | |
82 | }); | |
77b5e09d | 83 | |
5d2acf29 | 84 | // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=426 |
89fdcedb | 85 | it('testRollShortFractions', function() { |
5d2acf29 DV |
86 | var opts = { |
87 | customBars: true, | |
1b7afc93 | 88 | labels: ['x', 'A', 'B'] |
5d2acf29 | 89 | }; |
a49c164a DE |
90 | var data1 = [ [1, 10, [1, 20]] ]; |
91 | var data2 = [ [1, 10, [1, 20]], | |
92 | [2, 20, [1, 30]], | |
5d2acf29 DV |
93 | ]; |
94 | ||
95 | var graph = document.getElementById("graph"); | |
a49c164a | 96 | var g = new Dygraph(graph, data2, opts); |
5d2acf29 | 97 | |
a49c164a DE |
98 | var rolled1 = g.dataHandler_.rollingAverage(data1, 1, g); |
99 | var rolled2 = g.dataHandler_.rollingAverage(data2, 1, g); | |
5d2acf29 | 100 | |
dc910fce | 101 | assert.deepEqual(rolled1[0], rolled2[0]); |
89fdcedb | 102 | }); |
a49c164a | 103 | |
89fdcedb | 104 | it('testRollCustomBars', function() { |
a49c164a DE |
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); | |
319d0361 | 118 | var rolled = getRolledData(g, data, 1, 2); |
89fdcedb DV |
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 | }); | |
a49c164a | 124 | |
89fdcedb | 125 | it('testRollErrorBars', function() { |
a49c164a DE |
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); | |
319d0361 | 139 | var rolled = getRolledData(g, data, 1, 2); |
89fdcedb | 140 | assert.deepEqual([1, 10, [8, 12]], rolled[0]); |
a49c164a DE |
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; | |
dc910fce DV |
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"); | |
a49c164a | 149 | } |
89fdcedb | 150 | }); |
a49c164a | 151 | |
89fdcedb | 152 | it('testRollFractions', function() { |
a49c164a DE |
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); | |
319d0361 | 166 | var rolled = getRolledData(g, data, 1, 2); |
89fdcedb DV |
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 | }); | |
a49c164a | 172 | |
89fdcedb | 173 | it('testRollFractionsBars', function() { |
a49c164a DE |
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); | |
319d0361 | 189 | var rolled = getRolledData(g, data, 1, 2); |
a49c164a DE |
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++) { | |
dc910fce DV |
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"); | |
a49c164a | 200 | } |
89fdcedb | 201 | }); |
a49c164a | 202 | |
89fdcedb | 203 | it('testRollFractionsBarsWilson', function() { |
a49c164a DE |
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); | |
319d0361 | 219 | var rolled = getRolledData(g, data, 1, 2); |
a49c164a DE |
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++) { | |
dc910fce DV |
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"); | |
a49c164a | 230 | } |
89fdcedb | 231 | }); |
a49c164a | 232 | |
319d0361 | 233 | var getRolledData = function(g, data, seriesIdx, rollPeriod){ |
a49c164a DE |
234 | var options = g.attributes_; |
235 | return g.dataHandler_.rollingAverage(g.dataHandler_.extractSeries(data, seriesIdx, options), rollPeriod, options); | |
236 | }; | |
89fdcedb DV |
237 | |
238 | }); |