From: Dan Vanderkam Date: Sat, 26 Oct 2013 22:24:54 +0000 (-0500) Subject: merge X-Git-Tag: v1.1.0~78 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=b1a962155bc64c306bc71f045d9c936e0d6afbe4;p=dygraphs.git merge --- diff --git a/auto_tests/tests/range_selector.js b/auto_tests/tests/range_selector.js index 34e3574..5d143a7 100644 --- a/auto_tests/tests/range_selector.js +++ b/auto_tests/tests/range_selector.js @@ -17,7 +17,8 @@ RangeSelectorTestCase.prototype.testRangeSelector = function() { var opts = { width: 480, height: 320, - showRangeSelector: true + showRangeSelector: true, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -40,7 +41,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorWithErrorBars = function() { width: 480, height: 320, errorBars: true, - showRangeSelector: true + showRangeSelector: true, + labels: ['X', 'Y'] }; var data = [ [1, [10, 10]], @@ -63,7 +65,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorWithCustomBars = function() { width: 480, height: 320, customBars: true, - showRangeSelector: true + showRangeSelector: true, + labels: ['X', 'Y'] }; var data = [ [1, [10, 10, 100]], @@ -86,7 +89,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorWithLogScale = function() { width: 480, height: 320, logscale: true, - showRangeSelector: true + showRangeSelector: true, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -111,7 +115,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorOptions = function() { showRangeSelector: true, rangeSelectorHeight: 30, rangeSelectorPlotFillColor: 'lightyellow', - rangeSelectorPlotStyleColor: 'yellow' + rangeSelectorPlotStyleColor: 'yellow', + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -132,7 +137,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorOptions = function() { RangeSelectorTestCase.prototype.testRangeSelectorEnablingAfterCreation = function() { var opts = { width: 480, - height: 320 + height: 320, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -157,7 +163,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorWithAnimatedZoomsOption = funct width: 480, height: 320, showRangeSelector: true, - animatedZooms: true + animatedZooms: true, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -180,7 +187,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorWithAnimatedZoomsOption2 = func var opts = { width: 480, height: 320, - animatedZooms: true + animatedZooms: true, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -204,7 +212,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorInteraction = function() { var opts = { width: 480, height: 320, - showRangeSelector: true + showRangeSelector: true, + labels: ['X', 'Y'] }; var data = [ [1, 10], @@ -326,7 +335,8 @@ RangeSelectorTestCase.prototype.testRangeSelectorPositionIfXAxisNotDrawn = funct xAxisHeight: 30, drawXAxis: false, showRangeSelector: true, - rangeSelectorHeight: 30 + rangeSelectorHeight: 30, + labels: ['X', 'Y'] }; var data = [ [0, 1], @@ -344,6 +354,191 @@ RangeSelectorTestCase.prototype.testRangeSelectorPositionIfXAxisNotDrawn = funct assertEquals("Range selector is not at the expected position.","70px", fgcanvas.style.top); }; +RangeSelectorTestCase.prototype.testMiniPlotDrawn = function() { + // Install Proxy to track canvas calls. + var origFunc = Dygraph.getContext; + var miniHtx; + Dygraph.getContext = function(canvas) { + if (canvas.className != 'dygraph-rangesel-bgcanvas') { + return origFunc(canvas); + } + miniHtx = new Proxy(origFunc(canvas)); + return miniHtx; + }; + + var opts = { + width: 480, + height: 100, + xAxisHeight: 30, + drawXAxis: false, + showRangeSelector: true, + rangeSelectorHeight: 30, + rangeSelectorPlotStrokeColor: '#ff0000', + labels: ['X', 'Y'] + }; + var data = [ + [0, 1], + [5, 4], + [10, 8] + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + // TODO(danvk): more precise tests. + assertTrue(0 < CanvasAssertions.numLinesDrawn(miniHtx, '#ff0000')); + + Dygraph.getContext = origFunc; +}; + +// Tests data computation for the mini plot with a single series. +RangeSelectorTestCase.prototype.testSingleCombinedSeries = function() { + var opts = { + showRangeSelector: true, + labels: ['X', 'Y1'] + }; + var data = [ + [0, 1], + [5, 4], + [10, 8] + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + assertNotNull(rangeSelector); + + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assertEquals({ + yMin: 1 - 7 * 0.25, // 25% padding + yMax: 8 + 7 * 0.25, + data: [ + [0, 1], + [5, 4], + [10, 8] + ] + }, combinedSeries); +}; + + +// Tests that multiple series are averaged for the miniplot. +RangeSelectorTestCase.prototype.testCombinedSeries = function() { + var opts = { + showRangeSelector: true, + labels: ['X', 'Y1', 'Y2'] + }; + var data = [ + [0, 1, 3], // average = 2 + [5, 4, 6], // average = 5 + [10, 7, 9] // average = 8 + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + assertNotNull(rangeSelector); + + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assertEquals({ + yMin: 2 - 6 * 0.25, // 25% padding on combined series range. + yMax: 8 + 6 * 0.25, + data: [ + [0, 2], + [5, 5], + [10, 8] + ] + }, combinedSeries); +}; + +// Tests data computation for the mini plot with a single error bar series. +RangeSelectorTestCase.prototype.testSingleCombinedSeriesCustomBars = function() { + var opts = { + customBars: true, + showRangeSelector: true, + labels: ['X', 'Y1'] + }; + var data = [ + [0, [0, 1, 2]], // [low, value, high] + [5, [1, 4, 5]], + [10, [7, 8, 9]] + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + assertNotNull(rangeSelector); + + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assertEquals({ + yMin: 1 - 7 * 0.25, // 25% padding + yMax: 8 + 7 * 0.25, + data: [ + [0, 1], + [5, 4], + [10, 8] + ] + }, combinedSeries); +}; + +RangeSelectorTestCase.prototype.testSingleCombinedSeriesErrorBars = function() { + var opts = { + errorBars: true, + showRangeSelector: true, + labels: ['X', 'Y1'] + }; + var data = [ + [0, [1, 1]], // [value, standard deviation] + [5, [4, 2]], + [10, [8, 1]] + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + assertNotNull(rangeSelector); + + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assertEquals({ + yMin: 1 - 7 * 0.25, // 25% padding + yMax: 8 + 7 * 0.25, + data: [ + [0, 1], + [5, 4], + [10, 8] + ] + }, combinedSeries); +}; + +// Tests data computation for the mini plot with two custom bar series. +RangeSelectorTestCase.prototype.testTwoCombinedSeriesCustomBars = function() { + var opts = { + customBars: true, + showRangeSelector: true, + labels: ['X', 'Y1', 'Y2'] + }; + var data = [ + [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3 + [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6 + [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10 + ]; + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, data, opts); + + var rangeSelector = g.getPluginInstance_(Dygraph.Plugins.RangeSelector); + assertNotNull(rangeSelector); + + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assertEquals({ + yMin: 3 - 7 * 0.25, // 25% padding + yMax: 10 + 7 * 0.25, + data: [ + [0, 3], + [5, 6], + [10, 10] + ] + }, combinedSeries); +}; + + RangeSelectorTestCase.prototype.assertGraphExistence = function(g, graph) { assertNotNull(g); var zoomhandles = graph.getElementsByClassName('dygraph-rangesel-zoomhandle'); diff --git a/dygraph.js b/dygraph.js index c3efffc..7df3752 100644 --- a/dygraph.js +++ b/dygraph.js @@ -585,6 +585,22 @@ Dygraph.prototype.cascadeEvents_ = function(name, extra_props) { }; /** + * Fetch a plugin instance of a particular class. Only for testing. + * @private + * @param {!Class} type The type of the plugin. + * @return {Object} Instance of the plugin, or null if there is none. + */ +Dygraph.prototype.getPluginInstance_ = function(type) { + for (var i = 0; i < this.plugins_.length; i++) { + var p = this.plugins_[i]; + if (p.plugin instanceof type) { + return p.plugin; + } + } + return null; +}; + +/** * Returns the zoomed status of the chart for one or both axes. * * Axis is an optional parameter. Can be set to 'x' or 'y'.