Skip to content

Flat extend input traces on addTraces #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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}],
Expand Down
11 changes: 8 additions & 3 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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]);
}

Expand Down
17 changes: 14 additions & 3 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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]);
});
});

Expand Down