merge
[dygraphs.git] / auto_tests / tests / range_selector.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4 * @fileoverview Regression tests for range selector.
5 * @author paul.eric.felix@gmail.com (Paul Felix)
6 */
7 var RangeSelectorTestCase = TestCase("range-selector");
8
9 RangeSelectorTestCase.prototype.setUp = function() {
10 document.body.innerHTML = "<div id='graph'></div>";
11 };
12
13 RangeSelectorTestCase.prototype.tearDown = function() {
14 };
15
16 RangeSelectorTestCase.prototype.testRangeSelector = function() {
17 var opts = {
18 width: 480,
19 height: 320,
20 showRangeSelector: true,
21 labels: ['X', 'Y']
22 };
23 var data = [
24 [1, 10],
25 [2, 15],
26 [3, 10],
27 [4, 15],
28 [5, 10],
29 [6, 15],
30 [7, 10],
31 [8, 15],
32 [9, 10]
33 ];
34 var graph = document.getElementById("graph");
35 var g = new Dygraph(graph, data, opts);
36 this.assertGraphExistence(g, graph);
37 };
38
39 RangeSelectorTestCase.prototype.testRangeSelectorWithErrorBars = function() {
40 var opts = {
41 width: 480,
42 height: 320,
43 errorBars: true,
44 showRangeSelector: true,
45 labels: ['X', 'Y']
46 };
47 var data = [
48 [1, [10, 10]],
49 [2, [15, 10]],
50 [3, [10, 10]],
51 [4, [15, 10]],
52 [5, [10, 10]],
53 [6, [15, 20]],
54 [7, [10, 20]],
55 [8, [15, 20]],
56 [9, [10, 20]]
57 ];
58 var graph = document.getElementById("graph");
59 var g = new Dygraph(graph, data, opts);
60 this.assertGraphExistence(g, graph);
61 };
62
63 RangeSelectorTestCase.prototype.testRangeSelectorWithCustomBars = function() {
64 var opts = {
65 width: 480,
66 height: 320,
67 customBars: true,
68 showRangeSelector: true,
69 labels: ['X', 'Y']
70 };
71 var data = [
72 [1, [10, 10, 100]],
73 [2, [15, 20, 110]],
74 [3, [10, 30, 100]],
75 [4, [15, 40, 110]],
76 [5, [10, 120, 100]],
77 [6, [15, 50, 110]],
78 [7, [10, 70, 100]],
79 [8, [15, 90, 110]],
80 [9, [10, 50, 100]]
81 ];
82 var graph = document.getElementById("graph");
83 var g = new Dygraph(graph, data, opts);
84 this.assertGraphExistence(g, graph);
85 };
86
87 RangeSelectorTestCase.prototype.testRangeSelectorWithLogScale = function() {
88 var opts = {
89 width: 480,
90 height: 320,
91 logscale: true,
92 showRangeSelector: true,
93 labels: ['X', 'Y']
94 };
95 var data = [
96 [1, 10],
97 [2, 15],
98 [3, 10],
99 [4, 15],
100 [5, 10],
101 [6, 15],
102 [7, 10],
103 [8, 15],
104 [9, 10]
105 ];
106 var graph = document.getElementById("graph");
107 var g = new Dygraph(graph, data, opts);
108 this.assertGraphExistence(g, graph);
109 };
110
111 RangeSelectorTestCase.prototype.testRangeSelectorOptions = function() {
112 var opts = {
113 width: 480,
114 height: 320,
115 showRangeSelector: true,
116 rangeSelectorHeight: 30,
117 rangeSelectorPlotFillColor: 'lightyellow',
118 rangeSelectorPlotStyleColor: 'yellow',
119 labels: ['X', 'Y']
120 };
121 var data = [
122 [1, 10],
123 [2, 15],
124 [3, 10],
125 [4, 15],
126 [5, 10],
127 [6, 15],
128 [7, 10],
129 [8, 15],
130 [9, 10]
131 ];
132 var graph = document.getElementById("graph");
133 var g = new Dygraph(graph, data, opts);
134 this.assertGraphExistence(g, graph);
135 };
136
137 RangeSelectorTestCase.prototype.testRangeSelectorEnablingAfterCreation = function() {
138 var opts = {
139 width: 480,
140 height: 320,
141 labels: ['X', 'Y']
142 };
143 var data = [
144 [1, 10],
145 [2, 15],
146 [3, 10],
147 [4, 15],
148 [5, 10],
149 [6, 15],
150 [7, 10],
151 [8, 15],
152 [9, 10]
153 ];
154 var graph = document.getElementById("graph");
155 var g = new Dygraph(graph, data, opts);
156 g.updateOptions({showRangeSelector: true});
157 this.assertGraphExistence(g, graph);
158 };
159
160 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
161 RangeSelectorTestCase.prototype.testRangeSelectorWithAnimatedZoomsOption = function() {
162 var opts = {
163 width: 480,
164 height: 320,
165 showRangeSelector: true,
166 animatedZooms: true,
167 labels: ['X', 'Y']
168 };
169 var data = [
170 [1, 10],
171 [2, 15],
172 [3, 10],
173 [4, 15],
174 [5, 10],
175 [6, 15],
176 [7, 10],
177 [8, 15],
178 [9, 10]
179 ];
180 var graph = document.getElementById("graph");
181 var g = new Dygraph(graph, data, opts);
182 this.assertGraphExistence(g, graph);
183 assertFalse(g.getOption('animatedZooms'));
184 };
185
186 RangeSelectorTestCase.prototype.testRangeSelectorWithAnimatedZoomsOption2 = function() {
187 var opts = {
188 width: 480,
189 height: 320,
190 animatedZooms: true,
191 labels: ['X', 'Y']
192 };
193 var data = [
194 [1, 10],
195 [2, 15],
196 [3, 10],
197 [4, 15],
198 [5, 10],
199 [6, 15],
200 [7, 10],
201 [8, 15],
202 [9, 10]
203 ];
204 var graph = document.getElementById("graph");
205 var g = new Dygraph(graph, data, opts);
206 g.updateOptions({showRangeSelector: true});
207 this.assertGraphExistence(g, graph);
208 assertFalse(g.getOption('animatedZooms'));
209 };
210
211 RangeSelectorTestCase.prototype.testRangeSelectorInteraction = function() {
212 var opts = {
213 width: 480,
214 height: 320,
215 showRangeSelector: true,
216 labels: ['X', 'Y']
217 };
218 var data = [
219 [1, 10],
220 [2, 15],
221 [3, 10],
222 [4, 15],
223 [5, 10],
224 [6, 15],
225 [7, 10],
226 [8, 15],
227 [9, 10]
228 ];
229 var graph = document.getElementById("graph");
230 var g = new Dygraph(graph, data, opts);
231 this.assertGraphExistence(g, graph);
232 var zoomhandles = graph.getElementsByClassName('dygraph-rangesel-zoomhandle');
233
234 // Move left zoomhandle in
235 var xRange = g.xAxisRange().slice();
236
237 var mouseDownEvent = DygraphOps.createEvent({
238 type : 'dragstart',
239 detail: 1,
240 clientX : 0,
241 clientY : 0
242 });
243 zoomhandles[0].dispatchEvent(mouseDownEvent);
244
245 var mouseMoveEvent = DygraphOps.createEvent({
246 type : 'mousemove',
247 clientX : 20,
248 clientY : 20
249 });
250 zoomhandles[0].dispatchEvent(mouseMoveEvent);
251
252 var mouseUpEvent = DygraphOps.createEvent({
253 type : 'mouseup',
254 detail: 1,
255 clientX : 20,
256 clientY : 20
257 });
258 zoomhandles[0].dispatchEvent(mouseUpEvent);
259
260 var newXRange = g.xAxisRange().slice();
261 assert('left zoomhandle should have moved: '+newXRange[0]+'>'+xRange[0], newXRange[0] > xRange[0]);
262 assertEquals('right zoomhandle should not have moved', xRange[1], newXRange[1]);
263
264 // Move right zoomhandle in
265 xRange = newXRange;
266
267 mouseDownEvent = DygraphOps.createEvent({
268 type : 'dragstart',
269 detail: 1,
270 clientX : 100,
271 clientY : 100
272 });
273 zoomhandles[1].dispatchEvent(mouseDownEvent);
274
275 mouseMoveEvent = DygraphOps.createEvent({
276 type : 'mousemove',
277 clientX : 80,
278 clientY : 80
279 });
280 zoomhandles[1].dispatchEvent(mouseMoveEvent);
281
282 mouseUpEvent = DygraphOps.createEvent({
283 type : 'mouseup',
284 detail: 1,
285 clientX : 80,
286 clientY : 80
287 });
288 zoomhandles[1].dispatchEvent(mouseUpEvent);
289
290 var newXRange = g.xAxisRange().slice();
291 assert('right zoomhandle should have moved: '+newXRange[1]+'<'+xRange[1], newXRange[1] < xRange[1]);
292 assertEquals('left zoomhandle should not have moved', xRange[0], newXRange[0]);
293
294 // Pan left
295 xRange = newXRange;
296 var fgcanvas = graph.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
297 var x = parseInt(zoomhandles[0].style.left) + 20;
298 var y = parseInt(zoomhandles[0].style.top);
299
300 mouseDownEvent = DygraphOps.createEvent({
301 type : 'mousedown',
302 detail: 1,
303 clientX : x,
304 clientY : y
305 });
306 fgcanvas.dispatchEvent(mouseDownEvent);
307
308 x -= 10;
309
310 mouseMoveEvent = DygraphOps.createEvent({
311 type : 'mousemove',
312 clientX : x,
313 clientY : y
314 });
315 fgcanvas.dispatchEvent(mouseMoveEvent);
316
317 mouseUpEvent = DygraphOps.createEvent({
318 type : 'mouseup',
319 detail: 1,
320 clientX : x,
321 clientY : y
322 });
323 fgcanvas.dispatchEvent(mouseUpEvent);
324
325 var newXRange = g.xAxisRange().slice();
326 assert(newXRange[0]+'<'+xRange[0], newXRange[0] < xRange[0]);
327 assert(newXRange[1]+'<'+xRange[1], newXRange[1] < xRange[1]);
328 };
329
330
331 RangeSelectorTestCase.prototype.testRangeSelectorPositionIfXAxisNotDrawn = function() {
332 var opts = {
333 width: 480,
334 height: 100,
335 xAxisHeight: 30,
336 drawXAxis: false,
337 showRangeSelector: true,
338 rangeSelectorHeight: 30,
339 labels: ['X', 'Y']
340 };
341 var data = [
342 [0, 1],
343 [10, 1]
344 ];
345 var graph = document.getElementById("graph");
346 var g = new Dygraph(graph, data, opts);
347
348 //assert, that the range selector is at top position 70 since the 30px of the
349 // xAxis shouldn't be reserved since it isn't drawn.
350 this.assertGraphExistence(g, graph);
351 var bgcanvas = graph.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
352 assertEquals("Range selector is not at the expected position.","70px", bgcanvas.style.top);
353 var fgcanvas = graph.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
354 assertEquals("Range selector is not at the expected position.","70px", fgcanvas.style.top);
355 };
356
357 RangeSelectorTestCase.prototype.testMiniPlotDrawn = function() {
358 // Install Proxy to track canvas calls.
359 var origFunc = Dygraph.getContext;
360 var miniHtx;
361 Dygraph.getContext = function(canvas) {
362 if (canvas.className != 'dygraph-rangesel-bgcanvas') {
363 return origFunc(canvas);
364 }
365 miniHtx = new Proxy(origFunc(canvas));
366 return miniHtx;
367 };
368
369 var opts = {
370 width: 480,
371 height: 100,
372 xAxisHeight: 30,
373 drawXAxis: false,
374 showRangeSelector: true,
375 rangeSelectorHeight: 30,
376 rangeSelectorPlotStrokeColor: '#ff0000',
377 labels: ['X', 'Y']
378 };
379 var data = [
380 [0, 1],
381 [5, 4],
382 [10, 8]
383 ];
384 var graph = document.getElementById("graph");
385 var g = new Dygraph(graph, data, opts);
386
387 // TODO(danvk): more precise tests.
388 assertTrue(0 < CanvasAssertions.numLinesDrawn(miniHtx, '#ff0000'));
389
390 Dygraph.getContext = origFunc;
391 };
392
393 // Tests data computation for the mini plot with a single series.
394 RangeSelectorTestCase.prototype.testSingleCombinedSeries = function() {
395 var opts = {
396 showRangeSelector: true,
397 labels: ['X', 'Y1']
398 };
399 var data = [
400 [0, 1],
401 [5, 4],
402 [10, 8]
403 ];
404 var graph = document.getElementById("graph");
405 var g = new Dygraph(graph, data, opts);
406
407 var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector);
408 assertNotNull(rangeSelector);
409
410 var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_();
411 assertEquals({
412 yMin: 1 - 7 * 0.25, // 25% padding
413 yMax: 8 + 7 * 0.25,
414 data: [
415 [0, 1],
416 [5, 4],
417 [10, 8]
418 ]
419 }, combinedSeries);
420 };
421
422
423 // Tests that multiple series are averaged for the miniplot.
424 RangeSelectorTestCase.prototype.testCombinedSeries = function() {
425 var opts = {
426 showRangeSelector: true,
427 labels: ['X', 'Y1', 'Y2']
428 };
429 var data = [
430 [0, 1, 3], // average = 2
431 [5, 4, 6], // average = 5
432 [10, 7, 9] // average = 8
433 ];
434 var graph = document.getElementById("graph");
435 var g = new Dygraph(graph, data, opts);
436
437 var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector);
438 assertNotNull(rangeSelector);
439
440 var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_();
441 assertEquals({
442 yMin: 2 - 6 * 0.25, // 25% padding on combined series range.
443 yMax: 8 + 6 * 0.25,
444 data: [
445 [0, 2],
446 [5, 5],
447 [10, 8]
448 ]
449 }, combinedSeries);
450 };
451
452 // Tests data computation for the mini plot with a single error bar series.
453 RangeSelectorTestCase.prototype.testSingleCombinedSeriesCustomBars = function() {
454 var opts = {
455 customBars: true,
456 showRangeSelector: true,
457 labels: ['X', 'Y1']
458 };
459 var data = [
460 [0, [0, 1, 2]], // [low, value, high]
461 [5, [1, 4, 5]],
462 [10, [7, 8, 9]]
463 ];
464 var graph = document.getElementById("graph");
465 var g = new Dygraph(graph, data, opts);
466
467 var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector);
468 assertNotNull(rangeSelector);
469
470 var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_();
471 assertEquals({
472 yMin: 1 - 7 * 0.25, // 25% padding
473 yMax: 8 + 7 * 0.25,
474 data: [
475 [0, 1],
476 [5, 4],
477 [10, 8]
478 ]
479 }, combinedSeries);
480 };
481
482 RangeSelectorTestCase.prototype.testSingleCombinedSeriesErrorBars = function() {
483 var opts = {
484 errorBars: true,
485 showRangeSelector: true,
486 labels: ['X', 'Y1']
487 };
488 var data = [
489 [0, [1, 1]], // [value, standard deviation]
490 [5, [4, 2]],
491 [10, [8, 1]]
492 ];
493 var graph = document.getElementById("graph");
494 var g = new Dygraph(graph, data, opts);
495
496 var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector);
497 assertNotNull(rangeSelector);
498
499 var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_();
500 assertEquals({
501 yMin: 1 - 7 * 0.25, // 25% padding
502 yMax: 8 + 7 * 0.25,
503 data: [
504 [0, 1],
505 [5, 4],
506 [10, 8]
507 ]
508 }, combinedSeries);
509 };
510
511 // Tests data computation for the mini plot with two custom bar series.
512 RangeSelectorTestCase.prototype.testTwoCombinedSeriesCustomBars = function() {
513 var opts = {
514 customBars: true,
515 showRangeSelector: true,
516 labels: ['X', 'Y1', 'Y2']
517 };
518 var data = [
519 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
520 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
521 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
522 ];
523 var graph = document.getElementById("graph");
524 var g = new Dygraph(graph, data, opts);
525
526 var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector);
527 assertNotNull(rangeSelector);
528
529 var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_();
530 assertEquals({
531 yMin: 3 - 7 * 0.25, // 25% padding
532 yMax: 10 + 7 * 0.25,
533 data: [
534 [0, 3],
535 [5, 6],
536 [10, 10]
537 ]
538 }, combinedSeries);
539 };
540
541
542 RangeSelectorTestCase.prototype.assertGraphExistence = function(g, graph) {
543 assertNotNull(g);
544 var zoomhandles = graph.getElementsByClassName('dygraph-rangesel-zoomhandle');
545 assertEquals(2, zoomhandles.length);
546 var bgcanvas = graph.getElementsByClassName('dygraph-rangesel-bgcanvas');
547 assertEquals(1, bgcanvas.length);
548 var fgcanvas = graph.getElementsByClassName('dygraph-rangesel-fgcanvas');
549 assertEquals(1, fgcanvas.length);
550 }