Merge pull request #715 from simonkwesi/patch-1
[dygraphs.git] / auto_tests / tests / range_tests.js
... / ...
CommitLineData
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
28import Dygraph from '../../src/dygraph';
29
30import DygraphOps from './DygraphOps';
31import {assertDeepCloseTo} from './custom_asserts';
32
33var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]];
34var 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
45var 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
49describe("range-tests", function() {
50
51cleanupAfterEach();
52
53var 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 */
71it('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 */
123var 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 */
148it('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 */
166it('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 */
179it('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 */
193it('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 */
208it('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 */
246it('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 old-style avoidMinZero option.
257 */
258it('testAvoidMinZero', function() {
259 var g = createGraph({
260 avoidMinZero: true,
261 }, ZERO_TO_FIFTY_STEPS, [10, 20], [-5, 55]);
262});
263
264/**
265 * Verify ranges with user-specified padding, implicit avoidMinZero.
266 */
267it('testPaddingAuto', function() {
268 var g = createGraph({
269 xRangePad: 42,
270 yRangePad: 30
271 }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]);
272});
273
274/**
275 * Verify auto range with drawAxesAtZero.
276 */
277it('testPaddingAutoAxisAtZero', function() {
278 var g = createGraph({
279 drawAxesAtZero: true,
280 }, ZERO_TO_FIFTY_STEPS, [10, 20], [0, 55]);
281});
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 */
287it('testPaddingRange1', function() {
288 var g = createGraph({
289 valueRange: [0, 50],
290 xRangePad: 42,
291 yRangePad: 30,
292 drawAxesAtZero: true
293 }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]);
294});
295
296/**
297 * Verify user-specified range with padding and drawAxesAtZero options.
298 * User-supplied range differs from the auto range.
299 */
300it('testPaddingRange2', function() {
301 var g = createGraph({
302 valueRange: [10, 60],
303 xRangePad: 42,
304 yRangePad: 30,
305 drawAxesAtZero: true,
306 }, ZERO_TO_FIFTY_STEPS, [9, 21], [5, 65]);
307});
308
309/**
310 * Verify drawAxesAtZero and includeZero.
311 */
312it('testPaddingYAtZero', function() {
313 var g = createGraph({
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]);
323});
324
325/**
326 * Verify logscale, compat mode.
327 */
328it('testLogscaleCompat', function() {
329 var g = createGraph({
330 logscale: true
331 },
332 [[-10, 10], [10, 10], [30, 1000]],
333 [-10, 30], [10, 1099]);
334});
335
336/**
337 * Verify logscale, new mode.
338 */
339it('testLogscalePad', function() {
340 var g = createGraph({
341 logscale: true,
342 yRangePad: 30
343 },
344 [[-10, 10], [10, 10], [30, 1000]],
345 [-10, 30], [5.01691, 1993.25801]);
346});
347
348/**
349 * Verify scrolling all-zero region, traditional.
350 */
351it('testZeroScroll', function() {
352 var g = new Dygraph(
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 });
363});
364
365/**
366 * Verify scrolling all-zero region, new-style.
367 */
368it('testZeroScroll2', function() {
369 var g = new Dygraph(
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 });
381});
382
383});