Skip to content

Commit 125770b

Browse files
authored
Merge pull request #1175 from plotly/pr1136-clone-trace
Flat extend input traces on addTraces
2 parents fb0ff70 + 7e4c5c1 commit 125770b

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

.eslintrc

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"no-trailing-spaces": [2],
2323
"no-multiple-empty-lines": [2, {"max": 2, "maxEOF": 0}],
2424
"eol-last": [2],
25-
"linebreak-style": [2, "unix"],
2625
"indent": [2, 4, {"SwitchCase": 1}],
2726
"max-len": [0, 80],
2827
"brace-style": [0, "stroustrup", {"allowSingleLine": true}],

src/plot_api/plot_api.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,7 @@ function checkMoveTracesArgs(gd, currentIndices, newIndices) {
633633
* @param newIndices
634634
*/
635635
function checkAddTracesArgs(gd, traces, newIndices) {
636-
var i,
637-
value;
636+
var i, value;
638637

639638
// check that gd has attribute 'data' and 'data' is array
640639
if(!Array.isArray(gd.data)) {
@@ -956,10 +955,16 @@ Plotly.addTraces = function addTraces(gd, traces, newIndices) {
956955
if(!Array.isArray(traces)) {
957956
traces = [traces];
958957
}
958+
959+
// make sure traces do not repeat existing ones
960+
traces = traces.map(function(trace) {
961+
return Lib.extendFlat({}, trace);
962+
});
963+
959964
helpers.cleanData(traces, gd.data);
960965

961966
// add the traces to gd.data (no redrawing yet!)
962-
for(i = 0; i < traces.length; i += 1) {
967+
for(i = 0; i < traces.length; i++) {
963968
gd.data.push(traces[i]);
964969
}
965970

test/jasmine/tests/plot_api_test.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ describe('Test plot api', function() {
414414

415415
// make sure we didn't muck with gd.data if things failed!
416416
expect(gd).toEqual(expected);
417-
418417
});
419418

420419
it('should throw an error when traces and newIndices arrays are unequal', function() {
@@ -454,7 +453,6 @@ describe('Test plot api', function() {
454453
expect(gd.data[3].uid).toBeDefined();
455454
expect(PlotlyInternal.redraw).not.toHaveBeenCalled();
456455
expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-2, -1], [1, 3]);
457-
458456
});
459457

460458
it('should work when newIndices has negative indices', function() {
@@ -465,7 +463,6 @@ describe('Test plot api', function() {
465463
expect(gd.data[3].uid).toBeDefined();
466464
expect(PlotlyInternal.redraw).not.toHaveBeenCalled();
467465
expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-2, -1], [-3, -1]);
468-
469466
});
470467

471468
it('should work when newIndices is an integer', function() {
@@ -474,7 +471,21 @@ describe('Test plot api', function() {
474471
expect(gd.data[2].uid).toBeDefined();
475472
expect(PlotlyInternal.redraw).not.toHaveBeenCalled();
476473
expect(PlotlyInternal.moveTraces).toHaveBeenCalledWith(gd, [-1], [0]);
474+
});
475+
476+
it('should work when adding an existing trace', function() {
477+
Plotly.addTraces(gd, gd.data[0]);
478+
479+
expect(gd.data.length).toEqual(3);
480+
expect(gd.data[0]).not.toBe(gd.data[2]);
481+
});
482+
483+
it('should work when duplicating the existing data', function() {
484+
Plotly.addTraces(gd, gd.data);
477485

486+
expect(gd.data.length).toEqual(4);
487+
expect(gd.data[0]).not.toBe(gd.data[2]);
488+
expect(gd.data[1]).not.toBe(gd.data[3]);
478489
});
479490
});
480491

0 commit comments

Comments
 (0)