1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
23 * @fileoverview Test valueRange and dateWindow changes.
25 * @author konigsberg@google.com (Robert Konigsberg)
28 import Dygraph from
'../../src/dygraph';
30 import DygraphOps from
'./DygraphOps';
31 import {assertDeepCloseTo
} from
'./custom_asserts';
33 var ZERO_TO_FIFTY
= [[ 10, 0 ] , [ 20, 50 ]];
34 var ZERO_TO_FIFTY_STEPS
= (function() {
39 for (step
= 0; step
<= 50; step
++) {
40 a
.push([x
+ (step
* .2), y
+ step
]);
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 ]];
49 describe("range-tests", function() {
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];
58 opts
['labels'] = ['X', 'Y'];
59 var g
= new Dygraph('graph', data
, opts
);
61 assertDeepCloseTo(expectRangeX
, g
.xAxisRange(), 0.01);
62 assertDeepCloseTo(expectRangeY
, g
.yAxisRange(0), 0.01);
68 * Test that changes to valueRange and dateWindow are reflected
71 it('testRangeSetOperations', function() {
72 var g
= createGraph({valueRange
: [ 0, 55 ]});
74 g
.updateOptions({ dateWindow
: [ 12, 18 ] });
75 assert
.deepEqual([12, 18], g
.xAxisRange());
76 assert
.deepEqual([0, 55], g
.yAxisRange(0));
78 g
.updateOptions({ valueRange
: [ 10, 40 ] });
79 assert
.deepEqual([12, 18], g
.xAxisRange());
80 assert
.deepEqual([10, 40], g
.yAxisRange(0));
82 g
.updateOptions({ valueRange
: [10, NaN
] });
83 assert
.deepEqual([12, 18], g
.xAxisRange());
84 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
86 g
.updateOptions({ valueRange
: [10, 40] });
87 assert
.deepEqual([12, 18], g
.xAxisRange());
88 assert
.deepEqual([10, 40], g
.yAxisRange(0));
90 g
.updateOptions({ valueRange
: [10, null] });
91 assert
.deepEqual([12, 18], g
.xAxisRange());
92 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
94 g
.updateOptions({ valueRange
: [10, 40] });
95 assert
.deepEqual([12, 18], g
.xAxisRange());
96 assert
.deepEqual([10, 40], g
.yAxisRange(0));
98 g
.updateOptions({ valueRange
: [10, undefined
] });
99 assert
.deepEqual([12, 18], g
.xAxisRange());
100 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
102 g
.updateOptions({ valueRange
: [10, 40] });
103 assert
.deepEqual([12, 18], g
.xAxisRange());
104 assert
.deepEqual([10, 40], g
.yAxisRange(0));
106 g
.updateOptions({ });
107 assert
.deepEqual([12, 18], g
.xAxisRange());
108 assert
.deepEqual([10, 40], g
.yAxisRange(0));
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));
114 g
.updateOptions({ dateWindow
: null, valueRange
: null, axes
: null });
115 assert
.deepEqual([10, 20], g
.xAxisRange());
116 assert
.deepEqual([0, 55], g
.yAxisRange(0));
120 * Verify that when zoomed in by mouse operations, an empty call to
121 * updateOptions doesn't change the displayed ranges.
123 var zoom
= function(g
, xRange
, yRange
) {
124 var originalXRange
= g
.xAxisRange();
125 var originalYRange
= g
.yAxisRange(0);
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]);
131 assertDeepCloseTo(xRange
, g
.xAxisRange(), 0.2);
132 // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
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]);
139 assertDeepCloseTo(xRange, g.xAxisRange(), 0.2);
140 assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2);
145 * Verify that when zoomed in by mouse operations, an empty call to
146 * updateOptions doesn't change the displayed ranges
.
148 it('testEmptyUpdateOptions_doesntUnzoom', function() {
149 var g
= createGraph();
150 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
152 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
153 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
157 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
158 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
162 * Verify that when zoomed in by mouse operations, a call to
163 * updateOptions({ dateWindow : null, valueRange : null }) fully
166 it('testRestoreOriginalRanges_viaUpdateOptions', function() {
167 var g
= createGraph();
168 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
170 g
.updateOptions({ dateWindow
: null, valueRange
: null });
172 assert
.deepEqual([0, 55], g
.yAxisRange(0));
173 assert
.deepEqual([10, 20], g
.xAxisRange());
177 * Verify that log scale axis range is properly specified.
179 it('testLogScaleExcludesZero', function() {
180 var g
= new Dygraph("graph", FIVE_TO_ONE_THOUSAND
, {
184 assert
.deepEqual([10, 1099], g
.yAxisRange(0));
186 g
.updateOptions({ logscale
: false });
187 assert
.deepEqual([0, 1099], g
.yAxisRange(0));
191 * Verify that includeZero range is properly specified.
193 it('testIncludeZeroIncludesZero', function() {
194 var g
= new Dygraph("graph", [[0, 500], [500, 1000]], {
198 assert
.deepEqual([0, 1100], g
.yAxisRange(0));
200 g
.updateOptions({ includeZero
: false });
201 assert
.deepEqual([450, 1050], g
.yAxisRange(0));
206 * Verify that includeZero range is properly specified per axis.
208 it('testIncludeZeroPerAxis', function() {
209 var g
= new Dygraph("graph",
226 'y2': { includeZero
: true }
231 assert
.deepEqual([44, 116], g
.yAxisRange(0));
232 assert
.deepEqual([0, 121], g
.yAxisRange(1));
236 'y2': { includeZero
: false }
240 assert
.deepEqual([44, 116], g
.yAxisRange(1));
244 * Verify that very large Y ranges don't break things.
246 it('testHugeRange', function() {
247 var g
= new Dygraph("graph", [[0, -1e120
], [1, 1e230
]], {
251 assert
.closeTo(1, -1e229
/ g
.yAxisRange(0)[0], 0.001);
252 assert
.closeTo(1, 1.1e230
/ g
.yAxisRange(0)[1], 0.001);
256 * Verify old-style avoidMinZero option.
258 it('testAvoidMinZero', function() {
259 var g
= createGraph({
261 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [-5, 55]);
265 * Verify ranges with user-specified padding, implicit avoidMinZero.
267 it('testPaddingAuto', function() {
268 var g
= createGraph({
271 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
275 * Verify auto range with drawAxesAtZero.
277 it('testPaddingAutoAxisAtZero', function() {
278 var g
= createGraph({
279 drawAxesAtZero
: true,
280 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [0, 55]);
284 * Verify user-specified range with padding and drawAxesAtZero options.
285 * Try explicit range matching the auto range, should have identical results.
287 it('testPaddingRange1', function() {
288 var g
= createGraph({
293 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
297 * Verify user-specified range with padding and drawAxesAtZero options.
298 * User-supplied range differs from the auto range.
300 it('testPaddingRange2', function() {
301 var g
= createGraph({
302 valueRange
: [10, 60],
305 drawAxesAtZero
: true,
306 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [5, 65]);
310 * Verify drawAxesAtZero and includeZero.
312 it('testPaddingYAtZero', function() {
313 var g
= createGraph({
317 drawAxesAtZero
: true,
322 ], [-14, 34], [-5, 55]);
326 * Verify logscale, compat mode.
328 it('testLogscaleCompat', function() {
329 var g
= createGraph({
332 [[-10, 10], [10, 10], [30, 1000]],
333 [-10, 30], [10, 1099]);
337 * Verify logscale, new mode.
339 it('testLogscalePad', function() {
340 var g
= createGraph({
344 [[-10, 10], [10, 10], [30, 1000]],
345 [-10, 30], [5.01691, 1993.25801]);
349 * Verify scrolling all-zero region, traditional.
351 it('testZeroScroll', function() {
353 document
.getElementById("graph"),
359 drawAxesAtZero
: true,
366 * Verify scrolling all-zero region, new-style.
368 it('testZeroScroll2', function() {
370 document
.getElementById("graph"),
377 drawAxesAtZero
: true,