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)
27 var ZERO_TO_FIFTY
= [[ 10, 0 ] , [ 20, 50 ]];
28 var ZERO_TO_FIFTY_STEPS
= (function() {
33 for (step
= 0; step
<= 50; step
++) {
34 a
.push([x
+ (step
* .2), y
+ step
]);
39 var FIVE_TO_ONE_THOUSAND
= [
40 [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ],
41 [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]];
43 describe("range-tests", function() {
45 beforeEach(function() {
46 document
.body
.innerHTML
= "<div id='graph'></div>";
49 var createGraph
= function(opts
, data
, expectRangeX
, expectRangeY
) {
50 if (data
=== undefined
) data
= ZERO_TO_FIFTY_STEPS
;
51 if (expectRangeX
=== undefined
) expectRangeX
= [10, 20];
52 if (expectRangeY
=== undefined
) expectRangeY
= [0, 55];
54 opts
['labels'] = ['X', 'Y'];
55 var g
= new Dygraph('graph', data
, opts
);
57 assertDeepCloseTo(expectRangeX
, g
.xAxisRange(), 0.01);
58 assertDeepCloseTo(expectRangeY
, g
.yAxisRange(0), 0.01);
64 * Test that changes to valueRange and dateWindow are reflected
67 it('testRangeSetOperations', function() {
68 var g
= createGraph({valueRange
: [ 0, 55 ]});
70 g
.updateOptions({ dateWindow
: [ 12, 18 ] });
71 assert
.deepEqual([12, 18], g
.xAxisRange());
72 assert
.deepEqual([0, 55], g
.yAxisRange(0));
74 g
.updateOptions({ valueRange
: [ 10, 40 ] });
75 assert
.deepEqual([12, 18], g
.xAxisRange());
76 assert
.deepEqual([10, 40], g
.yAxisRange(0));
78 g
.updateOptions({ valueRange
: [10, NaN
] });
79 assert
.deepEqual([12, 18], g
.xAxisRange());
80 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
82 g
.updateOptions({ valueRange
: [10, 40] });
83 assert
.deepEqual([12, 18], g
.xAxisRange());
84 assert
.deepEqual([10, 40], g
.yAxisRange(0));
86 g
.updateOptions({ valueRange
: [10, null] });
87 assert
.deepEqual([12, 18], g
.xAxisRange());
88 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
90 g
.updateOptions({ valueRange
: [10, 40] });
91 assert
.deepEqual([12, 18], g
.xAxisRange());
92 assert
.deepEqual([10, 40], g
.yAxisRange(0));
94 g
.updateOptions({ valueRange
: [10, undefined
] });
95 assert
.deepEqual([12, 18], g
.xAxisRange());
96 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
98 g
.updateOptions({ valueRange
: [10, 40] });
99 assert
.deepEqual([12, 18], g
.xAxisRange());
100 assert
.deepEqual([10, 40], g
.yAxisRange(0));
102 g
.updateOptions({ });
103 assert
.deepEqual([12, 18], g
.xAxisRange());
104 assert
.deepEqual([10, 40], g
.yAxisRange(0));
106 g
.updateOptions({valueRange
: null, axes
: {y
:{valueRange
: [15, 20]}}});
107 assert
.deepEqual([12, 18], g
.xAxisRange());
108 assert
.deepEqual([15, 20], g
.yAxisRange(0));
110 g
.updateOptions({ dateWindow
: null, valueRange
: null, axes
: null });
111 assert
.deepEqual([10, 20], g
.xAxisRange());
112 assert
.deepEqual([0, 55], g
.yAxisRange(0));
116 * Verify that when zoomed in by mouse operations, an empty call to
117 * updateOptions doesn't change the displayed ranges.
119 var zoom
= function(g
, xRange
, yRange
) {
120 var originalXRange
= g
.xAxisRange();
121 var originalYRange
= g
.yAxisRange(0);
123 DygraphOps
.dispatchMouseDown(g
, xRange
[0], yRange
[0]);
124 DygraphOps
.dispatchMouseMove(g
, xRange
[1], yRange
[0]); // this is really necessary.
125 DygraphOps
.dispatchMouseUp(g
, xRange
[1], yRange
[0]);
127 assertDeepCloseTo(xRange
, g
.xAxisRange(), 0.2);
128 // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
130 var midX = (xRange[1] - xRange[0]) / 2;
131 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
132 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
133 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
135 assertDeepCloseTo(xRange, g.xAxisRange(), 0.2);
136 assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2);
141 * Verify that when zoomed in by mouse operations, an empty call to
142 * updateOptions doesn't change the displayed ranges
.
144 it('testEmptyUpdateOptions_doesntUnzoom', function() {
145 var g
= createGraph();
146 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
148 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
149 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
153 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
154 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
158 * Verify that when zoomed in by mouse operations, a call to
159 * updateOptions({ dateWindow : null, valueRange : null }) fully
162 it('testRestoreOriginalRanges_viaUpdateOptions', function() {
163 var g
= createGraph();
164 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
166 g
.updateOptions({ dateWindow
: null, valueRange
: null });
168 assert
.deepEqual([0, 55], g
.yAxisRange(0));
169 assert
.deepEqual([10, 20], g
.xAxisRange());
173 * Verify that log scale axis range is properly specified.
175 it('testLogScaleExcludesZero', function() {
176 var g
= new Dygraph("graph", FIVE_TO_ONE_THOUSAND
, {
180 assert
.deepEqual([10, 1099], g
.yAxisRange(0));
182 g
.updateOptions({ logscale
: false });
183 assert
.deepEqual([0, 1099], g
.yAxisRange(0));
187 * Verify that includeZero range is properly specified.
189 it('testIncludeZeroIncludesZero', function() {
190 var g
= new Dygraph("graph", [[0, 500], [500, 1000]], {
194 assert
.deepEqual([0, 1100], g
.yAxisRange(0));
196 g
.updateOptions({ includeZero
: false });
197 assert
.deepEqual([450, 1050], g
.yAxisRange(0));
202 * Verify that includeZero range is properly specified per axis.
204 it('testIncludeZeroPerAxis', function() {
205 var g
= new Dygraph("graph",
222 'y2': { includeZero
: true }
227 assert
.deepEqual([44, 116], g
.yAxisRange(0));
228 assert
.deepEqual([0, 121], g
.yAxisRange(1));
232 'y2': { includeZero
: false }
236 assert
.deepEqual([44, 116], g
.yAxisRange(1));
240 * Verify that very large Y ranges don't break things.
242 it('testHugeRange', function() {
243 var g
= new Dygraph("graph", [[0, -1e120
], [1, 1e230
]], {
247 assert
.closeTo(1, -1e229
/ g
.yAxisRange(0)[0], 0.001);
248 assert
.closeTo(1, 1.1e230
/ g
.yAxisRange(0)[1], 0.001);
252 * Verify old-style avoidMinZero option.
254 it('testAvoidMinZero', function() {
255 var g
= createGraph({
257 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [-5, 55]);
261 * Verify ranges with user-specified padding, implicit avoidMinZero.
263 it('testPaddingAuto', function() {
264 var g
= createGraph({
267 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
271 * Verify auto range with drawAxesAtZero.
273 it('testPaddingAutoAxisAtZero', function() {
274 var g
= createGraph({
275 drawAxesAtZero
: true,
276 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [0, 55]);
280 * Verify user-specified range with padding and drawAxesAtZero options.
281 * Try explicit range matching the auto range, should have identical results.
283 it('testPaddingRange1', function() {
284 var g
= createGraph({
289 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
293 * Verify user-specified range with padding and drawAxesAtZero options.
294 * User-supplied range differs from the auto range.
296 it('testPaddingRange2', function() {
297 var g
= createGraph({
298 valueRange
: [10, 60],
301 drawAxesAtZero
: true,
302 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [5, 65]);
306 * Verify drawAxesAtZero and includeZero.
308 it('testPaddingYAtZero', function() {
309 var g
= createGraph({
313 drawAxesAtZero
: true,
318 ], [-14, 34], [-5, 55]);
322 * Verify logscale, compat mode.
324 it('testLogscaleCompat', function() {
325 var g
= createGraph({
328 [[-10, 10], [10, 10], [30, 1000]],
329 [-10, 30], [10, 1099]);
333 * Verify logscale, new mode.
335 it('testLogscalePad', function() {
336 var g
= createGraph({
340 [[-10, 10], [10, 10], [30, 1000]],
341 [-10, 30], [5.01691, 1993.25801]);
345 * Verify scrolling all-zero region, traditional.
347 it('testZeroScroll', function() {
349 document
.getElementById("graph"),
355 drawAxesAtZero
: true,
362 * Verify scrolling all-zero region, new-style.
364 it('testZeroScroll2', function() {
366 document
.getElementById("graph"),
373 drawAxesAtZero
: true,