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
]);
38 var FIVE_TO_ONE_THOUSAND
= [
39 [ 1, 10 ], [ 2, 20 ], [ 3, 30 ], [ 4, 40 ] , [ 5, 50 ],
40 [ 6, 60 ], [ 7, 70 ], [ 8, 80 ], [ 9, 90 ] , [ 10, 1000 ]];
42 var RangeTestCase
= TestCase("range-tests");
44 RangeTestCase
.prototype.setUp
= function() {
45 document
.body
.innerHTML
= "<div id='graph'></div>";
48 RangeTestCase
.prototype.createGraph
= function(opts
, data
, expectRangeX
, expectRangeY
) {
49 if (data
=== undefined
) data
= ZERO_TO_FIFTY_STEPS
;
50 if (expectRangeX
=== undefined
) expectRangeX
= [10, 20];
51 if (expectRangeY
=== undefined
) expectRangeY
= [0, 55];
52 var graph
= document
.getElementById("graph");
53 var g
= new Dygraph(graph
, data
, opts
);
55 assertEqualsDelta(expectRangeX
, g
.xAxisRange(), 0.01);
56 assertEqualsDelta(expectRangeY
, g
.yAxisRange(0), 0.01);
62 * Test that changes to valueRange and dateWindow are reflected
65 RangeTestCase
.prototype.testRangeSetOperations
= function() {
66 var g
= this.createGraph({valueRange
: [ 0, 55 ]});
68 g
.updateOptions({ dateWindow
: [ 12, 18 ] });
69 assertEquals([12, 18], g
.xAxisRange());
70 assertEquals([0, 55], g
.yAxisRange(0));
72 g
.updateOptions({ valueRange
: [ 10, 40 ] });
73 assertEquals([12, 18], g
.xAxisRange());
74 assertEquals([10, 40], g
.yAxisRange(0));
76 g
.updateOptions({ valueRange
: [10, NaN
] });
77 assertEquals([12, 18], g
.xAxisRange());
78 assertEquals([10, 44.2], g
.yAxisRange(0));
80 g
.updateOptions({ valueRange
: [10, 40] });
81 assertEquals([12, 18], g
.xAxisRange());
82 assertEquals([10, 40], g
.yAxisRange(0));
84 g
.updateOptions({ valueRange
: [10, null] });
85 assertEquals([12, 18], g
.xAxisRange());
86 assertEquals([10, 44.2], g
.yAxisRange(0));
88 g
.updateOptions({ valueRange
: [10, 40] });
89 assertEquals([12, 18], g
.xAxisRange());
90 assertEquals([10, 40], g
.yAxisRange(0));
92 g
.updateOptions({ valueRange
: [10, undefined
] });
93 assertEquals([12, 18], g
.xAxisRange());
94 assertEquals([10, 44.2], g
.yAxisRange(0));
96 g
.updateOptions({ valueRange
: [10, 40] });
97 assertEquals([12, 18], g
.xAxisRange());
98 assertEquals([10, 40], g
.yAxisRange(0));
100 g
.updateOptions({ });
101 assertEquals([12, 18], g
.xAxisRange());
102 assertEquals([10, 40], g
.yAxisRange(0));
104 g
.updateOptions({valueRange
: null, axes
: {y
:{valueRange
: [15, 20]}}});
105 assertEquals([12, 18], g
.xAxisRange());
106 assertEquals([15, 20], g
.yAxisRange(0));
108 g
.updateOptions({ dateWindow
: null, valueRange
: null, axes
: null });
109 assertEquals([10, 20], g
.xAxisRange());
110 assertEquals([0, 55], g
.yAxisRange(0));
114 * Verify that when zoomed in by mouse operations, an empty call to
115 * updateOptions doesn't change the displayed ranges.
117 RangeTestCase
.prototype.zoom
= function(g
, xRange
, yRange
) {
118 var originalXRange
= g
.xAxisRange();
119 var originalYRange
= g
.yAxisRange(0);
121 DygraphOps
.dispatchMouseDown(g
, xRange
[0], yRange
[0]);
122 DygraphOps
.dispatchMouseMove(g
, xRange
[1], yRange
[0]); // this is really necessary.
123 DygraphOps
.dispatchMouseUp(g
, xRange
[1], yRange
[0]);
125 assertEqualsDelta(xRange
, g
.xAxisRange(), 0.2);
126 // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
128 var midX = (xRange[1] - xRange[0]) / 2;
129 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
130 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
131 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
133 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
134 assertEqualsDelta(yRange, g.yAxisRange(0), 0.2);
139 * Verify that when zoomed in by mouse operations, an empty call to
140 * updateOptions doesn't change the displayed ranges
.
142 RangeTestCase
.prototype.testEmptyUpdateOptions_doesntUnzoom
= function() {
143 var g
= this.createGraph();
144 this.zoom(g
, [ 11, 18 ], [ 35, 40 ]);
146 assertEqualsDelta([11, 18], g
.xAxisRange(), 0.1);
147 assertEqualsDelta([35, 40], g
.yAxisRange(0), 0.2);
151 assertEqualsDelta([11, 18], g
.xAxisRange(), 0.1);
152 assertEqualsDelta([35, 40], g
.yAxisRange(0), 0.2);
156 * Verify that when zoomed in by mouse operations, a call to
157 * updateOptions({ dateWindow : null, valueRange : null }) fully
160 RangeTestCase
.prototype.testRestoreOriginalRanges_viaUpdateOptions
= function() {
161 var g
= this.createGraph();
162 this.zoom(g
, [ 11, 18 ], [ 35, 40 ]);
164 g
.updateOptions({ dateWindow
: null, valueRange
: null });
166 assertEquals([0, 55], g
.yAxisRange(0));
167 assertEquals([10, 20], g
.xAxisRange());
171 * Verify that log scale axis range is properly specified.
173 RangeTestCase
.prototype.testLogScaleExcludesZero
= function() {
174 var g
= new Dygraph("graph", FIVE_TO_ONE_THOUSAND
, { logscale
: true });
175 assertEquals([10, 1099], g
.yAxisRange(0));
177 g
.updateOptions({ logscale
: false });
178 assertEquals([0, 1099], g
.yAxisRange(0));
182 * Verify that includeZero range is properly specified.
184 RangeTestCase
.prototype.testIncludeZeroIncludesZero
= function() {
185 var g
= new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero
: true });
186 assertEquals([0, 1100], g
.yAxisRange(0));
188 g
.updateOptions({ includeZero
: false });
189 assertEquals([450, 1050], g
.yAxisRange(0));
194 * Verify that includeZero range is properly specified per axis.
196 RangeTestCase
.prototype.testIncludeZeroPerAxis
= function() {
197 var g
= new Dygraph("graph",
214 'y2': { includeZero
: true }
219 assertEquals([44, 116], g
.yAxisRange(0));
220 assertEquals([0, 121], g
.yAxisRange(1));
224 'y2': { includeZero
: false }
228 assertEquals([44, 116], g
.yAxisRange(1));
233 * Verify that includeZero range is properly specified per axis with old axis options.
235 RangeTestCase
.prototype.testIncludeZeroPerAxisOld
= function() {
236 var g
= new Dygraph("graph",
251 'y': { includeZero
: true },
252 'y2': { includeZero
: false }
256 assertEquals([0, 121], g
.yAxisRange(0));
257 assertEquals([44, 116], g
.yAxisRange(1));
261 'y': { includeZero
: false },
262 'y2': { includeZero
: true }
266 assertEquals([44, 116], g
.yAxisRange(0));
267 assertEquals([0, 121], g
.yAxisRange(1));
271 * Verify that very large Y ranges don't break things.
273 RangeTestCase
.prototype.testHugeRange
= function() {
274 var g
= new Dygraph("graph", [[0, -1e120
], [1, 1e230
]], { includeZero
: true });
275 assertEqualsDelta(1, -1e229
/ g
.yAxisRange(0)[0], 0.001);
276 assertEqualsDelta(1, 1.1e230
/ g
.yAxisRange(0)[1], 0.001);
280 * Verify old-style avoidMinZero option.
282 RangeTestCase
.prototype.testAvoidMinZero
= function() {
283 var g
= this.createGraph({
285 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [-5, 55]);
289 * Verify ranges with user-specified padding, implicit avoidMinZero.
291 RangeTestCase
.prototype.testPaddingAuto
= function() {
292 var g
= this.createGraph({
295 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
299 * Verify auto range with drawAxesAtZero.
301 RangeTestCase
.prototype.testPaddingAutoAxisAtZero
= function() {
302 var g
= this.createGraph({
303 drawAxesAtZero
: true,
304 }, ZERO_TO_FIFTY_STEPS
, [10, 20], [0, 55]);
308 * Verify user-specified range with padding and drawAxesAtZero options.
309 * Try explicit range matching the auto range, should have identical results.
311 RangeTestCase
.prototype.testPaddingRange1
= function() {
312 var g
= this.createGraph({
317 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [-5, 55]);
321 * Verify user-specified range with padding and drawAxesAtZero options.
322 * User-supplied range differs from the auto range.
324 RangeTestCase
.prototype.testPaddingRange2
= function() {
325 var g
= this.createGraph({
326 valueRange
: [10, 60],
329 drawAxesAtZero
: true,
330 }, ZERO_TO_FIFTY_STEPS
, [9, 21], [5, 65]);
334 * Verify drawAxesAtZero and includeZero.
336 RangeTestCase
.prototype.testPaddingYAtZero
= function() {
337 var g
= this.createGraph({
341 drawAxesAtZero
: true,
346 ], [-14, 34], [-5, 55]);
350 * Verify logscale, compat mode.
352 RangeTestCase
.prototype.testLogscaleCompat
= function() {
353 var g
= this.createGraph({
356 [[-10, 10], [10, 10], [30, 1000]],
357 [-10, 30], [10, 1099]);
361 * Verify logscale, new mode.
363 RangeTestCase
.prototype.testLogscalePad
= function() {
364 var g
= this.createGraph({
368 [[-10, 10], [10, 10], [30, 1000]],
369 [-10, 30], [5.01691, 1993.25801]);
373 * Verify scrolling all-zero region, traditional.
375 RangeTestCase
.prototype.testZeroScroll
= function() {
377 document
.getElementById("graph"),
383 drawAxesAtZero
: true,
390 * Verify scrolling all-zero region, new-style.
392 RangeTestCase
.prototype.testZeroScroll2
= function() {
394 document
.getElementById("graph"),
401 drawAxesAtZero
: true,