Skip to content
Open
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
1 change: 1 addition & 0 deletions draftlogs/8193_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix gaps between stacked bars during transitions [[#8193](https://github.com/plotly/plotly.js/pull/8193)]
16 changes: 16 additions & 0 deletions src/traces/bar/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 64 additions & 0 deletions test/jasmine/tests/transition_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,68 @@ describe('Plotly.react transitions:', function() {
})
.then(done, done.fail);
});

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++) {
var y = [];
for(var j = 0; j < 20; j++) y.push(i === 0 ? v : 1);
data.push({type: 'bar', y: y});
}
return data;
}

// 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 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, 40]},
transition: {duration: 300}
};
var transitionPromise;

Plotly.newPlot(gd, makeData(20), layout)
.then(function() {
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() {
expect(sawMidTransition).toBe(true, 'sampled mid-transition');
expect(maxGap).toBeLessThan(0.1, 'largest gap between neighbouring bars');
return transitionPromise;
})
.then(done, done.fail);
});
});