From 1601ad51cc4e500d865f1d2fe475b8200380fc35 Mon Sep 17 00:00:00 2001 From: Bodison Date: Wed, 25 Nov 2026 17:38:53 +0000 Subject: [PATCH 1/3] Fix gaps between stacked bars during transitions --- src/traces/bar/plot.js | 16 +++++++++ test/jasmine/tests/transition_test.js | 48 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/traces/bar/plot.js b/src/traces/bar/plot.js index 2612f9a7614..f2d180676dd 100644 --- a/src/traces/bar/plot.js +++ b/src/traces/bar/plot.js @@ -89,6 +89,22 @@ function hasTransition(transitionOpts) { } function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback) { + if (!gd._fullLayout.uniformtext.mode && hasTransition(opts)) { + // One start time for everything drawn here: started one by one, bars that + // touch (e.g. in a stack) drift apart mid-transition. + traceLayer + .transition() + .duration(opts.duration) + .ease(opts.easing) + .each(function () { + _plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback); + }); + } else { + _plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback); + } +} + +function _plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback) { var xa = plotinfo.xaxis; var ya = plotinfo.yaxis; var fullLayout = gd._fullLayout; diff --git a/test/jasmine/tests/transition_test.js b/test/jasmine/tests/transition_test.js index ceba1cc4a66..4d75dd055c7 100644 --- a/test/jasmine/tests/transition_test.js +++ b/test/jasmine/tests/transition_test.js @@ -1182,4 +1182,52 @@ describe('Plotly.react transitions:', function() { }) .then(done, done.fail); }); + + it('should start the transitions of all bars at the same time', function(done) { + // enough bars that drawing them takes more than a millisecond + function makeData(v) { + var data = []; + for(var i = 0; i < 20; i++) { + var y = []; + for(var j = 0; j < 20; j++) y.push(i === 0 ? v : 1); + data.push({type: 'bar', y: y}); + } + return data; + } + + // the start time d3 keeps for each bar's pending or running transition + function getStartTimes() { + var times = []; + gd.querySelectorAll('.bars .point path').forEach(function(node) { + var lock = node.__transition__ || {}; + Object.keys(lock).forEach(function(k) { + if(lock[k] && lock[k].time) times.push(lock[k].time); + }); + }); + return times; + } + + // fixed axis ranges, so that only the traces are transitioned + var layout = { + barmode: 'stack', + xaxis: {range: [-1, 20]}, + yaxis: {range: [0, 25]}, + transition: {duration: 300} + }; + var transitionPromise; + + Plotly.newPlot(gd, makeData(1), layout) + .then(function() { + transitionPromise = Plotly.react(gd, makeData(2), layout); + }) + .then(delay(50)) + .then(function() { + var times = getStartTimes(); + expect(times.length).toBe(400, '# of bars in transition'); + expect(Math.max.apply(null, times) - Math.min.apply(null, times)) + .toBe(0, 'spread of start times'); + return transitionPromise; + }) + .then(done, done.fail); + }); }); From 10fcb697ff435442db52d5bf49993a1eaec0e7e9 Mon Sep 17 00:00:00 2001 From: Bodison Date: Wed, 25 Nov 2026 19:30:20 +0000 Subject: [PATCH 2/3] Add draftlog --- draftlogs/8193_fix.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 draftlogs/8193_fix.md diff --git a/draftlogs/8193_fix.md b/draftlogs/8193_fix.md new file mode 100644 index 00000000000..0616d53dbb9 --- /dev/null +++ b/draftlogs/8193_fix.md @@ -0,0 +1 @@ +- Fix gaps between stacked bars during transitions [[#8193](https://github.com/plotly/plotly.js/pull/8193)] From a8e7faee55a6f233bd1010623b11de10314b9b06 Mon Sep 17 00:00:00 2001 From: Bodison Date: Wed, 25 Nov 2026 20:45:47 +0000 Subject: [PATCH 3/3] Test bar positions mid-transition instead of d3's transition state --- test/jasmine/tests/transition_test.js | 52 +++++++++++++++++---------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/test/jasmine/tests/transition_test.js b/test/jasmine/tests/transition_test.js index 4d75dd055c7..ee8c2013f06 100644 --- a/test/jasmine/tests/transition_test.js +++ b/test/jasmine/tests/transition_test.js @@ -1183,8 +1183,8 @@ describe('Plotly.react transitions:', function() { .then(done, done.fail); }); - it('should start the transitions of all bars at the same time', function(done) { - // enough bars that drawing them takes more than a millisecond + it('should keep stacked bars together during a transition', function(done) { + // the bottom trace shrinks to nothing, moving every other bar function makeData(v) { var data = []; for(var i = 0; i < 20; i++) { @@ -1195,37 +1195,53 @@ describe('Plotly.react transitions:', function() { return data; } - // the start time d3 keeps for each bar's pending or running transition - function getStartTimes() { - var times = []; - gd.querySelectorAll('.bars .point path').forEach(function(node) { - var lock = node.__transition__ || {}; - Object.keys(lock).forEach(function(k) { - if(lock[k] && lock[k].time) times.push(lock[k].time); - }); + // bounding boxes of the bars in the first stack, from the bottom up + function getBoxes() { + var boxes = []; + gd.querySelectorAll('.bars').forEach(function(trace) { + boxes.push(trace.querySelector('.point path').getBBox()); }); - return times; + return boxes; + } + + var initialHeight; + var sawMidTransition = false; + var maxGap = 0; + + function sample() { + var boxes = getBoxes(); + var h = boxes[0].height; + if(h > 1 && h < initialHeight - 1) sawMidTransition = true; + + for(var i = 0; i + 1 < boxes.length; i++) { + var gap = boxes[i].y - (boxes[i + 1].y + boxes[i + 1].height); + maxGap = Math.max(maxGap, Math.abs(gap)); + } } // fixed axis ranges, so that only the traces are transitioned var layout = { barmode: 'stack', xaxis: {range: [-1, 20]}, - yaxis: {range: [0, 25]}, + yaxis: {range: [0, 40]}, transition: {duration: 300} }; var transitionPromise; - Plotly.newPlot(gd, makeData(1), layout) + Plotly.newPlot(gd, makeData(20), layout) .then(function() { - transitionPromise = Plotly.react(gd, makeData(2), layout); + initialHeight = getBoxes()[0].height; + transitionPromise = Plotly.react(gd, makeData(0), layout); }) + .then(delay(100)) + .then(sample) + .then(delay(50)) + .then(sample) .then(delay(50)) + .then(sample) .then(function() { - var times = getStartTimes(); - expect(times.length).toBe(400, '# of bars in transition'); - expect(Math.max.apply(null, times) - Math.min.apply(null, times)) - .toBe(0, 'spread of start times'); + expect(sawMidTransition).toBe(true, 'sampled mid-transition'); + expect(maxGap).toBeLessThan(0.1, 'largest gap between neighbouring bars'); return transitionPromise; }) .then(done, done.fail);