2.0.0 release fixes (#815)
[dygraphs.git] / auto_tests / tests / range_tests.js
CommitLineData
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
fd6b8dad 22/**
72a74f04
RK
23 * @fileoverview Test valueRange and dateWindow changes.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
e8c70e4e
DV
27
28import Dygraph from '../../src/dygraph';
29
30import DygraphOps from './DygraphOps';
31import {assertDeepCloseTo} from './custom_asserts';
32
72a74f04 33var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]];
89fdcedb 34var 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 45var FIVE_TO_ONE_THOUSAND = [
fd6b8dad 46 [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ],
b0d3471d 47 [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]];
72a74f04 48
89fdcedb 49describe("range-tests", function() {
72a74f04 50
e8c70e4e 51cleanupAfterEach();
72a74f04 52
319d0361 53var 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 71it('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));
fd6b8dad 109
4dd0ac55 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 123var 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 148it('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 166it('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 179it('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));
fd6b8dad 185
b0d3471d 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 193it('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));
fd6b8dad 199
478b866b 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 208it('testIncludeZeroPerAxis', function() {
fd6b8dad 209 var g = new Dygraph("graph",
c387c823 210 'X,A,B\n'+
211 '0,50,50\n'+
212 '50,110,110\n',
213 {
214 drawPoints: true,
215 pointSize:5,
fd6b8dad 216 series:{
c387c823 217 A: {
218 axis: 'y',
219 pointSize: 10
220 },
221 B: {
222 axis: 'y2'
223 }
fd6b8dad 224 },
c387c823 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.
fd6b8dad 245 */
89fdcedb 246it('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/**
fa460473
KW
256 * Verify ranges with user-specified padding, implicit avoidMinZero.
257 */
89fdcedb 258it('testPaddingAuto', function() {
319d0361 259 var g = createGraph({
fa460473
KW
260 xRangePad: 42,
261 yRangePad: 30
262 }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]);
89fdcedb 263});
fa460473
KW
264
265/**
266 * Verify auto range with drawAxesAtZero.
267 */
89fdcedb 268it('testPaddingAutoAxisAtZero', function() {
319d0361 269 var g = createGraph({
fa460473
KW
270 drawAxesAtZero: true,
271 }, ZERO_TO_FIFTY_STEPS, [10, 20], [0, 55]);
89fdcedb 272});
fa460473
KW
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 */
89fdcedb 278it('testPaddingRange1', function() {
319d0361 279 var g = createGraph({
fa460473
KW
280 valueRange: [0, 50],
281 xRangePad: 42,
282 yRangePad: 30,
283 drawAxesAtZero: true
284 }, ZERO_TO_FIFTY_STEPS, [9, 21], [-5, 55]);
89fdcedb 285});
fa460473
KW
286
287/**
288 * Verify user-specified range with padding and drawAxesAtZero options.
289 * User-supplied range differs from the auto range.
290 */
89fdcedb 291it('testPaddingRange2', function() {
319d0361 292 var g = createGraph({
fa460473
KW
293 valueRange: [10, 60],
294 xRangePad: 42,
295 yRangePad: 30,
296 drawAxesAtZero: true,
297 }, ZERO_TO_FIFTY_STEPS, [9, 21], [5, 65]);
89fdcedb 298});
fa460473
KW
299
300/**
301 * Verify drawAxesAtZero and includeZero.
302 */
89fdcedb 303it('testPaddingYAtZero', function() {
319d0361 304 var g = createGraph({
fa460473
KW
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]);
89fdcedb 314});
fa460473
KW
315
316/**
317 * Verify logscale, compat mode.
318 */
89fdcedb 319it('testLogscaleCompat', function() {
319d0361 320 var g = createGraph({
fa460473
KW
321 logscale: true
322 },
323 [[-10, 10], [10, 10], [30, 1000]],
324 [-10, 30], [10, 1099]);
89fdcedb 325});
fa460473
KW
326
327/**
328 * Verify logscale, new mode.
329 */
89fdcedb 330it('testLogscalePad', function() {
319d0361 331 var g = createGraph({
fa460473
KW
332 logscale: true,
333 yRangePad: 30
334 },
335 [[-10, 10], [10, 10], [30, 1000]],
f1d5ee3e 336 [-10, 30], [5.623, 1778.279]);
89fdcedb 337});
ccecde93
KW
338
339/**
ccecde93
KW
340 * Verify scrolling all-zero region, new-style.
341 */
89fdcedb
DV
342it('testZeroScroll2', function() {
343 var g = new Dygraph(
ccecde93
KW
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 });
89fdcedb
DV
355});
356
357});