From d5daacbe906d7c256098b1316e6fc6e572e6e644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Wed, 20 Jan 2016 18:26:31 -0500 Subject: [PATCH 1/4] add descending sorter function --- src/lib/index.js | 1 + src/lib/search.js | 1 + 2 files changed, 2 insertions(+) diff --git a/src/lib/index.js b/src/lib/index.js index 12b78cb4c2c..99f9d5f61b4 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -31,6 +31,7 @@ lib.parseDate = datesModule.parseDate; var searchModule = require('./search'); lib.findBin = searchModule.findBin; lib.sorterAsc = searchModule.sorterAsc; +lib.sorterDec = searchModule.sorterDes; lib.distinctVals = searchModule.distinctVals; lib.roundUp = searchModule.roundUp; diff --git a/src/lib/search.js b/src/lib/search.js index 260c496a193..5ff6e64d19d 100644 --- a/src/lib/search.js +++ b/src/lib/search.js @@ -56,6 +56,7 @@ function greaterThan(a, b) { return a > b; } function greaterOrEqual(a, b) { return a >= b; } exports.sorterAsc = function(a, b) { return a - b; }; +exports.sorterDes = function(a, b) { return b - a; }; /** * find distinct values in an array, lumping together ones that appear to From b7281b4722ef5d29b4007c0434869f4c5b5a79dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Wed, 20 Jan 2016 18:28:47 -0500 Subject: [PATCH 2/4] use descending sorter in deleteTrace: so that 0,1, ... 11 are sorted as 11, 10, ... 0 instead of 1, 10, 11, 2, 3 ... 9 --- src/plot_api/plot_api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 86d92cdfea4..c2ae08f8986 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -1423,7 +1423,7 @@ Plotly.deleteTraces = function deleteTraces(gd, indices) { indices = positivifyIndices(indices, gd.data.length - 1); // we want descending here so that splicing later doesn't affect indexing - indices.sort().reverse(); + indices.sort(Lib.sorterDec); for (i = 0; i < indices.length; i += 1) { deletedTrace = gd.data.splice(indices[i], 1)[0]; traces.push(deletedTrace); From 9aa06d2e7631d7a77200259149fe23abceb2dafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Wed, 20 Jan 2016 18:29:06 -0500 Subject: [PATCH 3/4] add deleteTrace test for big indice arrays --- test/jasmine/tests/plot_api_test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index de3a660ce80..59184e2dd58 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -141,6 +141,32 @@ describe('Test plot api', function() { }); + it('should work with more than 10 indices', function() { + gd.data = []; + + for(var i = 0; i < 20; i++) { + gd.data.push({ + name: 'trace #' + i + }); + } + + var expectedData = [ + {name: 'trace #12'}, + {name: 'trace #13'}, + {name: 'trace #14'}, + {name: 'trace #15'}, + {name: 'trace #16'}, + {name: 'trace #17'}, + {name: 'trace #18'}, + {name: 'trace #19'} + ]; + + Plotly.deleteTraces(gd, [0,1,2,3,4,5,6,7,8,9,10,11]); + expect(gd.data).toEqual(expectedData); + expect(PlotlyInternal.redraw).toHaveBeenCalled(); + + }); + }); describe('Plotly.addTraces', function() { From 15e73095c8e1eb5b8dfc6fe4de1319c4b65ba9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jan 2016 09:42:01 -0500 Subject: [PATCH 4/4] export descending sorter as `sorterDes` --- src/lib/index.js | 2 +- src/plot_api/plot_api.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/index.js b/src/lib/index.js index 99f9d5f61b4..961a5ab0cf4 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -31,7 +31,7 @@ lib.parseDate = datesModule.parseDate; var searchModule = require('./search'); lib.findBin = searchModule.findBin; lib.sorterAsc = searchModule.sorterAsc; -lib.sorterDec = searchModule.sorterDes; +lib.sorterDes = searchModule.sorterDes; lib.distinctVals = searchModule.distinctVals; lib.roundUp = searchModule.roundUp; diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index c2ae08f8986..ad325a38695 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -1423,7 +1423,7 @@ Plotly.deleteTraces = function deleteTraces(gd, indices) { indices = positivifyIndices(indices, gd.data.length - 1); // we want descending here so that splicing later doesn't affect indexing - indices.sort(Lib.sorterDec); + indices.sort(Lib.sorterDes); for (i = 0; i < indices.length; i += 1) { deletedTrace = gd.data.splice(indices[i], 1)[0]; traces.push(deletedTrace);