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 | */ | |
e8c70e4e DV |
27 | |
28 | import Dygraph from '../../src/dygraph'; | |
29 | ||
30 | import DygraphOps from './DygraphOps'; | |
31 | import {assertDeepCloseTo} from './custom_asserts'; | |
32 | ||
72a74f04 | 33 | var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; |
89fdcedb | 34 | var ZERO_TO_FIFTY_STEPS = (function() { |
72a74f04 RK |
35 | var a = []; |
36 | var x = 10; | |
37 | var y = 0; | |
38 | var step = 0; | |
39 | for (step = 0; step <= 50; step++) { | |
40 | a.push([x + (step * .2), y + step]); | |
41 | } | |
42 | return a; | |
89fdcedb DV |
43 | }()); |
44 | ||
b0d3471d RK |
45 | var FIVE_TO_ONE_THOUSAND = [ |
46 | [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ], | |
47 | [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]]; | |
72a74f04 | 48 | |
89fdcedb | 49 | describe("range-tests", function() { |
72a74f04 | 50 | |
e8c70e4e | 51 | cleanupAfterEach(); |
72a74f04 | 52 | |
319d0361 | 53 | var createGraph = function(opts, data, expectRangeX, expectRangeY) { |
fa460473 KW |
54 | if (data === undefined) data = ZERO_TO_FIFTY_STEPS; |
55 | if (expectRangeX === undefined) expectRangeX = [10, 20]; | |
56 | if (expectRangeY === undefined) expectRangeY = [0, 55]; | |
65129ba8 DV |
57 | if (!opts) opts = {}; |
58 | opts['labels'] = ['X', 'Y']; | |
59 | var g = new Dygraph('graph', data, opts); | |
72a74f04 | 60 | |
dc910fce DV |
61 | assertDeepCloseTo(expectRangeX, g.xAxisRange(), 0.01); |
62 | assertDeepCloseTo(expectRangeY, g.yAxisRange(0), 0.01); | |
72a74f04 RK |
63 | |
64 | return g; | |
65 | }; | |
66 | ||
67 | /** | |
68 | * Test that changes to valueRange and dateWindow are reflected | |
69 | * appropriately. | |
70 | */ | |
89fdcedb | 71 | it('testRangeSetOperations', function() { |
319d0361 | 72 | var g = createGraph({valueRange : [ 0, 55 ]}); |
72a74f04 RK |
73 | |
74 | g.updateOptions({ dateWindow : [ 12, 18 ] }); | |
89fdcedb DV |
75 | assert.deepEqual([12, 18], g.xAxisRange()); |
76 | assert.deepEqual([0, 55], g.yAxisRange(0)); | |
72a74f04 RK |
77 | |
78 | g.updateOptions({ valueRange : [ 10, 40 ] }); | |
89fdcedb DV |
79 | assert.deepEqual([12, 18], g.xAxisRange()); |
80 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
72a74f04 | 81 | |
e4ddb639 | 82 | g.updateOptions({ valueRange: [10, NaN] }); |
89fdcedb DV |
83 | assert.deepEqual([12, 18], g.xAxisRange()); |
84 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
e4ddb639 | 85 | |
86 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
87 | assert.deepEqual([12, 18], g.xAxisRange()); |
88 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
e4ddb639 | 89 | |
30aae41c | 90 | g.updateOptions({ valueRange: [10, null] }); |
89fdcedb DV |
91 | assert.deepEqual([12, 18], g.xAxisRange()); |
92 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
30aae41c | 93 | |
94 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
95 | assert.deepEqual([12, 18], g.xAxisRange()); |
96 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
30aae41c | 97 | |
98 | g.updateOptions({ valueRange: [10, undefined] }); | |
89fdcedb DV |
99 | assert.deepEqual([12, 18], g.xAxisRange()); |
100 | assert.deepEqual([10, 44.2], g.yAxisRange(0)); | |
30aae41c | 101 | |
102 | g.updateOptions({ valueRange: [10, 40] }); | |
89fdcedb DV |
103 | assert.deepEqual([12, 18], g.xAxisRange()); |
104 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
30aae41c | 105 | |
72a74f04 | 106 | g.updateOptions({ }); |
89fdcedb DV |
107 | assert.deepEqual([12, 18], g.xAxisRange()); |
108 | assert.deepEqual([10, 40], g.yAxisRange(0)); | |
4dd0ac55 RV |
109 | |
110 | g.updateOptions({valueRange : null, axes: {y:{valueRange : [15, 20]}}}); | |
89fdcedb DV |
111 | assert.deepEqual([12, 18], g.xAxisRange()); |
112 | assert.deepEqual([15, 20], g.yAxisRange(0)); | |
72a74f04 | 113 | |
4dd0ac55 | 114 | g.updateOptions({ dateWindow : null, valueRange : null, axes: null }); |
89fdcedb DV |
115 | assert.deepEqual([10, 20], g.xAxisRange()); |
116 | assert.deepEqual([0, 55], g.yAxisRange(0)); | |
117 | }); | |
72a74f04 RK |
118 | |
119 | /** | |
120 | * Verify that when zoomed in by mouse operations, an empty call to | |
121 | * updateOptions doesn't change the displayed ranges. | |
122 | */ | |
319d0361 | 123 | var zoom = function(g, xRange, yRange) { |
72a74f04 RK |
124 | var originalXRange = g.xAxisRange(); |
125 | var originalYRange = g.yAxisRange(0); | |
126 | ||
a12f271c | 127 | DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]); |
72a74f04 RK |
128 | DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. |
129 | DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); | |
130 | ||
dc910fce DV |
131 | assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); |
132 | // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. | |
72a74f04 RK |
133 | |
134 | var midX = (xRange[1] - xRange[0]) / 2; | |
a12f271c | 135 | DygraphOps.dispatchMouseDown(g, midX, yRange[0]); |
72a74f04 RK |
136 | DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. |
137 | DygraphOps.dispatchMouseUp(g, midX, yRange[1]); | |
138 | ||
dc910fce DV |
139 | assertDeepCloseTo(xRange, g.xAxisRange(), 0.2); |
140 | assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2); | |
72a74f04 RK |
141 | } |
142 | ||
143 | ||
144 | /** | |
145 | * Verify that when zoomed in by mouse operations, an empty call to | |
146 | * updateOptions doesn't change the displayed ranges. | |
147 | */ | |
89fdcedb | 148 | it('testEmptyUpdateOptions_doesntUnzoom', function() { |
319d0361 DV |
149 | var g = createGraph(); |
150 | zoom(g, [ 11, 18 ], [ 35, 40 ]); | |
72a74f04 | 151 | |
dc910fce DV |
152 | assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); |
153 | assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); | |
a12f271c | 154 | |
72a74f04 RK |
155 | g.updateOptions({}); |
156 | ||
dc910fce DV |
157 | assertDeepCloseTo([11, 18], g.xAxisRange(), 0.1); |
158 | assertDeepCloseTo([35, 40], g.yAxisRange(0), 0.2); | |
89fdcedb | 159 | }); |
72a74f04 RK |
160 | |
161 | /** | |
162 | * Verify that when zoomed in by mouse operations, a call to | |
163 | * updateOptions({ dateWindow : null, valueRange : null }) fully | |
164 | * unzooms. | |
165 | */ | |
89fdcedb | 166 | it('testRestoreOriginalRanges_viaUpdateOptions', function() { |
319d0361 DV |
167 | var g = createGraph(); |
168 | zoom(g, [ 11, 18 ], [ 35, 40 ]); | |
72a74f04 RK |
169 | |
170 | g.updateOptions({ dateWindow : null, valueRange : null }); | |
171 | ||
89fdcedb DV |
172 | assert.deepEqual([0, 55], g.yAxisRange(0)); |
173 | assert.deepEqual([10, 20], g.xAxisRange()); | |
174 | }); | |
b0d3471d RK |
175 | |
176 | /** | |
177 | * Verify that log scale axis range is properly specified. | |
178 | */ | |
89fdcedb | 179 | it('testLogScaleExcludesZero', function() { |
65129ba8 DV |
180 | var g = new Dygraph("graph", FIVE_TO_ONE_THOUSAND, { |
181 | logscale: true, | |
182 | labels: ['X', 'Y'] | |
183 | }); | |
89fdcedb | 184 | assert.deepEqual([10, 1099], g.yAxisRange(0)); |
b0d3471d RK |
185 | |
186 | g.updateOptions({ logscale : false }); | |
89fdcedb DV |
187 | assert.deepEqual([0, 1099], g.yAxisRange(0)); |
188 | }); | |
478b866b RK |
189 | |
190 | /** | |
191 | * Verify that includeZero range is properly specified. | |
192 | */ | |
89fdcedb | 193 | it('testIncludeZeroIncludesZero', function() { |
65129ba8 DV |
194 | var g = new Dygraph("graph", [[0, 500], [500, 1000]], { |
195 | includeZero: true, | |
196 | labels: ['X', 'Y'] | |
197 | }); | |
89fdcedb | 198 | assert.deepEqual([0, 1100], g.yAxisRange(0)); |
478b866b RK |
199 | |
200 | g.updateOptions({ includeZero : false }); | |
89fdcedb DV |
201 | assert.deepEqual([450, 1050], g.yAxisRange(0)); |
202 | }); | |
fa0d7ad8 | 203 | |
c387c823 | 204 | |
205 | /** | |
206 | * Verify that includeZero range is properly specified per axis. | |
207 | */ | |
89fdcedb | 208 | it('testIncludeZeroPerAxis', function() { |
c387c823 | 209 | var g = new Dygraph("graph", |
210 | 'X,A,B\n'+ | |
211 | '0,50,50\n'+ | |
212 | '50,110,110\n', | |
213 | { | |
214 | drawPoints: true, | |
215 | pointSize:5, | |
216 | series:{ | |
217 | A: { | |
218 | axis: 'y', | |
219 | pointSize: 10 | |
220 | }, | |
221 | B: { | |
222 | axis: 'y2' | |
223 | } | |
224 | }, | |
225 | axes: { | |
226 | 'y2': { includeZero: true } | |
227 | } | |
228 | }); | |
229 | ||
230 | ||
89fdcedb DV |
231 | assert.deepEqual([44, 116], g.yAxisRange(0)); |
232 | assert.deepEqual([0, 121], g.yAxisRange(1)); | |
c387c823 | 233 | |
234 | g.updateOptions({ | |
235 | axes: { | |
236 | 'y2': { includeZero: false } | |
237 | } | |
238 | }); | |
239 | ||
89fdcedb DV |
240 | assert.deepEqual([44, 116], g.yAxisRange(1)); |
241 | }); | |
c387c823 | 242 | |
fa0d7ad8 KW |
243 | /** |
244 | * Verify that very large Y ranges don't break things. | |
245 | */ | |
89fdcedb | 246 | it('testHugeRange', function() { |
65129ba8 DV |
247 | var g = new Dygraph("graph", [[0, -1e120], [1, 1e230]], { |
248 | includeZero: true, | |
249 | labels: ['X', 'Y'] | |
250 | }); | |
dc910fce DV |
251 | assert.closeTo(1, -1e229 / g.yAxisRange(0)[0], 0.001); |
252 | assert.closeTo(1, 1.1e230 / g.yAxisRange(0)[1], 0.001); | |
89fdcedb | 253 | }); |
fa460473 KW |
254 | |
255 | /** | |
256 | * Verify old-style avoidMinZero option. | |
257 | */ | |
89fdcedb | 258 | it('testAvoidMinZero', function() { |
319d0361 | 259 | var g = createGraph({ |
fa460473 KW |
260 | avoidMinZero: true, |
261 | }, ZERO_TO_FIFTY_STEPS, [10, 20], [-5, 55]); | |
89fdcedb | 262 | }); |
fa460473 KW |
263 | |
264 | /** | |
265 | * Verify ranges with user-specified padding, implicit avoidMinZero. | |
266 | */ | |
89fdcedb | 267 | it('testPaddingAuto', function() { |
319d0361 | 268 | var g = createGraph({ |
fa460473 KW |
269 | xRangePad: 42, |
270 | yRangePad: 30 | |
271 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); | |
89fdcedb | 272 | }); |
fa460473 KW |
273 | |
274 | /** | |
275 | * Verify auto range with drawAxesAtZero. | |
276 | */ | |
89fdcedb | 277 | it('testPaddingAutoAxisAtZero', function() { |
319d0361 | 278 | var g = createGraph({ |
fa460473 KW |
279 | drawAxesAtZero: true, |
280 | }, ZERO_TO_FIFTY_STEPS, [10, 20], [0, 55]); | |
89fdcedb | 281 | }); |
fa460473 KW |
282 | |
283 | /** | |
284 | * Verify user-specified range with padding and drawAxesAtZero options. | |
285 | * Try explicit range matching the auto range, should have identical results. | |
286 | */ | |
89fdcedb | 287 | it('testPaddingRange1', function() { |
319d0361 | 288 | var g = createGraph({ |
fa460473 KW |
289 | valueRange: [0, 50], |
290 | xRangePad: 42, | |
291 | yRangePad: 30, | |
292 | drawAxesAtZero: true | |
293 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]); | |
89fdcedb | 294 | }); |
fa460473 KW |
295 | |
296 | /** | |
297 | * Verify user-specified range with padding and drawAxesAtZero options. | |
298 | * User-supplied range differs from the auto range. | |
299 | */ | |
89fdcedb | 300 | it('testPaddingRange2', function() { |
319d0361 | 301 | var g = createGraph({ |
fa460473 KW |
302 | valueRange: [10, 60], |
303 | xRangePad: 42, | |
304 | yRangePad: 30, | |
305 | drawAxesAtZero: true, | |
306 | }, ZERO_TO_FIFTY_STEPS, [9, 21], [5, 65]); | |
89fdcedb | 307 | }); |
fa460473 KW |
308 | |
309 | /** | |
310 | * Verify drawAxesAtZero and includeZero. | |
311 | */ | |
89fdcedb | 312 | it('testPaddingYAtZero', function() { |
319d0361 | 313 | var g = createGraph({ |
fa460473 KW |
314 | includeZero: true, |
315 | xRangePad: 42, | |
316 | yRangePad: 30, | |
317 | drawAxesAtZero: true, | |
318 | }, [ | |
319 | [-10, 10], | |
320 | [10, 20], | |
321 | [30, 50] | |
322 | ], [-14, 34], [-5, 55]); | |
89fdcedb | 323 | }); |
fa460473 KW |
324 | |
325 | /** | |
326 | * Verify logscale, compat mode. | |
327 | */ | |
89fdcedb | 328 | it('testLogscaleCompat', function() { |
319d0361 | 329 | var g = createGraph({ |
fa460473 KW |
330 | logscale: true |
331 | }, | |
332 | [[-10, 10], [10, 10], [30, 1000]], | |
333 | [-10, 30], [10, 1099]); | |
89fdcedb | 334 | }); |
fa460473 KW |
335 | |
336 | /** | |
337 | * Verify logscale, new mode. | |
338 | */ | |
89fdcedb | 339 | it('testLogscalePad', function() { |
319d0361 | 340 | var g = createGraph({ |
fa460473 KW |
341 | logscale: true, |
342 | yRangePad: 30 | |
343 | }, | |
344 | [[-10, 10], [10, 10], [30, 1000]], | |
345 | [-10, 30], [5.01691, 1993.25801]); | |
89fdcedb | 346 | }); |
ccecde93 KW |
347 | |
348 | /** | |
349 | * Verify scrolling all-zero region, traditional. | |
350 | */ | |
89fdcedb DV |
351 | it('testZeroScroll', function() { |
352 | var g = new Dygraph( | |
ccecde93 KW |
353 | document.getElementById("graph"), |
354 | "X,Y\n" + | |
355 | "1,0\n" + | |
356 | "8,0\n" + | |
357 | "9,0.1\n", | |
358 | { | |
359 | drawAxesAtZero: true, | |
360 | animatedZooms: true, | |
361 | avoidMinZero: true | |
362 | }); | |
89fdcedb | 363 | }); |
ccecde93 KW |
364 | |
365 | /** | |
366 | * Verify scrolling all-zero region, new-style. | |
367 | */ | |
89fdcedb DV |
368 | it('testZeroScroll2', function() { |
369 | var g = new Dygraph( | |
ccecde93 KW |
370 | document.getElementById("graph"), |
371 | "X,Y\n" + | |
372 | "1,0\n" + | |
373 | "8,0\n" + | |
374 | "9,0.1\n", | |
375 | { | |
376 | animatedZooms: true, | |
377 | drawAxesAtZero: true, | |
378 | xRangePad: 4, | |
379 | yRangePad: 4 | |
380 | }); | |
89fdcedb DV |
381 | }); |
382 | ||
383 | }); |