Commit | Line | Data |
---|---|---|
718ad8e2 | 1 | // Copyright (c) 2011 Google, Inc. |
72a74f04 RK |
2 | // |
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | |
4 | // of this software and associated documentation files (the "Software"), to deal | |
5 | // in the Software without restriction, including without limitation the rights | |
6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
7 | // copies of the Software, and to permit persons to whom the Software is | |
8 | // furnished to do so, subject to the following conditions: | |
9 | // | |
10 | // The above copyright notice and this permission notice shall be included in | |
11 | // all copies or substantial portions of the Software. | |
12 | // | |
13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
19 | // THE SOFTWARE. | |
20 | ||
21 | ||
22 | /** | |
23 | * @fileoverview Test valueRange and dateWindow changes. | |
24 | * | |
25 | * @author konigsberg@google.com (Robert Konigsberg) | |
26 | */ | |
27 | var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; | |
89fdcedb | 28 | var ZERO_TO_FIFTY_STEPS = (function() { |
72a74f04 RK |
29 | var a = []; |
30 | var x = 10; | |
31 | var y = 0; | |
32 | var step = 0; | |
33 | for (step = 0; step <= 50; step++) { | |
34 | a.push([x + (step * .2), y + step]); | |
35 | } | |
36 | return a; | |
89fdcedb DV |
37 | }()); |
38 | ||
b0d3471d RK |
39 | var FIVE_TO_ONE_THOUSAND = [ |
40 | [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ], | |
41 | [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]]; | |
72a74f04 | 42 | |
89fdcedb | 43 | describe("range-tests", function() { |
72a74f04 | 44 | |
89fdcedb | 45 | beforeEach(function() { |
72a74f04 | 46 | document.body.innerHTML = "<div id='graph'></div>"; |
89fdcedb | 47 | }); |
72a74f04 | 48 | |
319d0361 | 49 | var createGraph = function(opts, data, expectRangeX, expectRangeY) { |
fa460473 KW |
50 | if (data === undefined) data = ZERO_TO_FIFTY_STEPS; |
51 | if (expectRangeX === undefined) expectRangeX = [10, 20]; | |
52 | if (expectRangeY === undefined) expectRangeY = [0, 55]; | |
72a74f04 | 53 | var graph = document.getElementById("graph"); |
fa460473 | 54 | var g = new Dygraph(graph, data, opts); |
72a74f04 | 55 | |
dc910fce DV |
56 | assertDeepCloseTo(expectRangeX, g.xAxisRange(), 0.01); |
57 | assertDeepCloseTo(expectRangeY, g.yAxisRange(0), 0.01); | |
72a74f04 RK |
58 | |
59 | return g; | |
60 | }; | |
61 | ||
62 | /** | |
63 | * Test that changes to valueRange and dateWindow are reflected | |
64 | * appropriately. | |
65 | */ | |
89fdcedb | 66 | it('testRangeSetOperations', function() { |
319d0361 | 67 | var g = createGraph({valueRange : [ 0, 55 ]}); |
72a74f04 RK |
68 | |
69 | g.updateOptions({ dateWindow : [ 12, 18 ] }); | |
89fdcedb DV |
70 | assert.deepEqual([12, 18], g.xAxisRange()); |
71 | assert.deepEqual([0, 55], g.yAxisRange(0)); | |
72a74f04 RK |
72 | |
73 | g.updateOptions({ valueRange : [ 10, 40 ] }); | |
89fdcedb DV |
74 | assert.deepEqual([12, 18], g.xAxisRange()); |
75 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
72a74f04 | 76 | |
e4ddb639 | 77 | g.updateOptions({ valueRange: [10, NaN] }); |
89fdcedb DV |
78 | assert.deepEqual([12, 18], g.xAxisRange()); |
79 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
e4ddb639 | 80 | |
81 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
82 | assert.deepEqual([12, 18], g.xAxisRange()); |
83 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
e4ddb639 | 84 | |
30aae41c | 85 | g.updateOptions({ valueRange: [10, null] }); |
89fdcedb DV |
86 | assert.deepEqual([12, 18], g.xAxisRange()); |
87 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
30aae41c | 88 | |
89 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
90 | assert.deepEqual([12, 18], g.xAxisRange()); |
91 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
30aae41c | 92 | |
93 | g.updateOptions({ valueRange: [10, undefined] }); | |
89fdcedb DV |
94 | assert.deepEqual([12, 18], g.xAxisRange()); |
95 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
30aae41c | 96 | |
97 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
98 | assert.deepEqual([12, 18], g.xAxisRange()); |
99 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
30aae41c | 100 | |
72a74f04 | 101 | g.updateOptions({ }); |
89fdcedb DV |
102 | assert.deepEqual([12, 18], g.xAxisRange()); |
103 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
4dd0ac55 RV |
104 | |
105 | g.updateOptions({valueRange : null, axes: {y:{valueRange : [15, 20]}}}); | |
89fdcedb DV |
106 | assert.deepEqual([12, 18], g.xAxisRange()); |
107 | assert.deepEqual([15, 20], g.yAxisRange(0)); | |
72a74f04 | 108 | |
4dd0ac55 | 109 | g.updateOptions({ dateWindow : null, valueRange : null, axes: null }); |
89fdcedb DV |
110 | assert.deepEqual([10, 20], g.xAxisRange()); |
111 | assert.deepEqual([0, 55], g.yAxisRange(0)); | |
112 | }); | |
72a74f04 RK |
113 | |
114 | /** | |
115 | * Verify that when zoomed in by mouse operations, an empty call to | |
116 | * updateOptions doesn't change the displayed ranges. | |
117 | */ | |
319d0361 | 118 | var zoom = function(g, xRange, yRange) { |
72a74f04 RK |
119 | var originalXRange = g.xAxisRange(); |
120 | var originalYRange = g.yAxisRange(0); | |
121 | ||
a12f271c | 122 | DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]); |
72a74f04 RK |
123 | DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. |
124 | DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); | |
125 | ||
dc910fce DV |
126 | assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); |
127 | // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. | |
72a74f04 RK |
128 | |
129 | var midX = (xRange[1] - xRange[0]) / 2; | |
a12f271c | 130 | DygraphOps.dispatchMouseDown(g, midX, yRange[0]); |
72a74f04 RK |
131 | DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. |
132 | DygraphOps.dispatchMouseUp(g, midX, yRange[1]); | |
133 | ||
dc910fce DV |
134 | assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); |
135 | assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2); | |
72a74f04 RK |
136 | } |
137 | ||
138 | ||
139 | /** | |
140 | * Verify that when zoomed in by mouse operations, an empty call to | |
141 | * updateOptions doesn't change the displayed ranges. | |
142 | */ | |
89fdcedb | 143 | it('testEmptyUpdateOptions_doesntUnzoom', function() { |
319d0361 DV |
144 | var g = createGraph(); |
145 | zoom(g, [ 11, 18 ], [ 35, 40 ]); | |
72a74f04 | 146 | |
dc910fce DV |
147 | assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); |
148 | assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); | |
a12f271c | 149 | |
72a74f04 RK |
150 | g.updateOptions({}); |
151 | ||
dc910fce DV |
152 | assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); |
153 | assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); | |
89fdcedb | 154 | }); |
72a74f04 RK |
155 | |
156 | /** | |
157 | * Verify that when zoomed in by mouse operations, a call to | |
158 | * updateOptions({ dateWindow : null, valueRange : null }) fully | |
159 | * unzooms. | |
160 | */ | |
89fdcedb | 161 | it('testRestoreOriginalRanges_viaUpdateOptions', function() { |
319d0361 DV |
162 | var g = createGraph(); |
163 | zoom(g, [ 11, 18 ], [ 35, 40 ]); | |
72a74f04 RK |
164 | |
165 | g.updateOptions({ dateWindow : null, valueRange : null }); | |
166 | ||
89fdcedb DV |
167 | assert.deepEqual([0, 55], g.yAxisRange(0)); |
168 | assert.deepEqual([10, 20], g.xAxisRange()); | |
169 | }); | |
b0d3471d RK |
170 | |
171 | /** | |
172 | * Verify that log scale axis range is properly specified. | |
173 | */ | |
89fdcedb | 174 | it('testLogScaleExcludesZero', function() { |
b0d3471d | 175 | var g = new Dygraph("graph", FIVE_TO_ONE_THOUSAND, { logscale : true }); |
89fdcedb | 176 | assert.deepEqual([10, 1099], g.yAxisRange(0)); |
b0d3471d RK |
177 | |
178 | g.updateOptions({ logscale : false }); | |
89fdcedb DV |
179 | assert.deepEqual([0, 1099], g.yAxisRange(0)); |
180 | }); | |
478b866b RK |
181 | |
182 | /** | |
183 | * Verify that includeZero range is properly specified. | |
184 | */ | |
89fdcedb | 185 | it('testIncludeZeroIncludesZero', function() { |
478b866b | 186 | var g = new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero : true }); |
89fdcedb | 187 | assert.deepEqual([0, 1100], g.yAxisRange(0)); |
478b866b RK |
188 | |
189 | g.updateOptions({ includeZero : false }); | |
89fdcedb DV |
190 | assert.deepEqual([450, 1050], g.yAxisRange(0)); |
191 | }); | |
fa0d7ad8 | 192 | |
c387c823 | 193 | |
194 | /** | |
195 | * Verify that includeZero range is properly specified per axis. | |
196 | */ | |
89fdcedb | 197 | it('testIncludeZeroPerAxis', function() { |
c387c823 | 198 | var g = new Dygraph("graph", |
199 | 'X,A,B\n'+ | |
200 | '0,50,50\n'+ | |
201 | '50,110,110\n', | |
202 | { | |
203 | drawPoints: true, | |
204 | pointSize:5, | |
205 | series:{ | |
206 | A: { | |
207 | axis: 'y', | |
208 | pointSize: 10 | |
209 | }, | |
210 | B: { | |
211 | axis: 'y2' | |
212 | } | |
213 | }, | |
214 | axes: { | |
215 | 'y2': { includeZero: true } | |
216 | } | |
217 | }); | |
218 | ||
219 | ||
89fdcedb DV |
220 | assert.deepEqual([44, 116], g.yAxisRange(0)); |
221 | assert.deepEqual([0, 121], g.yAxisRange(1)); | |
c387c823 | 222 | |
223 | g.updateOptions({ | |
224 | axes: { | |
225 | 'y2': { includeZero: false } | |
226 | } | |
227 | }); | |
228 | ||
89fdcedb DV |
229 | assert.deepEqual([44, 116], g.yAxisRange(1)); |
230 | }); | |
c387c823 | 231 | |
fa0d7ad8 KW |
232 | /** |
233 | * Verify that very large Y ranges don't break things. | |
234 | */ | |
89fdcedb | 235 | it('testHugeRange', function() { |
fa0d7ad8 | 236 | var g = new Dygraph("graph", [[0, -1e120], [1, 1e230]], { includeZero : true }); |
dc910fce DV |
237 | assert.closeTo(1, -1e229 / g.yAxisRange(0)[0], 0.001); |
238 | assert.closeTo(1, 1.1e230 / g.yAxisRange(0)[1], 0.001); | |
89fdcedb | 239 | }); |
fa460473 KW |
240 | |
241 | /** | |
242 | * Verify old-style avoidMinZero option. | |
243 | */ | |
89fdcedb | 244 | it('testAvoidMinZero', function() { |
319d0361 | 245 | var g = createGraph({ |
fa460473 KW |
246 | avoidMinZero: true, |
247 | }, ZERO_TO_FIFTY_STEPS, [10, 20], [-5, 55]); | |
89fdcedb | 248 | }); |
fa460473 KW |
249 | |
250 | /** | |
251 | * Verify ranges with user-specified padding, implicit avoidMinZero. | |
252 | */ | |
89fdcedb | 253 | it('testPaddingAuto', function() { |
319d0361 | 254 | var g = createGraph({ |
fa460473 KW |
255 | xRangePad: 42, |
256 | yRangePad: 30 | |
257 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); | |
89fdcedb | 258 | }); |
fa460473 KW |
259 | |
260 | /** | |
261 | * Verify auto range with drawAxesAtZero. | |
262 | */ | |
89fdcedb | 263 | it('testPaddingAutoAxisAtZero', function() { |
319d0361 | 264 | var g = createGraph({ |
fa460473 KW |
265 | drawAxesAtZero: true, |
266 | }, ZERO_TO_FIFTY_STEPS, [10, 20], [0, 55]); | |
89fdcedb | 267 | }); |
fa460473 KW |
268 | |
269 | /** | |
270 | * Verify user-specified range with padding and drawAxesAtZero options. | |
271 | * Try explicit range matching the auto range, should have identical results. | |
272 | */ | |
89fdcedb | 273 | it('testPaddingRange1', function() { |
319d0361 | 274 | var g = createGraph({ |
fa460473 KW |
275 | valueRange: [0, 50], |
276 | xRangePad: 42, | |
277 | yRangePad: 30, | |
278 | drawAxesAtZero: true | |
279 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); | |
89fdcedb | 280 | }); |
fa460473 KW |
281 | |
282 | /** | |
283 | * Verify user-specified range with padding and drawAxesAtZero options. | |
284 | * User-supplied range differs from the auto range. | |
285 | */ | |
89fdcedb | 286 | it('testPaddingRange2', function() { |
319d0361 | 287 | var g = createGraph({ |
fa460473 KW |
288 | valueRange: [10, 60], |
289 | xRangePad: 42, | |
290 | yRangePad: 30, | |
291 | drawAxesAtZero: true, | |
292 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [5, 65]); | |
89fdcedb | 293 | }); |
fa460473 KW |
294 | |
295 | /** | |
296 | * Verify drawAxesAtZero and includeZero. | |
297 | */ | |
89fdcedb | 298 | it('testPaddingYAtZero', function() { |
319d0361 | 299 | var g = createGraph({ |
fa460473 KW |
300 | includeZero: true, |
301 | xRangePad: 42, | |
302 | yRangePad: 30, | |
303 | drawAxesAtZero: true, | |
304 | }, [ | |
305 | [-10, 10], | |
306 | [10, 20], | |
307 | [30, 50] | |
308 | ], [-14, 34], [-5, 55]); | |
89fdcedb | 309 | }); |
fa460473 KW |
310 | |
311 | /** | |
312 | * Verify logscale, compat mode. | |
313 | */ | |
89fdcedb | 314 | it('testLogscaleCompat', function() { |
319d0361 | 315 | var g = createGraph({ |
fa460473 KW |
316 | logscale: true |
317 | }, | |
318 | [[-10, 10], [10, 10], [30, 1000]], | |
319 | [-10, 30], [10, 1099]); | |
89fdcedb | 320 | }); |
fa460473 KW |
321 | |
322 | /** | |
323 | * Verify logscale, new mode. | |
324 | */ | |
89fdcedb | 325 | it('testLogscalePad', function() { |
319d0361 | 326 | var g = createGraph({ |
fa460473 KW |
327 | logscale: true, |
328 | yRangePad: 30 | |
329 | }, | |
330 | [[-10, 10], [10, 10], [30, 1000]], | |
331 | [-10, 30], [5.01691, 1993.25801]); | |
89fdcedb | 332 | }); |
ccecde93 KW |
333 | |
334 | /** | |
335 | * Verify scrolling all-zero region, traditional. | |
336 | */ | |
89fdcedb DV |
337 | it('testZeroScroll', function() { |
338 | var g = new Dygraph( | |
ccecde93 KW |
339 | document.getElementById("graph"), |
340 | "X,Y\n" + | |
341 | "1,0\n" + | |
342 | "8,0\n" + | |
343 | "9,0.1\n", | |
344 | { | |
345 | drawAxesAtZero: true, | |
346 | animatedZooms: true, | |
347 | avoidMinZero: true | |
348 | }); | |
89fdcedb | 349 | }); |
ccecde93 KW |
350 | |
351 | /** | |
352 | * Verify scrolling all-zero region, new-style. | |
353 | */ | |
89fdcedb DV |
354 | it('testZeroScroll2', function() { |
355 | var g = new Dygraph( | |
ccecde93 KW |
356 | document.getElementById("graph"), |
357 | "X,Y\n" + | |
358 | "1,0\n" + | |
359 | "8,0\n" + | |
360 | "9,0.1\n", | |
361 | { | |
362 | animatedZooms: true, | |
363 | drawAxesAtZero: true, | |
364 | xRangePad: 4, | |
365 | yRangePad: 4 | |
366 | }); | |
89fdcedb DV |
367 | }); |
368 | ||
369 | }); |