From 9a43cda97df7d67845fcf7370b4a563a96d0a300 Mon Sep 17 00:00:00 2001 From: Robert Elliott Date: Tue, 6 Jun 2017 04:25:18 +0200 Subject: [PATCH] issue 1662 (#1663) Fixes issue #1662. --- .gitignore | 1 + spec/data-spec.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/data.js | 14 +++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e6f33ca..a3f800e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ d3.min.js components build .sass-cache +.idea/ # coverage report /coverage diff --git a/spec/data-spec.js b/spec/data-spec.js index 7379bde..bb3ccbb 100644 --- a/spec/data-spec.js +++ b/spec/data-spec.js @@ -186,6 +186,65 @@ describe('c3 chart data', function () { }); }); + describe('addHiddenTargetIds if not already hidden', function () { + it('should update args', function () { + args = { + data: { + columns: [ + ['data1', 30, 200, 100, 400, 150, 250], + ['data2', 150, 120, 110, 140, 115, 125] + ] + } + }; + expect(true).toBeTruthy(); + }); + + it('length of hiddenTargetIds should not change if same key added twice', function () { + chart.internal.addHiddenTargetIds('data1'); + expect(chart.internal.hiddenTargetIds.length).toBe(1); + chart.internal.addHiddenTargetIds('data1'); + expect(chart.internal.hiddenTargetIds.length).toBe(1); + chart.hide('data1'); + expect(chart.internal.hiddenTargetIds.length).toBe(1); + chart.internal.addHiddenTargetIds('data2'); + expect(chart.internal.hiddenTargetIds.length).toBe(2); + chart.show(); + chart.hide(['data1', 'data2']); + expect(chart.internal.hiddenTargetIds.length).toBe(2); + chart.show(); + chart.hide(); + expect(chart.internal.hiddenTargetIds.length).toBe(2); + }); + }); + + describe('addHiddenLegendIds if not already hidden', function () { + it('should update args', function () { + args = { + data: { + columns: [ + ['data1', 30, 200, 100, 400, 150, 250], + ['data2', 150, 120, 110, 140, 115, 125] + ] + } + }; + expect(true).toBeTruthy(); + }); + + it('length of hiddenLegendIds should not change if same key added twice', function () { + chart.internal.addHiddenLegendIds('data1'); + expect(chart.internal.hiddenLegendIds.length).toBe(1); + chart.internal.addHiddenLegendIds('data1'); + expect(chart.internal.hiddenLegendIds.length).toBe(1); + chart.hide('data1', {withLegend: true}); + expect(chart.internal.hiddenLegendIds.length).toBe(1); + chart.hide('data2', {withLegend: true}); + expect(chart.internal.hiddenLegendIds.length).toBe(2); + chart.show(['data1', 'data2'], {withLegend: true}); + chart.hide(['data1', 'data2'], {withLegend: true}); + expect(chart.internal.hiddenLegendIds.length).toBe(2); + }); + }); + describe('data.xs', function () { beforeAll(function () { diff --git a/src/data.js b/src/data.js index 507337b..342f192 100644 --- a/src/data.js +++ b/src/data.js @@ -179,13 +179,23 @@ c3_chart_internal_fn.mapTargetsToUniqueXs = function (targets) { return xs.sort(function (a, b) { return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN; }); }; c3_chart_internal_fn.addHiddenTargetIds = function (targetIds) { - this.hiddenTargetIds = this.hiddenTargetIds.concat(targetIds); + targetIds = (targetIds instanceof Array) ? targetIds : new Array(targetIds); + for (var i = 0; i < targetIds.length; i++) { + if (this.hiddenTargetIds.indexOf(targetIds[i]) < 0) { + this.hiddenTargetIds = this.hiddenTargetIds.concat(targetIds[i]); + } + } }; c3_chart_internal_fn.removeHiddenTargetIds = function (targetIds) { this.hiddenTargetIds = this.hiddenTargetIds.filter(function (id) { return targetIds.indexOf(id) < 0; }); }; c3_chart_internal_fn.addHiddenLegendIds = function (targetIds) { - this.hiddenLegendIds = this.hiddenLegendIds.concat(targetIds); + targetIds = (targetIds instanceof Array) ? targetIds : new Array(targetIds); + for (var i = 0; i < targetIds.length; i++) { + if (this.hiddenLegendIds.indexOf(targetIds[i]) < 0) { + this.hiddenLegendIds = this.hiddenLegendIds.concat(targetIds[i]); + } + } }; c3_chart_internal_fn.removeHiddenLegendIds = function (targetIds) { this.hiddenLegendIds = this.hiddenLegendIds.filter(function (id) { return targetIds.indexOf(id) < 0; });