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