Skip to content

Commit e074459

Browse files
authored
Merge pull request #7668 from plotly/fix-stacked-area-hide-show-bug
Fix stacked area hide show bug
2 parents 93607b1 + 172791d commit e074459

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

draftlogs/7668_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix issue where some traces in stacked area charts were not rendering their fill correctly after certain sequences of hide/show operations in the legend [[#7668](https://github.com/plotly/plotly.js/pull/7668)]

src/traces/scatter/plot.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ function createFills(gd, traceJoin, plotinfo) {
8383

8484
var trace = d[0].trace;
8585

86+
// trace._ownFill and trace._nextFill may have leftover values
87+
// from a previous call to createFills. They should always start as null.
88+
// We clear both values in order to start with a clean slate.
89+
// Note that these are DIFFERENT VARIABLES than
90+
// trace._ownfill and trace._nexttrace referenced a few lines down.
91+
trace._ownFill = null;
92+
trace._nextFill = null;
93+
8694
var fillData = [];
8795
if(trace._ownfill) fillData.push('_ownFill');
8896
if(trace._nexttrace) fillData.push('_nextFill');
@@ -91,9 +99,7 @@ function createFills(gd, traceJoin, plotinfo) {
9199

92100
fillJoin.enter().append('g');
93101

94-
fillJoin.exit()
95-
.each(function(d) { trace[d] = null; })
96-
.remove();
102+
fillJoin.exit().remove();
97103

98104
fillJoin.order().each(function(d) {
99105
// make a path element inside the fill group, just so

test/jasmine/tests/scatter_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,50 @@ describe('stacked area', function() {
13611361
})
13621362
.then(done, done.fail);
13631363
});
1364+
1365+
it('maintains correct fill elements when toggling trace visibility in stacked area charts', function(done) {
1366+
// Regression test for issue #7660
1367+
// When isolating, then hiding all, then showing all traces in a stacked area chart,
1368+
// fill elements should be correctly drawn for all traces.
1369+
1370+
function countNonEmptyFills() {
1371+
return d3SelectAll('.scatterlayer .js-fill').filter(function() {
1372+
var d = d3Select(this).attr('d');
1373+
return d && d !== 'M0,0Z';
1374+
}).size();
1375+
}
1376+
1377+
Plotly.newPlot(gd, [
1378+
{x: [1, 2, 3], y: [2, 1, 4], stackgroup: 'one', name: 'trace 0'},
1379+
{x: [1, 2, 3], y: [1, 1, 2], stackgroup: 'one', name: 'trace 1'},
1380+
{x: [1, 2, 3], y: [3, 0, 2], stackgroup: 'one', name: 'trace 2'}
1381+
])
1382+
.then(function() {
1383+
// Initially all 3 traces should be visible with fills
1384+
expect(countNonEmptyFills()).toBe(3, 'should have 3 fill paths initially');
1385+
1386+
// Show trace 1 only
1387+
return Plotly.restyle(gd, 'visible', ['legendonly', true, 'legendonly']);
1388+
})
1389+
.then(function() {
1390+
// Verify only one fill is drawn (trace 1)
1391+
expect(countNonEmptyFills()).toBe(1, 'should have 1 fill path when isolating trace 1');
1392+
// Hide all traces
1393+
return Plotly.restyle(gd, 'visible', ['legendonly', 'legendonly', 'legendonly']);
1394+
})
1395+
.then(function() {
1396+
// Verify no fills are drawn
1397+
expect(countNonEmptyFills()).toBe(0, 'should have 0 fill paths when hiding all traces');
1398+
// Show all traces again
1399+
return Plotly.restyle(gd, 'visible', [true, true, true]);
1400+
})
1401+
.then(function() {
1402+
// Verify all 3 fills are drawn again
1403+
// This is the step that was failing in #7660
1404+
expect(countNonEmptyFills()).toBe(3, 'should have 3 fill paths after showing all traces again');
1405+
})
1406+
.then(done, done.fail);
1407+
});
13641408
});
13651409

13661410
describe('scatter hoverPoints', function() {

0 commit comments

Comments
 (0)