Skip to content

Commit bcbb898

Browse files
committed
warn user if trace index is invalid or out of bounds
also fix attribute in test
1 parent 78ab4c6 commit bcbb898

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/plot_api/helpers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,15 @@ exports.coerceTraceIndices = function(gd, traceIndices) {
491491
return gd.data.map(function(_, i) { return i; });
492492
}
493493
else if(Array.isArray(traceIndices)) {
494-
return traceIndices.filter(function(i) {return i < gd.data.length;});
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;
495503
}
496504

497505
return traceIndices;

test/jasmine/tests/plot_api_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ describe('Test plot api', function() {
930930
mockDefaultsAndCalc(gd);
931931

932932
// Call restyle on an invalid trace indice
933-
Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'scatter'}, [1]);
933+
Plotly.restyle(gd, {'type': 'scatter', 'marker.color': 'red'}, [1]);
934934
});
935935

936936
it('restores null values to defaults', function() {
@@ -2595,7 +2595,7 @@ describe('Test plot api', function() {
25952595

25962596
it('ignores invalid trace indices', function() {
25972597
// Call update on an invalid trace indice
2598-
Plotly.update(gd, {'type': 'scatter', 'marker.color': 'scatter'}, {}, [1]);
2598+
Plotly.update(gd, {'type': 'scatter', 'marker.color': 'red'}, {}, [1]);
25992599
});
26002600
});
26012601

0 commit comments

Comments
 (0)