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
) {
49 var graph
= document
.getElementById("graph");
50 var g
= new Dygraph(graph
, ZERO_TO_FIFTY_STEPS
, opts
);
52 assertEquals([10, 20], g
.xAxisRange());
53 assertEquals([0, 55], g
.yAxisRange(0));
59 * Test that changes to valueRange and dateWindow are reflected
62 RangeTestCase
.prototype.testRangeSetOperations
= function() {
63 var g
= this.createGraph({valueRange
: [ 0, 55 ]});
65 g
.updateOptions({ dateWindow
: [ 12, 18 ] });
66 assertEquals([12, 18], g
.xAxisRange());
67 assertEquals([0, 55], g
.yAxisRange(0));
69 g
.updateOptions({ valueRange
: [ 10, 40 ] });
70 assertEquals([12, 18], g
.xAxisRange());
71 assertEquals([10, 40], g
.yAxisRange(0));
74 assertEquals([12, 18], g
.xAxisRange());
75 assertEquals([10, 40], g
.yAxisRange(0));
77 g
.updateOptions({valueRange
: null, axes
: {y
:{valueRange
: [15, 20]}}});
78 assertEquals([12, 18], g
.xAxisRange());
79 assertEquals([15, 20], g
.yAxisRange(0));
81 g
.updateOptions({ dateWindow
: null, valueRange
: null, axes
: null });
82 assertEquals([10, 20], g
.xAxisRange());
83 assertEquals([0, 55], g
.yAxisRange(0));
87 * Verify that when zoomed in by mouse operations, an empty call to
88 * updateOptions doesn't change the displayed ranges.
90 RangeTestCase
.prototype.zoom
= function(g
, xRange
, yRange
) {
91 var originalXRange
= g
.xAxisRange();
92 var originalYRange
= g
.yAxisRange(0);
94 DygraphOps
.dispatchMouseDown(g
, xRange
[0], yRange
[0]);
95 DygraphOps
.dispatchMouseMove(g
, xRange
[1], yRange
[0]); // this is really necessary.
96 DygraphOps
.dispatchMouseUp(g
, xRange
[1], yRange
[0]);
98 assertEqualsDelta(xRange
, g
.xAxisRange(), 0.2);
99 // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
101 var midX = (xRange[1] - xRange[0]) / 2;
102 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
103 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
104 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
106 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
107 assertEqualsDelta(yRange, g.yAxisRange(0), 0.2);
112 * Verify that when zoomed in by mouse operations, an empty call to
113 * updateOptions doesn't change the displayed ranges
.
115 RangeTestCase
.prototype.testEmptyUpdateOptions_doesntUnzoom
= function() {
116 var g
= this.createGraph();
117 this.zoom(g
, [ 11, 18 ], [ 35, 40 ]);
119 assertEqualsDelta([11, 18], g
.xAxisRange(), 0.1);
120 assertEqualsDelta([35, 40], g
.yAxisRange(0), 0.2);
124 assertEqualsDelta([11, 18], g
.xAxisRange(), 0.1);
125 assertEqualsDelta([35, 40], g
.yAxisRange(0), 0.2);
129 * Verify that when zoomed in by mouse operations, a call to
130 * updateOptions({ dateWindow : null, valueRange : null }) fully
133 RangeTestCase
.prototype.testRestoreOriginalRanges_viaUpdateOptions
= function() {
134 var g
= this.createGraph();
135 this.zoom(g
, [ 11, 18 ], [ 35, 40 ]);
137 g
.updateOptions({ dateWindow
: null, valueRange
: null });
139 assertEquals([0, 55], g
.yAxisRange(0));
140 assertEquals([10, 20], g
.xAxisRange());
144 * Verify that log scale axis range is properly specified.
146 RangeTestCase
.prototype.testLogScaleExcludesZero
= function() {
147 var g
= new Dygraph("graph", FIVE_TO_ONE_THOUSAND
, { logscale
: true });
148 assertEquals([10, 1099], g
.yAxisRange(0));
150 g
.updateOptions({ logscale
: false });
151 assertEquals([0, 1099], g
.yAxisRange(0));
155 * Verify that includeZero range is properly specified.
157 RangeTestCase
.prototype.testIncludeZeroIncludesZero
= function() {
158 var g
= new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero
: true });
159 assertEquals([0, 1100], g
.yAxisRange(0));
161 g
.updateOptions({ includeZero
: false });
162 assertEquals([450, 1050], g
.yAxisRange(0));
167 * Verify that includeZero range is properly specified per axis.
169 RangeTestCase
.prototype.testIncludeZeroPerAxis
= function() {
170 var g
= new Dygraph("graph",
187 'y2': { includeZero
: true }
192 assertEquals([44, 116], g
.yAxisRange(0));
193 assertEquals([0, 121], g
.yAxisRange(1));
197 'y2': { includeZero
: false }
201 assertEquals([44, 116], g
.yAxisRange(1));
206 * Verify that includeZero range is properly specified per axis with old axis options.
208 RangeTestCase
.prototype.testIncludeZeroPerAxisOld
= function() {
209 var g
= new Dygraph("graph",
224 'y': { includeZero
: true },
225 'y2': { includeZero
: false }
229 assertEquals([0, 121], g
.yAxisRange(0));
230 assertEquals([44, 116], g
.yAxisRange(1));
234 'y': { includeZero
: false },
235 'y2': { includeZero
: true }
239 assertEquals([44, 116], g
.yAxisRange(0));
240 assertEquals([0, 121], g
.yAxisRange(1));
244 * Verify that very large Y ranges don't break things.
246 RangeTestCase
.prototype.testHugeRange
= function() {
247 var g
= new Dygraph("graph", [[0, -1e120
], [1, 1e230
]], { includeZero
: true });
248 assertEqualsDelta(1, -1e229
/ g
.yAxisRange(0)[0], 0.001);
249 assertEqualsDelta(1, 1.1e230
/ g
.yAxisRange(0)[1], 0.001);