Skip to content

ignore invalid trace indices in restyle and update #3114

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 2 commits into from
Oct 16, 2018
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
11 changes: 11 additions & 0 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,17 @@ exports.coerceTraceIndices = function(gd, traceIndices) {
else if(!Array.isArray(traceIndices) || !traceIndices.length) {
return gd.data.map(function(_, i) { return i; });
}
else if(Array.isArray(traceIndices)) {
var traceIndicesOut = [];
for(var i = 0; i < traceIndices.length; i++) {
if(Lib.isIndex(traceIndices[i], gd.data.length)) {
traceIndicesOut.push(traceIndices[i]);
} else {
Lib.warn('trace index (', traceIndices[i], ') is not a number or is out of bounds');
}
}
return traceIndicesOut;
}
Copy link
Contributor Author

@antoinerg antoinerg Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I do a Lib.warn('trace indice is not number or is out of bound')?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea - should help users debug, since this really shouldn't happen normally.

indice index is not a number or is out of bounds

Also I guess while we're at it we should filter out non-integers and negative numbers. Oh hey lib.isIndex - accepts strings too but those seem to be OK, ie a=[1,2,3]; a['1']===2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooray for lib.isIndex. I really should spend some time to find out all the functions we have in lib!


return traceIndices;
};
Expand Down
17 changes: 17 additions & 0 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,18 @@ describe('Test plot api', function() {
expect(gd._fullData[0].marker.color).toBe('blue');
});

it('ignores invalid trace indices', function() {
var gd = {
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
layout: {}
};

mockDefaultsAndCalc(gd);

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

it('restores null values to defaults', function() {
var gd = {
data: [{x: [1, 2, 3], y: [1, 2, 3], type: 'scatter'}],
Expand Down Expand Up @@ -2580,6 +2592,11 @@ describe('Test plot api', function() {
.catch(failTest)
.then(done);
});

it('ignores invalid trace indices', function() {
// Call update on an invalid trace indice
Plotly.update(gd, {'type': 'scatter', 'marker.color': 'red'}, {}, [1]);
});
});

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