Skip to content

Commit 7ab6c22

Browse files
authored
Merge pull request #3527 from plotly/react-uirevision-when-removing-traces
Fix react+uirevision when removing traces
2 parents 02e51d8 + 7d11774 commit 7ab6c22

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/plot_api/plot_api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ function getTraceIndexFromUid(uid, data, tracei) {
25392539
if(data[i].uid === uid) return i;
25402540
}
25412541
// fall back on trace order, but only if user didn't provide a uid for that trace
2542-
return data[tracei].uid ? -1 : tracei;
2542+
return (!data[tracei] || data[tracei].uid) ? -1 : tracei;
25432543
}
25442544

25452545
function valsMatch(v1, v2) {

test/jasmine/tests/plot_api_react_test.js

+58
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,64 @@ describe('Plotly.react and uirevision attributes', function() {
10781078
.then(done);
10791079
});
10801080

1081+
describe('should handle case where traces are removed', function() {
1082+
var y0 = [1, 2, 1];
1083+
var y1 = [2, 1, 2];
1084+
1085+
function mockLegendClick() {
1086+
return Registry.call('_guiRestyle', gd, 'visible', 'legendonly');
1087+
}
1088+
1089+
function _assert(msg, exp) {
1090+
return function() {
1091+
expect(gd._fullData.length).toBe(exp.length, msg + ' - # traces');
1092+
exp.forEach(function(expi, i) {
1093+
expect(gd._fullData[i].visible).toBe(expi, msg + ' trace ' + i + ' visibility');
1094+
});
1095+
};
1096+
}
1097+
1098+
it('- case no uirevision no uid', function(done) {
1099+
Plotly.newPlot(gd, [{y: y0}, {y: y1}])
1100+
.then(_assert('base', [true, true]))
1101+
.then(mockLegendClick)
1102+
.then(_react([{y: [1, 2, 1]}]))
1103+
.then(_assert('after react', [true]))
1104+
.catch(failTest)
1105+
.then(done);
1106+
});
1107+
1108+
it('- case no uirevision with uid', function(done) {
1109+
Plotly.newPlot(gd, [{y: y0, uid: 'a'}, {y: y1, uid: 'b'}])
1110+
.then(_assert('base', [true, true]))
1111+
.then(mockLegendClick)
1112+
.then(_react([{y: [1, 2, 1], uid: 'a'}]))
1113+
.then(_assert('after react', [true]))
1114+
.catch(failTest)
1115+
.then(done);
1116+
});
1117+
1118+
it('- case with uirevision no uid', function(done) {
1119+
Plotly.newPlot(gd, [{y: y0}, {y: y1}], {uirevision: true})
1120+
.then(_assert('base', [true, true]))
1121+
.then(mockLegendClick)
1122+
.then(_react({data: [{y: [1, 2, 1]}], layout: {uirevision: true}}))
1123+
.then(_assert('after react', ['legendonly']))
1124+
.catch(failTest)
1125+
.then(done);
1126+
});
1127+
1128+
it('- case with uirevision with uid', function(done) {
1129+
Plotly.newPlot(gd, [{y: y0, uid: 'a'}, {y: y1, uid: 'b'}], {uirevision: true})
1130+
.then(_assert('base', [true, true]))
1131+
.then(mockLegendClick)
1132+
.then(_react({data: [{y: [1, 2, 1], uid: 'a'}], layout: {uirevision: true}}))
1133+
.then(_assert('after react', ['legendonly']))
1134+
.catch(failTest)
1135+
.then(done);
1136+
});
1137+
});
1138+
10811139
it('controls axis edits with axis.uirevision', function(done) {
10821140
function fig(mainRev, xRev, yRev, x2Rev, y2Rev) {
10831141
return {

0 commit comments

Comments
 (0)