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];
53 var graph
= document
.getElementById("graph");
54 var g
= new Dygraph(graph
, data
, opts
);
56 assertDeepCloseTo(expectRangeX
, g
.xAxisRange(), 0.01);
57 assertDeepCloseTo(expectRangeY
, g
.yAxisRange(0), 0.01);
63 * Test that changes to valueRange and dateWindow are reflected
66 it('testRangeSetOperations', function() {
67 var g
= createGraph({valueRange
: [ 0, 55 ]});
69 g
.updateOptions({ dateWindow
: [ 12, 18 ] });
70 assert
.deepEqual([12, 18], g
.xAxisRange());
71 assert
.deepEqual([0, 55], g
.yAxisRange(0));
73 g
.updateOptions({ valueRange
: [ 10, 40 ] });
74 assert
.deepEqual([12, 18], g
.xAxisRange());
75 assert
.deepEqual([10, 40], g
.yAxisRange(0));
77 g
.updateOptions({ valueRange
: [10, NaN
] });
78 assert
.deepEqual([12, 18], g
.xAxisRange());
79 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
81 g
.updateOptions({ valueRange
: [10, 40] });
82 assert
.deepEqual([12, 18], g
.xAxisRange());
83 assert
.deepEqual([10, 40], g
.yAxisRange(0));
85 g
.updateOptions({ valueRange
: [10, null] });
86 assert
.deepEqual([12, 18], g
.xAxisRange());
87 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
89 g
.updateOptions({ valueRange
: [10, 40] });
90 assert
.deepEqual([12, 18], g
.xAxisRange());
91 assert
.deepEqual([10, 40], g
.yAxisRange(0));
93 g
.updateOptions({ valueRange
: [10, undefined
] });
94 assert
.deepEqual([12, 18], g
.xAxisRange());
95 assert
.deepEqual([10, 44.2], g
.yAxisRange(0));
97 g
.updateOptions({ valueRange
: [10, 40] });
98 assert
.deepEqual([12, 18], g
.xAxisRange());
99 assert
.deepEqual([10, 40], g
.yAxisRange(0));
101 g
.updateOptions({ });
102 assert
.deepEqual([12, 18], g
.xAxisRange());
103 assert
.deepEqual([10, 40], g
.yAxisRange(0));
105 g
.updateOptions({valueRange
: null, axes
: {y
:{valueRange
: [15, 20]}}});
106 assert
.deepEqual([12, 18], g
.xAxisRange());
107 assert
.deepEqual([15, 20], g
.yAxisRange(0));
109 g
.updateOptions({ dateWindow
: null, valueRange
: null, axes
: null });
110 assert
.deepEqual([10, 20], g
.xAxisRange());
111 assert
.deepEqual([0, 55], g
.yAxisRange(0));
115 * Verify that when zoomed in by mouse operations, an empty call to
116 * updateOptions doesn't change the displayed ranges.
118 var zoom
= function(g
, xRange
, yRange
) {
119 var originalXRange
= g
.xAxisRange();
120 var originalYRange
= g
.yAxisRange(0);
122 DygraphOps
.dispatchMouseDown(g
, xRange
[0], yRange
[0]);
123 DygraphOps
.dispatchMouseMove(g
, xRange
[1], yRange
[0]); // this is really necessary.
124 DygraphOps
.dispatchMouseUp(g
, xRange
[1], yRange
[0]);
126 assertDeepCloseTo(xRange
, g
.xAxisRange(), 0.2);
127 // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
129 var midX = (xRange[1] - xRange[0]) / 2;
130 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
131 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
132 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
134 assertDeepCloseTo(xRange, g.xAxisRange(), 0.2);
135 assertDeepCloseTo(yRange, g.yAxisRange(0), 0.2);
140 * Verify that when zoomed in by mouse operations, an empty call to
141 * updateOptions doesn't change the displayed ranges
.
143 it('testEmptyUpdateOptions_doesntUnzoom', function() {
144 var g
= createGraph();
145 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
147 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
148 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
152 assertDeepCloseTo([11, 18], g
.xAxisRange(), 0.1);
153 assertDeepCloseTo([35, 40], g
.yAxisRange(0), 0.2);
157 * Verify that when zoomed in by mouse operations, a call to
158 * updateOptions({ dateWindow : null, valueRange : null }) fully
161 it('testRestoreOriginalRanges_viaUpdateOptions', function() {
162 var g
= createGraph();
163 zoom(g
, [ 11, 18 ], [ 35, 40 ]);
165 g
.updateOptions({ dateWindow
: null, valueRange
: null });
167 assert
.deepEqual([0, 55], g
.yAxisRange(0));
168 assert
.deepEqual([10, 20], g
.xAxisRange());
172 * Verify that log scale axis range is properly specified.
174 it('testLogScaleExcludesZero', function() {
175 var g
= new Dygraph("graph", FIVE_TO_ONE_THOUSAND
, { logscale
: true });
176 assert
.deepEqual([10, 1099], g
.yAxisRange(0));
178 g
.updateOptions({ logscale
: false });
179 assert
.deepEqual([0, 1099], g
.yAxisRange(0));
183 * Verify that includeZero range is properly specified.
185 it('testIncludeZeroIncludesZero', function() {
186 var g
= new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero
: true });
187 assert
.deepEqual([0, 1100], g
.yAxisRange(0));
189 g
.updateOptions({ includeZero
: false });
190 assert
.deepEqual([450, 1050], g
.yAxisRange(0));
195 * Verify that includeZero range is properly specified per axis.
197 it('testIncludeZeroPerAxis', function() {
198 var g
= new Dygraph("graph",
215 'y2': { includeZero
: true }
220 assert
.deepEqual([44, 116], g
.yAxisRange(0));
221 assert
.deepEqual([0, 121], g
.yAxisRange(1));
225 'y2': { includeZero
: false }
229 assert
.deepEqual([44, 116], g
.yAxisRange(1));
233 * Verify that very large Y ranges don't break things.
235 it('testHugeRange', function() {
236 var g
= new Dygraph("graph", [[0, -1e120
], [1, 1e230
]], { includeZero
: true });
237 assert
.closeTo(1, -1e229
/ g
.yAxisRange(0)[0], 0.001);
238 assert
.closeTo(1, 1.1e230
/ g
.yAxisRange(0)[1], 0.001);
242 * Verify old-style avoidMinZero option.
244 it('testAvoidMinZero', function() {
245 var g
= createGraph({
247 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [-5, 55]);
251 * Verify ranges with user-specified padding, implicit avoidMinZero.
253 it('testPaddingAuto', function() {
254 var g
= createGraph({
257 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
261 * Verify auto range with drawAxesAtZero.
263 it('testPaddingAutoAxisAtZero', function() {
264 var g
= createGraph({
265 drawAxesAtZero
: true,
266 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [0, 55]);
270 * Verify user-specified range with padding and drawAxesAtZero options.
271 * Try explicit range matching the auto range, should have identical results.
273 it('testPaddingRange1', function() {
274 var g
= createGraph({
279 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
283 * Verify user-specified range with padding and drawAxesAtZero options.
284 * User-supplied range differs from the auto range.
286 it('testPaddingRange2', function() {
287 var g
= createGraph({
288 valueRange
: [10, 60],
291 drawAxesAtZero
: true,
292 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [5, 65]);
296 * Verify drawAxesAtZero and includeZero.
298 it('testPaddingYAtZero', function() {
299 var g
= createGraph({
303 drawAxesAtZero
: true,
308 ], [-14, 34], [-5, 55]);
312 * Verify logscale, compat mode.
314 it('testLogscaleCompat', function() {
315 var g
= createGraph({
318 [[-10, 10], [10, 10], [30, 1000]],
319 [-10, 30], [10, 1099]);
323 * Verify logscale, new mode.
325 it('testLogscalePad', function() {
326 var g
= createGraph({
330 [[-10, 10], [10, 10], [30, 1000]],
331 [-10, 30], [5.01691, 1993.25801]);
335 * Verify scrolling all-zero region, traditional.
337 it('testZeroScroll', function() {
339 document
.getElementById("graph"),
345 drawAxesAtZero
: true,
352 * Verify scrolling all-zero region, new-style.
354 it('testZeroScroll2', function() {
356 document
.getElementById("graph"),
363 drawAxesAtZero
: true,