1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Regression tests for range selector.
5 * @author paul.eric.felix@gmail.com (Paul Felix)
8 import Dygraph from
'../../src/dygraph';
9 import * as utils from
'../../src/dygraph-utils';
10 import RangeSelectorPlugin from
'../../src/plugins/range-selector';
12 import Util from
'./Util';
13 import DygraphOps from
'./DygraphOps';
14 import CanvasAssertions from
'./CanvasAssertions';
15 import Proxy from
'./Proxy';
17 describe("range-selector", function() {
23 beforeEach(function() {
24 restoreConsole
= Util
.captureConsole(logs
);
27 afterEach(function() {
31 it('testRangeSelector', function() {
35 showRangeSelector
: true,
49 var graph
= document
.getElementById("graph");
50 var g
= new Dygraph(graph
, data
, opts
);
51 assertGraphExistence(g
, graph
);
54 it('testRangeSelectorWithErrorBars', function() {
59 showRangeSelector
: true,
73 var graph
= document
.getElementById("graph");
74 var g
= new Dygraph(graph
, data
, opts
);
75 assertGraphExistence(g
, graph
);
78 it('testRangeSelectorWithCustomBars', function() {
83 showRangeSelector
: true,
97 var graph
= document
.getElementById("graph");
98 var g
= new Dygraph(graph
, data
, opts
);
99 assertGraphExistence(g
, graph
);
102 it('testRangeSelectorWithLogScale', function() {
107 showRangeSelector
: true,
121 var graph
= document
.getElementById("graph");
122 var g
= new Dygraph(graph
, data
, opts
);
123 assertGraphExistence(g
, graph
);
126 it('testRangeSelectorOptions', function() {
130 showRangeSelector
: true,
131 rangeSelectorHeight
: 30,
132 rangeSelectorPlotFillColor
: 'lightyellow',
133 rangeSelectorPlotFillGradientColor
: 'rgba(200, 200, 42, 10)',
147 var graph
= document
.getElementById("graph");
148 var g
= new Dygraph(graph
, data
, opts
);
149 assertGraphExistence(g
, graph
);
152 it('testAdditionalRangeSelectorOptions', function() {
156 showRangeSelector
: true,
157 rangeSelectorHeight
: 30,
158 rangeSelectorBackgroundStrokeColor
: 'blue',
159 rangeSelectorBackgroundLineWidth
: 3,
160 rangeSelectorPlotLineWidth
: 0.5,
161 rangeSelectorForegroundStrokeColor
: 'red',
162 rangeSelectorForegroundLineWidth
: 2,
163 rangeSelectorAlpha
: 0.8,
177 var graph
= document
.getElementById("graph");
178 var g
= new Dygraph(graph
, data
, opts
);
179 assertGraphExistence(g
, graph
);
182 it('testRangeSelectorEnablingAfterCreation', function() {
199 var graph
= document
.getElementById("graph");
200 var g
= new Dygraph(graph
, data
, opts
);
201 var initialChartHeight
= g
.getArea().h
;
202 g
.updateOptions({showRangeSelector
: true});
203 assertGraphExistence(g
, graph
);
204 assert(g
.getArea().h
< initialChartHeight
); // range selector shown
206 g
.updateOptions({showRangeSelector
: false});
207 assert
.equal(g
.getArea().h
, initialChartHeight
); // range selector hidden
210 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
211 it('testRangeSelectorWithAnimatedZoomsOption', function() {
215 showRangeSelector
: true,
230 var graph
= document
.getElementById("graph");
231 var g
= new Dygraph(graph
, data
, opts
);
232 assertGraphExistence(g
, graph
);
233 assert
.isFalse(g
.getOption('animatedZooms'));
234 assert
.deepEqual(logs
, {
236 warn
: ["Animated zooms and range selector are not compatible; disabling animatedZooms."]
240 it('testRangeSelectorWithAnimatedZoomsOption2', function() {
258 var graph
= document
.getElementById("graph");
259 var g
= new Dygraph(graph
, data
, opts
);
260 g
.updateOptions({showRangeSelector
: true});
261 assertGraphExistence(g
, graph
);
262 assert
.isFalse(g
.getOption('animatedZooms'));
263 assert
.deepEqual(logs
, {
265 warn
: ["Animated zooms and range selector are not compatible; disabling animatedZooms."]
269 it('testRangeSelectorInteraction', function() {
273 showRangeSelector
: true,
287 var graph
= document
.getElementById("graph");
288 var g
= new Dygraph(graph
, data
, opts
);
289 assertGraphExistence(g
, graph
);
290 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
292 // Move left zoomhandle in
293 var xRange
= g
.xAxisRange().slice();
295 var mouseDownEvent
= DygraphOps
.createEvent({
301 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
303 var mouseMoveEvent
= DygraphOps
.createEvent({
308 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
310 var mouseUpEvent
= DygraphOps
.createEvent({
316 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
318 var newXRange
= g
.xAxisRange().slice();
319 assert(newXRange
[0] > xRange
[0], 'left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0]);
320 assert
.equal(xRange
[1], newXRange
[1], 'right zoomhandle should not have moved');
322 // Move right zoomhandle in
325 mouseDownEvent
= DygraphOps
.createEvent({
331 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
333 mouseMoveEvent
= DygraphOps
.createEvent({
338 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
340 mouseUpEvent
= DygraphOps
.createEvent({
346 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
348 var newXRange
= g
.xAxisRange().slice();
349 assert(newXRange
[1] < xRange
[1], 'right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1]);
350 assert
.equal(xRange
[0], newXRange
[0], 'left zoomhandle should not have moved');
354 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
355 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
356 var y
= parseInt(zoomhandles
[0].style
.top
);
358 mouseDownEvent
= DygraphOps
.createEvent({
364 fgcanvas
.dispatchEvent(mouseDownEvent
);
368 mouseMoveEvent
= DygraphOps
.createEvent({
373 fgcanvas
.dispatchEvent(mouseMoveEvent
);
375 mouseUpEvent
= DygraphOps
.createEvent({
381 fgcanvas
.dispatchEvent(mouseUpEvent
);
383 var newXRange
= g
.xAxisRange().slice();
384 assert(newXRange
[0] < xRange
[0], newXRange
[0]+'<'+xRange
[0]);
385 assert(newXRange
[1] < xRange
[1], newXRange
[1]+'<'+xRange
[1]);
389 it('testRangeSelectorPositionIfXAxisNotDrawn', function() {
394 axes
: { x
: { drawAxis
: false }},
395 showRangeSelector
: true,
396 rangeSelectorHeight
: 30,
403 var graph
= document
.getElementById("graph");
404 var g
= new Dygraph(graph
, data
, opts
);
406 //assert, that the range selector is at top position 70 since the 30px of the
407 // xAxis shouldn't be reserved since it isn't drawn.
408 assertGraphExistence(g
, graph
);
409 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
410 assert
.equal("70px", bgcanvas
.style
.top
, "Range selector is not at the expected position.");
411 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
412 assert
.equal("70px", fgcanvas
.style
.top
, "Range selector is not at the expected position.");
415 it('testMiniPlotDrawn', function() {
416 // Install Proxy to track canvas calls.
417 var origFunc
= utils
.getContext
;
419 utils
.getContext
= function(canvas
) {
420 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
421 return origFunc(canvas
);
423 miniHtx
= new Proxy(origFunc(canvas
));
431 axes
: { x
: { drawAxis
: false }},
432 showRangeSelector
: true,
433 rangeSelectorHeight
: 30,
434 rangeSelectorPlotStrokeColor
: '#ff0000',
442 var graph
= document
.getElementById("graph");
443 var g
= new Dygraph(graph
, data
, opts
);
445 // TODO(danvk): more precise tests.
446 assert
.isNotNull(miniHtx
);
447 assert
.isTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
449 utils
.getContext
= origFunc
;
452 // Tests data computation for the mini plot with a single series.
453 it('testSingleCombinedSeries', function() {
455 showRangeSelector
: true,
463 var graph
= document
.getElementById("graph");
464 var g
= new Dygraph(graph
, data
, opts
);
466 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
467 assert
.isNotNull(rangeSelector
);
469 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
471 yMin
: 1 - 7 * 0.25, // 25% padding
482 // Tests that multiple series are averaged for the miniplot.
483 it('testCombinedSeries', function() {
485 showRangeSelector
: true,
486 labels
: ['X', 'Y1', 'Y2']
489 [0, 1, 3], // average = 2
490 [5, 4, 6], // average = 5
491 [10, 7, 9] // average = 8
493 var graph
= document
.getElementById("graph");
494 var g
= new Dygraph(graph
, data
, opts
);
496 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
497 assert
.isNotNull(rangeSelector
);
499 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
501 yMin
: 2 - 6 * 0.25, // 25% padding on combined series range.
511 // Tests selection of a specific series to average for the mini plot.
512 it('testSelectedCombinedSeries', function() {
514 showRangeSelector
: true,
515 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4'],
517 'Y1': { showInRangeSelector
: true },
518 'Y3': { showInRangeSelector
: true }
522 [0, 5, 8, 13, 21], // average (first and third) = 9
523 [5, 1, 3, 7, 14], // average (first and third) = 4
524 [10, 0, 19, 10, 6] // average (first and third) = 5
526 var graph
= document
.getElementById("graph");
527 var g
= new Dygraph(graph
, data
, opts
);
529 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
530 assert
.isNotNull(rangeSelector
);
532 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
534 yMin
: 4 - 5 * 0.25, // 25% padding on combined series range.
544 // Tests data computation for the mini plot with a single error bar series.
545 it('testSingleCombinedSeriesCustomBars', function() {
548 showRangeSelector
: true,
552 [0, [0, 1, 2]], // [low, value, high]
556 var graph
= document
.getElementById("graph");
557 var g
= new Dygraph(graph
, data
, opts
);
559 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
560 assert
.isNotNull(rangeSelector
);
562 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
564 yMin
: 1 - 7 * 0.25, // 25% padding
574 it('testSingleCombinedSeriesErrorBars', function() {
577 showRangeSelector
: true,
581 [0, [1, 1]], // [value, standard deviation]
585 var graph
= document
.getElementById("graph");
586 var g
= new Dygraph(graph
, data
, opts
);
588 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
589 assert
.isNotNull(rangeSelector
);
591 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
593 yMin
: 1 - 7 * 0.25, // 25% padding
603 // Tests data computation for the mini plot with two custom bar series.
604 it('testTwoCombinedSeriesCustomBars', function() {
607 showRangeSelector
: true,
608 labels
: ['X', 'Y1', 'Y2']
611 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
612 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
613 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
615 var graph
= document
.getElementById("graph");
616 var g
= new Dygraph(graph
, data
, opts
);
618 var rangeSelector
= g
.getPluginInstance_(RangeSelectorPlugin
);
619 assert
.isNotNull(rangeSelector
);
621 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
623 yMin
: 3 - 7 * 0.25, // 25% padding
634 var assertGraphExistence
= function(g
, graph
) {
636 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
637 assert
.equal(2, zoomhandles
.length
);
638 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
639 assert
.equal(1, bgcanvas
.length
);
640 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
641 assert
.equal(1, fgcanvas
.length
);