Allows that user specifies only one limit of valueRange and let other one to be calcu...
[dygraphs.git] / auto_tests / tests / range_tests.js
1 // Copyright (c) 2011 Google, Inc.
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // THE SOFTWARE.
20
21
22 /**
23 * @fileoverview Test valueRange and dateWindow changes.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27 var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]];
28 var ZERO_TO_FIFTY_STEPS = function() {
29 var a = [];
30 var x = 10;
31 var y = 0;
32 var step = 0;
33 for (step = 0; step <= 50; step++) {
34 a.push([x + (step * .2), y + step]);
35 }
36 return a;
37 } ();
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 ]];
41
42 var RangeTestCase = TestCase("range-tests");
43
44 RangeTestCase.prototype.setUp = function() {
45 document.body.innerHTML = "<div id='graph'></div>";
46 };
47
48 RangeTestCase.prototype.createGraph = function(opts) {
49 var graph = document.getElementById("graph");
50 var g = new Dygraph(graph, ZERO_TO_FIFTY_STEPS, opts);
51
52 assertEquals([10, 20], g.xAxisRange());
53 assertEquals([0, 55], g.yAxisRange(0));
54
55 return g;
56 };
57
58 /**
59 * Test that changes to valueRange and dateWindow are reflected
60 * appropriately.
61 */
62 RangeTestCase.prototype.testRangeSetOperations = function() {
63 var g = this.createGraph({valueRange : [ 0, 55 ]});
64
65 g.updateOptions({ dateWindow : [ 12, 18 ] });
66 assertEquals([12, 18], g.xAxisRange());
67 assertEquals([0, 55], g.yAxisRange(0));
68
69 g.updateOptions({ valueRange : [ 10, 40 ] });
70 assertEquals([12, 18], g.xAxisRange());
71 assertEquals([10, 40], g.yAxisRange(0));
72
73 g.updateOptions({ valueRange: [10, NaN] });
74 assertEquals([12, 18], g.xAxisRange());
75 assertEquals([10, 44.2], g.yAxisRange(0));
76
77 g.updateOptions({ valueRange: [10, 40] });
78 assertEquals([12, 18], g.xAxisRange());
79 assertEquals([10, 40], g.yAxisRange(0));
80
81 g.updateOptions({ });
82 assertEquals([12, 18], g.xAxisRange());
83 assertEquals([10, 40], g.yAxisRange(0));
84
85 g.updateOptions({valueRange : null, axes: {y:{valueRange : [15, 20]}}});
86 assertEquals([12, 18], g.xAxisRange());
87 assertEquals([15, 20], g.yAxisRange(0));
88
89 g.updateOptions({ dateWindow : null, valueRange : null, axes: null });
90 assertEquals([10, 20], g.xAxisRange());
91 assertEquals([0, 55], g.yAxisRange(0));
92 };
93
94 /**
95 * Verify that when zoomed in by mouse operations, an empty call to
96 * updateOptions doesn't change the displayed ranges.
97 */
98 RangeTestCase.prototype.zoom = function(g, xRange, yRange) {
99 var originalXRange = g.xAxisRange();
100 var originalYRange = g.yAxisRange(0);
101
102 DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]);
103 DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary.
104 DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]);
105
106 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
107 // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle.
108
109 var midX = (xRange[1] - xRange[0]) / 2;
110 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
111 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
112 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
113
114 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
115 assertEqualsDelta(yRange, g.yAxisRange(0), 0.2);
116 }
117
118
119 /**
120 * Verify that when zoomed in by mouse operations, an empty call to
121 * updateOptions doesn't change the displayed ranges.
122 */
123 RangeTestCase.prototype.testEmptyUpdateOptions_doesntUnzoom = function() {
124 var g = this.createGraph();
125 this.zoom(g, [ 11, 18 ], [ 35, 40 ]);
126
127 assertEqualsDelta([11, 18], g.xAxisRange(), 0.1);
128 assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2);
129
130 g.updateOptions({});
131
132 assertEqualsDelta([11, 18], g.xAxisRange(), 0.1);
133 assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2);
134 }
135
136 /**
137 * Verify that when zoomed in by mouse operations, a call to
138 * updateOptions({ dateWindow : null, valueRange : null }) fully
139 * unzooms.
140 */
141 RangeTestCase.prototype.testRestoreOriginalRanges_viaUpdateOptions = function() {
142 var g = this.createGraph();
143 this.zoom(g, [ 11, 18 ], [ 35, 40 ]);
144
145 g.updateOptions({ dateWindow : null, valueRange : null });
146
147 assertEquals([0, 55], g.yAxisRange(0));
148 assertEquals([10, 20], g.xAxisRange());
149 }
150
151 /**
152 * Verify that log scale axis range is properly specified.
153 */
154 RangeTestCase.prototype.testLogScaleExcludesZero = function() {
155 var g = new Dygraph("graph", FIVE_TO_ONE_THOUSAND, { logscale : true });
156 assertEquals([10, 1099], g.yAxisRange(0));
157
158 g.updateOptions({ logscale : false });
159 assertEquals([0, 1099], g.yAxisRange(0));
160 }
161
162 /**
163 * Verify that includeZero range is properly specified.
164 */
165 RangeTestCase.prototype.testIncludeZeroIncludesZero = function() {
166 var g = new Dygraph("graph", [[0, 500], [500, 1000]], { includeZero : true });
167 assertEquals([0, 1100], g.yAxisRange(0));
168
169 g.updateOptions({ includeZero : false });
170 assertEquals([450, 1050], g.yAxisRange(0));
171 }
172
173
174 /**
175 * Verify that includeZero range is properly specified per axis.
176 */
177 RangeTestCase.prototype.testIncludeZeroPerAxis = function() {
178 var g = new Dygraph("graph",
179 'X,A,B\n'+
180 '0,50,50\n'+
181 '50,110,110\n',
182 {
183 drawPoints: true,
184 pointSize:5,
185 series:{
186 A: {
187 axis: 'y',
188 pointSize: 10
189 },
190 B: {
191 axis: 'y2'
192 }
193 },
194 axes: {
195 'y2': { includeZero: true }
196 }
197 });
198
199
200 assertEquals([44, 116], g.yAxisRange(0));
201 assertEquals([0, 121], g.yAxisRange(1));
202
203 g.updateOptions({
204 axes: {
205 'y2': { includeZero: false }
206 }
207 });
208
209 assertEquals([44, 116], g.yAxisRange(1));
210 }
211
212
213 /**
214 * Verify that includeZero range is properly specified per axis with old axis options.
215 */
216 RangeTestCase.prototype.testIncludeZeroPerAxisOld = function() {
217 var g = new Dygraph("graph",
218 'X,A,B\n' +
219 '0,50,50\n' +
220 '50,110,110\n',
221 {
222 drawPoints: true,
223 pointSize: 5,
224
225 A: {
226 pointSize: 10
227 },
228 B: {
229 axis: {}
230 },
231 axes: {
232 'y': { includeZero: true },
233 'y2': { includeZero: false }
234 }
235 });
236
237 assertEquals([0, 121], g.yAxisRange(0));
238 assertEquals([44, 116], g.yAxisRange(1));
239
240 g.updateOptions({
241 axes: {
242 'y': { includeZero: false },
243 'y2': { includeZero: true }
244 }
245 });
246
247 assertEquals([44, 116], g.yAxisRange(0));
248 assertEquals([0, 121], g.yAxisRange(1));
249 }
250
251 /**
252 * Verify that very large Y ranges don't break things.
253 */
254 RangeTestCase.prototype.testHugeRange = function() {
255 var g = new Dygraph("graph", [[0, -1e120], [1, 1e230]], { includeZero : true });
256 assertEqualsDelta(1, -1e229 / g.yAxisRange(0)[0], 0.001);
257 assertEqualsDelta(1, 1.1e230 / g.yAxisRange(0)[1], 0.001);
258 }