1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Regression tests for range selector.
5 * @author paul.eric.felix@gmail.com (Paul Felix)
7 describe("range-selector", function() {
11 beforeEach(function() {
12 document
.body
.innerHTML
= "<div id='graph'></div>";
13 restoreConsole
= Util
.captureConsole(logs
);
16 afterEach(function() {
20 it('testRangeSelector', function() {
24 showRangeSelector
: true,
38 var graph
= document
.getElementById("graph");
39 var g
= new Dygraph(graph
, data
, opts
);
40 assertGraphExistence(g
, graph
);
43 it('testRangeSelectorWithErrorBars', function() {
48 showRangeSelector
: true,
62 var graph
= document
.getElementById("graph");
63 var g
= new Dygraph(graph
, data
, opts
);
64 assertGraphExistence(g
, graph
);
67 it('testRangeSelectorWithCustomBars', function() {
72 showRangeSelector
: true,
86 var graph
= document
.getElementById("graph");
87 var g
= new Dygraph(graph
, data
, opts
);
88 assertGraphExistence(g
, graph
);
91 it('testRangeSelectorWithLogScale', function() {
96 showRangeSelector
: true,
110 var graph
= document
.getElementById("graph");
111 var g
= new Dygraph(graph
, data
, opts
);
112 assertGraphExistence(g
, graph
);
115 it('testRangeSelectorOptions', function() {
119 showRangeSelector
: true,
120 rangeSelectorHeight
: 30,
121 rangeSelectorPlotFillColor
: 'lightyellow',
122 rangeSelectorPlotFillGradientColor
: 'rgba(200, 200, 42, 10)',
136 var graph
= document
.getElementById("graph");
137 var g
= new Dygraph(graph
, data
, opts
);
138 assertGraphExistence(g
, graph
);
141 it('testAdditionalRangeSelectorOptions', function() {
145 showRangeSelector
: true,
146 rangeSelectorHeight
: 30,
147 rangeSelectorBackgroundStrokeColor
: 'blue',
148 rangeSelectorBackgroundLineWidth
: 3,
149 rangeSelectorPlotLineWidth
: 0.5,
150 rangeSelectorForegroundStrokeColor
: 'red',
151 rangeSelectorForegroundLineWidth
: 2,
152 rangeSelectorAlpha
: 0.8,
166 var graph
= document
.getElementById("graph");
167 var g
= new Dygraph(graph
, data
, opts
);
168 assertGraphExistence(g
, graph
);
171 it('testRangeSelectorEnablingAfterCreation', function() {
188 var graph
= document
.getElementById("graph");
189 var g
= new Dygraph(graph
, data
, opts
);
190 var initialChartHeight
= g
.getArea().h
;
191 g
.updateOptions({showRangeSelector
: true});
192 assertGraphExistence(g
, graph
);
193 assert(g
.getArea().h
< initialChartHeight
); // range selector shown
195 g
.updateOptions({showRangeSelector
: false});
196 assert
.equal(g
.getArea().h
, initialChartHeight
); // range selector hidden
199 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
200 it('testRangeSelectorWithAnimatedZoomsOption', function() {
204 showRangeSelector
: true,
219 var graph
= document
.getElementById("graph");
220 var g
= new Dygraph(graph
, data
, opts
);
221 assertGraphExistence(g
, graph
);
222 assert
.isFalse(g
.getOption('animatedZooms'));
223 assert
.deepEqual(logs
, {
225 warn
: ["Animated zooms and range selector are not compatible; disabling animatedZooms."]
229 it('testRangeSelectorWithAnimatedZoomsOption2', function() {
247 var graph
= document
.getElementById("graph");
248 var g
= new Dygraph(graph
, data
, opts
);
249 g
.updateOptions({showRangeSelector
: true});
250 assertGraphExistence(g
, graph
);
251 assert
.isFalse(g
.getOption('animatedZooms'));
252 assert
.deepEqual(logs
, {
254 warn
: ["Animated zooms and range selector are not compatible; disabling animatedZooms."]
258 it('testRangeSelectorInteraction', function() {
262 showRangeSelector
: true,
276 var graph
= document
.getElementById("graph");
277 var g
= new Dygraph(graph
, data
, opts
);
278 assertGraphExistence(g
, graph
);
279 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
281 // Move left zoomhandle in
282 var xRange
= g
.xAxisRange().slice();
284 var mouseDownEvent
= DygraphOps
.createEvent({
290 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
292 var mouseMoveEvent
= DygraphOps
.createEvent({
297 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
299 var mouseUpEvent
= DygraphOps
.createEvent({
305 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
307 var newXRange
= g
.xAxisRange().slice();
308 assert(newXRange
[0] > xRange
[0], 'left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0]);
309 assert
.equal(xRange
[1], newXRange
[1], 'right zoomhandle should not have moved');
311 // Move right zoomhandle in
314 mouseDownEvent
= DygraphOps
.createEvent({
320 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
322 mouseMoveEvent
= DygraphOps
.createEvent({
327 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
329 mouseUpEvent
= DygraphOps
.createEvent({
335 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
337 var newXRange
= g
.xAxisRange().slice();
338 assert(newXRange
[1] < xRange
[1], 'right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1]);
339 assert
.equal(xRange
[0], newXRange
[0], 'left zoomhandle should not have moved');
343 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
344 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
345 var y
= parseInt(zoomhandles
[0].style
.top
);
347 mouseDownEvent
= DygraphOps
.createEvent({
353 fgcanvas
.dispatchEvent(mouseDownEvent
);
357 mouseMoveEvent
= DygraphOps
.createEvent({
362 fgcanvas
.dispatchEvent(mouseMoveEvent
);
364 mouseUpEvent
= DygraphOps
.createEvent({
370 fgcanvas
.dispatchEvent(mouseUpEvent
);
372 var newXRange
= g
.xAxisRange().slice();
373 assert(newXRange
[0] < xRange
[0], newXRange
[0]+'<'+xRange
[0]);
374 assert(newXRange
[1] < xRange
[1], newXRange
[1]+'<'+xRange
[1]);
378 it('testRangeSelectorPositionIfXAxisNotDrawn', function() {
383 axes
: { x
: { drawAxis
: false }},
384 showRangeSelector
: true,
385 rangeSelectorHeight
: 30,
392 var graph
= document
.getElementById("graph");
393 var g
= new Dygraph(graph
, data
, opts
);
395 //assert, that the range selector is at top position 70 since the 30px of the
396 // xAxis shouldn't be reserved since it isn't drawn.
397 assertGraphExistence(g
, graph
);
398 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
399 assert
.equal("70px", bgcanvas
.style
.top
, "Range selector is not at the expected position.");
400 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
401 assert
.equal("70px", fgcanvas
.style
.top
, "Range selector is not at the expected position.");
404 it('testMiniPlotDrawn', function() {
405 // Install Proxy to track canvas calls.
406 var origFunc
= Dygraph
.getContext
;
408 Dygraph
.getContext
= function(canvas
) {
409 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
410 return origFunc(canvas
);
412 miniHtx
= new Proxy(origFunc(canvas
));
420 axes
: { x
: { drawAxis
: false }},
421 showRangeSelector
: true,
422 rangeSelectorHeight
: 30,
423 rangeSelectorPlotStrokeColor
: '#ff0000',
431 var graph
= document
.getElementById("graph");
432 var g
= new Dygraph(graph
, data
, opts
);
434 // TODO(danvk): more precise tests.
435 assert
.isNotNull(miniHtx
);
436 assert
.isTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
438 Dygraph
.getContext
= origFunc
;
441 // Tests data computation for the mini plot with a single series.
442 it('testSingleCombinedSeries', function() {
444 showRangeSelector
: true,
452 var graph
= document
.getElementById("graph");
453 var g
= new Dygraph(graph
, data
, opts
);
455 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
456 assert
.isNotNull(rangeSelector
);
458 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
460 yMin
: 1 - 7 * 0.25, // 25% padding
471 // Tests that multiple series are averaged for the miniplot.
472 it('testCombinedSeries', function() {
474 showRangeSelector
: true,
475 labels
: ['X', 'Y1', 'Y2']
478 [0, 1, 3], // average = 2
479 [5, 4, 6], // average = 5
480 [10, 7, 9] // average = 8
482 var graph
= document
.getElementById("graph");
483 var g
= new Dygraph(graph
, data
, opts
);
485 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
486 assert
.isNotNull(rangeSelector
);
488 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
490 yMin
: 2 - 6 * 0.25, // 25% padding on combined series range.
500 // Tests selection of a specific series to average for the mini plot.
501 it('testSelectedCombinedSeries', function() {
503 showRangeSelector
: true,
504 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4'],
506 'Y1': { showInRangeSelector
: true },
507 'Y3': { showInRangeSelector
: true }
511 [0, 5, 8, 13, 21], // average (first and third) = 9
512 [5, 1, 3, 7, 14], // average (first and third) = 4
513 [10, 0, 19, 10, 6] // average (first and third) = 5
515 var graph
= document
.getElementById("graph");
516 var g
= new Dygraph(graph
, data
, opts
);
518 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
519 assert
.isNotNull(rangeSelector
);
521 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
523 yMin
: 4 - 5 * 0.25, // 25% padding on combined series range.
533 // Tests data computation for the mini plot with a single error bar series.
534 it('testSingleCombinedSeriesCustomBars', function() {
537 showRangeSelector
: true,
541 [0, [0, 1, 2]], // [low, value, high]
545 var graph
= document
.getElementById("graph");
546 var g
= new Dygraph(graph
, data
, opts
);
548 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
549 assert
.isNotNull(rangeSelector
);
551 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
553 yMin
: 1 - 7 * 0.25, // 25% padding
563 it('testSingleCombinedSeriesErrorBars', function() {
566 showRangeSelector
: true,
570 [0, [1, 1]], // [value, standard deviation]
574 var graph
= document
.getElementById("graph");
575 var g
= new Dygraph(graph
, data
, opts
);
577 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
578 assert
.isNotNull(rangeSelector
);
580 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
582 yMin
: 1 - 7 * 0.25, // 25% padding
592 // Tests data computation for the mini plot with two custom bar series.
593 it('testTwoCombinedSeriesCustomBars', function() {
596 showRangeSelector
: true,
597 labels
: ['X', 'Y1', 'Y2']
600 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
601 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
602 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
604 var graph
= document
.getElementById("graph");
605 var g
= new Dygraph(graph
, data
, opts
);
607 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
608 assert
.isNotNull(rangeSelector
);
610 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
612 yMin
: 3 - 7 * 0.25, // 25% padding
623 var assertGraphExistence
= function(g
, graph
) {
625 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
626 assert
.equal(2, zoomhandles
.length
);
627 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
628 assert
.equal(1, bgcanvas
.length
);
629 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
630 assert
.equal(1, fgcanvas
.length
);