diff --git a/.eslintrc b/.eslintrc index 093ed358b6f..9404630b5bc 100644 --- a/.eslintrc +++ b/.eslintrc @@ -22,7 +22,6 @@ "no-trailing-spaces": [2], "no-multiple-empty-lines": [2, {"max": 2, "maxEOF": 0}], "eol-last": [2], - "linebreak-style": [2, "unix"], "indent": [2, 4, {"SwitchCase": 1}], "max-len": [0, 80], "brace-style": [0, "stroustrup", {"allowSingleLine": true}], diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index bd9f22f8fe8..1a5bd8d1fc0 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -633,8 +633,7 @@ function checkMoveTracesArgs(gd, currentIndices, newIndices) { * @param newIndices */ function checkAddTracesArgs(gd, traces, newIndices) { - var i, - value; + var i, value; // check that gd has attribute 'data' and 'data' is array if(!Array.isArray(gd.data)) { @@ -956,10 +955,16 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) { if(!Array.isArray(traces)) { traces = [traces]; } + + // make sure traces do not repeat existing ones + traces = traces.map(function(trace) { + return Lib.extendFlat({}, trace); + }); + helpers.cleanData(traces, gd.data); // add the traces to gd.data (no redrawing yet!) - for(i = 0; i < traces.length; i += 1) { + for(i = 0; i < traces.length; i++) { gd.data.push(traces[i]); } diff --git a/test/jasmine/tests/plot_api_test.js b/test/jasmine/tests/plot_api_test.js index c4d99ac8846..80acfbb1b7e 100644 --- a/test/jasmine/tests/plot_api_test.js +++ b/test/jasmine/tests/plot_api_test.js @@ -414,7 +414,6 @@ describe('Test plot api', function() { // make sure we didn't muck with gd.data if things failed! expect(gd).toEqual(expected); - }); it('should throw an error when traces and newIndices arrays are unequal', function() { @@ -454,7 +453,6 @@ describe('Test plot api', function() { expect(gd.data[3].uid).toBeDefined(); expect(PlotlyInternal.redraw).not.toHaveBeenCalled(); expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-2, -1], [1, 3]); - }); it('should work when newIndices has negative indices', function() { @@ -465,7 +463,6 @@ describe('Test plot api', function() { expect(gd.data[3].uid).toBeDefined(); expect(PlotlyInternal.redraw).not.toHaveBeenCalled(); expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-2, -1], [-3, -1]); - }); it('should work when newIndices is an integer', function() { @@ -474,7 +471,21 @@ describe('Test plot api', function() { expect(gd.data[2].uid).toBeDefined(); expect(PlotlyInternal.redraw).not.toHaveBeenCalled(); expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-1], [0]); + }); + + it('should work when adding an existing trace', function() { + Plotly.addTraces(gd, gd.data[0]); + + expect(gd.data.length).toEqual(3); + expect(gd.data[0]).not.toBe(gd.data[2]); + }); + + it('should work when duplicating the existing data', function() { + Plotly.addTraces(gd, gd.data); + expect(gd.data.length).toEqual(4); + expect(gd.data[0]).not.toBe(gd.data[2]); + expect(gd.data[1]).not.toBe(gd.data[3]); }); });