Skip to content

Commit 5468d6a

Browse files
authored
Merge pull request #3114 from plotly/3113-restyle-invalid-trace
ignore invalid trace indices in restyle and update
2 parents 9899230 + bcbb898 commit 5468d6a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/plot_api/helpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,17 @@ exports.coerceTraceIndices = function(gd, traceIndices) {
490490
else if(!Array.isArray(traceIndices) || !traceIndices.length) {
491491
return gd.data.map(function(_, i) { return i; });
492492
}
493+
else if(Array.isArray(traceIndices)) {
494+
var traceIndicesOut = [];
495+
for(var i = 0; i < traceIndices.length; i++) {
496+
if(Lib.isIndex(traceIndices[i], gd.data.length)) {
497+
traceIndicesOut.push(traceIndices[i]);
498+
} else {
499+
Lib.warn('trace index (', traceIndices[i], ') is not a number or is out of bounds');
500+
}
501+
}
502+
return traceIndicesOut;
503+
}
493504

494505
return traceIndices;
495506
};

test/jasmine/tests/plot_api_test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,18 @@ describe('Test plot api', function() {
921921
expect(gd._fullData[0].marker.color).toBe('blue');
922922
});
923923

924+
it('ignores invalid trace indices', function() {
925+
var gd = {
926+
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
927+
layout: {}
928+
};
929+
930+
mockDefaultsAndCalc(gd);
931+
932+
// Call restyle on an invalid trace indice
933+
Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'red'}, [1]);
934+
});
935+
924936
it('restores null values to defaults', function() {
925937
var gd = {
926938
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
@@ -2580,6 +2592,11 @@ describe('Test plot api', function() {
25802592
.catch(failTest)
25812593
.then(done);
25822594
});
2595+
2596+
it('ignores invalid trace indices', function() {
2597+
// Call update on an invalid trace indice
2598+
Plotly.update(gd, {'type': 'scatter', 'marker.color': 'red'}, {}, [1]);
2599+
});
25832600
});
25842601

25852602
describe('@noCIdep Plotly.react', function() {

0 commit comments

Comments
 (0)