From: Dan Vanderkam Date: Sat, 1 Oct 2016 20:24:22 +0000 (-0400) Subject: Only include visible series in range selector X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=refs%2Fheads%2Frange-selector-visible;p=dygraphs.git Only include visible series in range selector --- diff --git a/auto_tests/tests/range_selector.js b/auto_tests/tests/range_selector.js index 82884c8..1bba353 100644 --- a/auto_tests/tests/range_selector.js +++ b/auto_tests/tests/range_selector.js @@ -630,6 +630,73 @@ it('testTwoCombinedSeriesCustomBars', function() { }, combinedSeries); }); +it('testHiddenSeriesExcludedFromMiniplot', function() { + var opts = { + showRangeSelector: true, + labels: ['X', 'Y1', 'Y2'], + visibility: [true, false] + }; + 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_(RangeSelectorPlugin); + assert.isNotNull(rangeSelector); + + // Invisible series (e.g. Y2) are not included in the combined series. + var combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assert.deepEqual({ + yMin: 1 - 6 * 0.25, // 25% padding on single series range. + yMax: 7 + 6 * 0.25, + data: [ + [0, 1], + [5, 4], + [10, 7] + ] + }, combinedSeries); + + // If Y2 is explicitly marked to be included in the range selector, + // then it will be (even if it's not visible). Since we've started being + // explicit about marking series for inclusion, this means that Y1 is no + // longer included. + g.updateOptions({ + series: { + Y2: { showInRangeSelector: true }, + } + }); + combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assert.deepEqual({ + yMin: 3 - 6 * 0.25, // 25% padding on combined series range. + yMax: 9 + 6 * 0.25, + data: [ + [0, 3], + [5, 6], + [10, 9] + ] + }, combinedSeries); + + // If we explicitly mark Y1, too, then it also gets included. + g.updateOptions({ + series: { + Y1: { showInRangeSelector: true }, + Y2: { showInRangeSelector: true }, + } + }); + combinedSeries = rangeSelector.computeCombinedSeriesAndLimits_(); + assert.deepEqual({ + yMin: 2 - 6 * 0.25, // 25% padding on combined series range. + yMax: 8 + 6 * 0.25, + data: [ + [0, 2], + [5, 5], + [10, 8] + ] + }, combinedSeries); +}); var assertGraphExistence = function(g, graph) { assert.isNotNull(g); diff --git a/src/dygraph-options-reference.js b/src/dygraph-options-reference.js index 73615c2..dc9243b 100644 --- a/src/dygraph-options-reference.js +++ b/src/dygraph-options-reference.js @@ -789,7 +789,7 @@ OPTIONS_REFERENCE = // "default": "null", "labels": ["Range Selector"], "type": "boolean", - "description": "Mark this series for inclusion in the range selector. The mini plot curve will be an average of all such series. If this is not specified for any series, the default behavior is to average all the series. Setting it for one series will result in that series being charted alone in the range selector." + "description": "Mark this series for inclusion in the range selector. The mini plot curve will be an average of all such series. If this is not specified for any series, the default behavior is to average all the visible series. Setting it for one series will result in that series being charted alone in the range selector. Once it's set for a single series, it needs to be set for all series which should be included (regardless of visibility)." }, "animatedZooms": { "default": "false", diff --git a/src/plugins/range-selector.js b/src/plugins/range-selector.js index ef3aedb..7f91dc8 100644 --- a/src/plugins/range-selector.js +++ b/src/plugins/range-selector.js @@ -635,13 +635,23 @@ rangeSelector.prototype.computeCombinedSeriesAndLimits_ = function() { var labels = g.getLabels(); var includeSeries = new Array(numColumns); var anySet = false; + var visibility = g.visibility(); + var inclusion = []; + for (i = 1; i < numColumns; i++) { var include = this.getOption_('showInRangeSelector', labels[i]); - includeSeries[i] = include; + inclusion.push(include); if (include !== null) anySet = true; // it's set explicitly for this series } - if (!anySet) { - for (i = 0; i < includeSeries.length; i++) includeSeries[i] = true; + + if (anySet) { + for (i = 1; i < numColumns; i++) { + includeSeries[i] = inclusion[i - 1]; + } + } else { + for (i = 1; i < numColumns; i++) { + includeSeries[i] = visibility[i - 1]; + } } // Create a combined series (average of selected series values).