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() {
9 beforeEach(function() {
10 document
.body
.innerHTML
= "<div id='graph'></div>";
13 afterEach(function() {
16 it('testRangeSelector', function() {
20 showRangeSelector
: true,
34 var graph
= document
.getElementById("graph");
35 var g
= new Dygraph(graph
, data
, opts
);
36 assertGraphExistence(g
, graph
);
39 it('testRangeSelectorWithErrorBars', function() {
44 showRangeSelector
: true,
58 var graph
= document
.getElementById("graph");
59 var g
= new Dygraph(graph
, data
, opts
);
60 assertGraphExistence(g
, graph
);
63 it('testRangeSelectorWithCustomBars', function() {
68 showRangeSelector
: true,
82 var graph
= document
.getElementById("graph");
83 var g
= new Dygraph(graph
, data
, opts
);
84 assertGraphExistence(g
, graph
);
87 it('testRangeSelectorWithLogScale', function() {
92 showRangeSelector
: true,
106 var graph
= document
.getElementById("graph");
107 var g
= new Dygraph(graph
, data
, opts
);
108 assertGraphExistence(g
, graph
);
111 it('testRangeSelectorOptions', function() {
115 showRangeSelector
: true,
116 rangeSelectorHeight
: 30,
117 rangeSelectorPlotFillColor
: 'lightyellow',
118 rangeSelectorPlotFillGradientColor
: 'rgba(200, 200, 42, 10)',
132 var graph
= document
.getElementById("graph");
133 var g
= new Dygraph(graph
, data
, opts
);
134 assertGraphExistence(g
, graph
);
137 it('testAdditionalRangeSelectorOptions', function() {
141 showRangeSelector
: true,
142 rangeSelectorHeight
: 30,
143 rangeSelectorBackgroundStrokeColor
: 'blue',
144 rangeSelectorBackgroundLineWidth
: 3,
145 rangeSelectorPlotLineWidth
: 0.5,
146 rangeSelectorForegroundStrokeColor
: 'red',
147 rangeSelectorForegroundLineWidth
: 2,
148 rangeSelectorAlpha
: 0.8,
162 var graph
= document
.getElementById("graph");
163 var g
= new Dygraph(graph
, data
, opts
);
164 assertGraphExistence(g
, graph
);
167 it('testRangeSelectorEnablingAfterCreation', function() {
184 var graph
= document
.getElementById("graph");
185 var g
= new Dygraph(graph
, data
, opts
);
186 var initialChartHeight
= g
.getArea().h
;
187 g
.updateOptions({showRangeSelector
: true});
188 assertGraphExistence(g
, graph
);
189 assert(g
.getArea().h
< initialChartHeight
); // range selector shown
191 g
.updateOptions({showRangeSelector
: false});
192 assert
.equal(g
.getArea().h
, initialChartHeight
); // range selector hidden
195 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
196 it('testRangeSelectorWithAnimatedZoomsOption', function() {
200 showRangeSelector
: true,
215 var graph
= document
.getElementById("graph");
216 var g
= new Dygraph(graph
, data
, opts
);
217 assertGraphExistence(g
, graph
);
218 assert
.isFalse(g
.getOption('animatedZooms'));
221 it('testRangeSelectorWithAnimatedZoomsOption2', function() {
239 var graph
= document
.getElementById("graph");
240 var g
= new Dygraph(graph
, data
, opts
);
241 g
.updateOptions({showRangeSelector
: true});
242 assertGraphExistence(g
, graph
);
243 assert
.isFalse(g
.getOption('animatedZooms'));
246 it('testRangeSelectorInteraction', function() {
250 showRangeSelector
: true,
264 var graph
= document
.getElementById("graph");
265 var g
= new Dygraph(graph
, data
, opts
);
266 assertGraphExistence(g
, graph
);
267 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
269 // Move left zoomhandle in
270 var xRange
= g
.xAxisRange().slice();
272 var mouseDownEvent
= DygraphOps
.createEvent({
278 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
280 var mouseMoveEvent
= DygraphOps
.createEvent({
285 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
287 var mouseUpEvent
= DygraphOps
.createEvent({
293 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
295 var newXRange
= g
.xAxisRange().slice();
296 assert(newXRange
[0] > xRange
[0], 'left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0]);
297 assert
.equal(xRange
[1], newXRange
[1], 'right zoomhandle should not have moved');
299 // Move right zoomhandle in
302 mouseDownEvent
= DygraphOps
.createEvent({
308 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
310 mouseMoveEvent
= DygraphOps
.createEvent({
315 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
317 mouseUpEvent
= DygraphOps
.createEvent({
323 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
325 var newXRange
= g
.xAxisRange().slice();
326 assert(newXRange
[1] < xRange
[1], 'right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1]);
327 assert
.equal(xRange
[0], newXRange
[0], 'left zoomhandle should not have moved');
331 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
332 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
333 var y
= parseInt(zoomhandles
[0].style
.top
);
335 mouseDownEvent
= DygraphOps
.createEvent({
341 fgcanvas
.dispatchEvent(mouseDownEvent
);
345 mouseMoveEvent
= DygraphOps
.createEvent({
350 fgcanvas
.dispatchEvent(mouseMoveEvent
);
352 mouseUpEvent
= DygraphOps
.createEvent({
358 fgcanvas
.dispatchEvent(mouseUpEvent
);
360 var newXRange
= g
.xAxisRange().slice();
361 assert(newXRange
[0] < xRange
[0], newXRange
[0]+'<'+xRange
[0]);
362 assert(newXRange
[1] < xRange
[1], newXRange
[1]+'<'+xRange
[1]);
366 it('testRangeSelectorPositionIfXAxisNotDrawn', function() {
371 axes
: { x
: { drawAxis
: false }},
372 showRangeSelector
: true,
373 rangeSelectorHeight
: 30,
380 var graph
= document
.getElementById("graph");
381 var g
= new Dygraph(graph
, data
, opts
);
383 //assert, that the range selector is at top position 70 since the 30px of the
384 // xAxis shouldn't be reserved since it isn't drawn.
385 assertGraphExistence(g
, graph
);
386 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
387 assert
.equal("70px", bgcanvas
.style
.top
, "Range selector is not at the expected position.");
388 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
389 assert
.equal("70px", fgcanvas
.style
.top
, "Range selector is not at the expected position.");
392 it('testMiniPlotDrawn', function() {
393 // Install Proxy to track canvas calls.
394 var origFunc
= Dygraph
.getContext
;
396 Dygraph
.getContext
= function(canvas
) {
397 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
398 return origFunc(canvas
);
400 miniHtx
= new Proxy(origFunc(canvas
));
408 axes
: { x
: { drawAxis
: false }},
409 showRangeSelector
: true,
410 rangeSelectorHeight
: 30,
411 rangeSelectorPlotStrokeColor
: '#ff0000',
419 var graph
= document
.getElementById("graph");
420 var g
= new Dygraph(graph
, data
, opts
);
422 // TODO(danvk): more precise tests.
423 assert
.isNotNull(miniHtx
);
424 assert
.isTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
426 Dygraph
.getContext
= origFunc
;
429 // Tests data computation for the mini plot with a single series.
430 it('testSingleCombinedSeries', function() {
432 showRangeSelector
: true,
440 var graph
= document
.getElementById("graph");
441 var g
= new Dygraph(graph
, data
, opts
);
443 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
444 assert
.isNotNull(rangeSelector
);
446 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
448 yMin
: 1 - 7 * 0.25, // 25% padding
459 // Tests that multiple series are averaged for the miniplot.
460 it('testCombinedSeries', function() {
462 showRangeSelector
: true,
463 labels
: ['X', 'Y1', 'Y2']
466 [0, 1, 3], // average = 2
467 [5, 4, 6], // average = 5
468 [10, 7, 9] // average = 8
470 var graph
= document
.getElementById("graph");
471 var g
= new Dygraph(graph
, data
, opts
);
473 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
474 assert
.isNotNull(rangeSelector
);
476 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
478 yMin
: 2 - 6 * 0.25, // 25% padding on combined series range.
488 // Tests selection of a specific series to average for the mini plot.
489 it('testSelectedCombinedSeries', function() {
491 showRangeSelector
: true,
492 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4'],
494 'Y1': { showInRangeSelector
: true },
495 'Y3': { showInRangeSelector
: true }
499 [0, 5, 8, 13, 21], // average (first and third) = 9
500 [5, 1, 3, 7, 14], // average (first and third) = 4
501 [10, 0, 19, 10, 6] // average (first and third) = 5
503 var graph
= document
.getElementById("graph");
504 var g
= new Dygraph(graph
, data
, opts
);
506 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
507 assert
.isNotNull(rangeSelector
);
509 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
511 yMin
: 4 - 5 * 0.25, // 25% padding on combined series range.
521 // Tests data computation for the mini plot with a single error bar series.
522 it('testSingleCombinedSeriesCustomBars', function() {
525 showRangeSelector
: true,
529 [0, [0, 1, 2]], // [low, value, high]
533 var graph
= document
.getElementById("graph");
534 var g
= new Dygraph(graph
, data
, opts
);
536 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
537 assert
.isNotNull(rangeSelector
);
539 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
541 yMin
: 1 - 7 * 0.25, // 25% padding
551 it('testSingleCombinedSeriesErrorBars', function() {
554 showRangeSelector
: true,
558 [0, [1, 1]], // [value, standard deviation]
562 var graph
= document
.getElementById("graph");
563 var g
= new Dygraph(graph
, data
, opts
);
565 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
566 assert
.isNotNull(rangeSelector
);
568 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
570 yMin
: 1 - 7 * 0.25, // 25% padding
580 // Tests data computation for the mini plot with two custom bar series.
581 it('testTwoCombinedSeriesCustomBars', function() {
584 showRangeSelector
: true,
585 labels
: ['X', 'Y1', 'Y2']
588 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
589 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
590 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
592 var graph
= document
.getElementById("graph");
593 var g
= new Dygraph(graph
, data
, opts
);
595 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
596 assert
.isNotNull(rangeSelector
);
598 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
600 yMin
: 3 - 7 * 0.25, // 25% padding
611 var assertGraphExistence
= function(g
, graph
) {
613 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
614 assert
.equal(2, zoomhandles
.length
);
615 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
616 assert
.equal(1, bgcanvas
.length
);
617 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
618 assert
.equal(1, fgcanvas
.length
);