From e5cc0a4c5b0bd04207d46a4880d03581c83ee066 Mon Sep 17 00:00:00 2001 From: Danilo Reinert Date: Sun, 6 Sep 2015 15:51:51 -0300 Subject: [PATCH] Support key-value object in setVisibility --- auto_tests/tests/visibility.js | 10 ++-------- src/dygraph.js | 45 ++++++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/auto_tests/tests/visibility.js b/auto_tests/tests/visibility.js index 796bc81..dad65dc 100644 --- a/auto_tests/tests/visibility.js +++ b/auto_tests/tests/visibility.js @@ -67,14 +67,8 @@ 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."); +it('testObjectSeriesShowAndHide', function() { + assert.equal(' B D', getVisibleSeries(false, [{1:true, 2:false, 3:true}, null])); }); }); diff --git a/src/dygraph.js b/src/dygraph.js index 9b9e204..1de84a6 100644 --- a/src/dygraph.js +++ b/src/dygraph.js @@ -3578,30 +3578,41 @@ 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|boolean[]} value the visibility state expressed as a single - * boolean or an array of boolean equally - * sized to the num argument array + * @param {number|number[]|object} num the series index or an array of series indices + or an object mapping series numbers, as keys, to + visibility state (boolean values) + * + * @param {boolean} value the visibility state expressed as a boolean */ Dygraph.prototype.setVisibility = function(num, value) { var x = this.visibility(); - var valueIsArray = Array.isArray(value); + var numIsObject = false; - 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."); + if (!Array.isArray(num)) { + if (num !== null && typeof num === 'object') { + numIsObject = true; + } else { + num = [num]; } } - 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]); - } else { - x[num[i]] = valueIsArray ? value[i] : value; + if (numIsObject) { + for (var i in num) { + if (num.hasOwnProperty(i)) { + if (i < 0 || i >= x.length) { + console.warn("Invalid series number in setVisibility: " + i); + } else { + x[i] = num[i]; + } + } + } + } else { + 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]); + } else { + x[num[i]] = value; + } } } -- 2.7.4