From: Danilo Reinert Date: Wed, 12 Aug 2015 13:18:31 +0000 (-0300) Subject: Support value as a boolean array in setVisibility X-Git-Tag: v2.0.0~42^2~2 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=94fff6f2a229e28095253d8a83854c8c44497a14;p=dygraphs.git Support value as a boolean array in setVisibility --- diff --git a/auto_tests/tests/visibility.js b/auto_tests/tests/visibility.js index e8860c6..796bc81 100644 --- a/auto_tests/tests/visibility.js +++ b/auto_tests/tests/visibility.js @@ -67,4 +67,14 @@ it('testMultiSeriesShow', function() { assert.equal(' B D', getVisibleSeries(false, [[1,3], true])); }); +it('testValueAsArray', function() { + assert.equal(' B D', getVisibleSeries(false, [[1,2,3], [true,false,true]])); +}); + +it('testValueArgumentOfDifferentLength', function() { + assert.throws(function(){ getVisibleSeries(false, [[1,2,3], [true, true]]) }, + Error, "The value argument of setVisibility must have the same " + + "length of the num argument when passed as an array."); +}); + }); diff --git a/src/dygraph.js b/src/dygraph.js index 4f3ce77..9b9e204 100644 --- a/src/dygraph.js +++ b/src/dygraph.js @@ -3579,18 +3579,29 @@ Dygraph.prototype.visibility = function() { * Changes the visibility of one or more series. * * @param {number|number[]} num the series index or an array of series indices - * @param {boolean} value true or false, identifying the visibility. + * @param {boolean|boolean[]} value the visibility state expressed as a single + * boolean or an array of boolean equally + * sized to the num argument array */ Dygraph.prototype.setVisibility = function(num, value) { var x = this.visibility(); + var valueIsArray = Array.isArray(value); - if (num.constructor !== Array) num = [num]; + if (!Array.isArray(num)) num = [num]; + + // check if second argument is an array and, if so, is of the same length + if (valueIsArray) { + if (value.length != num.length) { + throw new Error("The value argument of setVisibility must have the same" + + " length of the num argument when passed as an array."); + } + } for (var i = 0; i < num.length; i++) { if (num[i] < 0 || num[i] >= x.length) { - console.warn("invalid series number in setVisibility: " + num[i]); + console.warn("Invalid series number in setVisibility: " + num[i]); } else { - x[num[i]] = value; + x[num[i]] = valueIsArray ? value[i] : value; } }