Skip to content

Commit 42d5e4e

Browse files
committed
end Legend.draw early when margin-redraw in-progress
... to avoid race conditions leading to wrong legend position and size.
1 parent 35b0c92 commit 42d5e4e

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/components/legend/draw.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ module.exports = function draw(gd) {
9393
Lib.syncOrAsync([
9494
Plots.previousPromises,
9595
function() { return computeLegendDimensions(gd, groups, traces); },
96-
function() { return expandMargin(gd); },
9796
function() {
97+
// IF expandMargin return a Promise (which is truthy),
98+
// we're under a doAutoMargin redraw, so we don't have to
99+
// draw the remaining pieces below
100+
if(expandMargin(gd)) return;
101+
98102
var gs = fullLayout._size;
99103
var bw = opts.borderwidth;
100104

@@ -632,8 +636,7 @@ function expandMargin(gd) {
632636
var xanchor = getXanchor(opts);
633637
var yanchor = getYanchor(opts);
634638

635-
636-
Plots.autoMargin(gd, 'legend', {
639+
return Plots.autoMargin(gd, 'legend', {
637640
x: opts.x,
638641
y: opts.y,
639642
l: opts._width * (FROM_TL[xanchor]),

src/plots/plots.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ plots.autoMargin = function(gd, id, o) {
18631863
}
18641864

18651865
if(!fullLayout._replotting) {
1866-
plots.doAutoMargin(gd);
1866+
return plots.doAutoMargin(gd);
18671867
}
18681868
}
18691869
};

test/jasmine/tests/legend_test.js

+28
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,34 @@ describe('legend relayout update', function() {
715715
.then(done);
716716
});
717717
});
718+
719+
it('should make legend fit in graph viewport', function(done) {
720+
var fig = Lib.extendDeep({}, require('@mocks/legend_negative_x.json'));
721+
722+
function _assert(msg, xy, wh) {
723+
return function() {
724+
var fullLayout = gd._fullLayout;
725+
var legend3 = d3.select('g.legend');
726+
var bg3 = legend3.select('rect.bg');
727+
var translate = Drawing.getTranslate(legend3);
728+
var x = translate.x;
729+
var y = translate.y;
730+
var w = +bg3.attr('width');
731+
var h = +bg3.attr('height');
732+
expect([x, y]).toBeWithinArray(xy, 25, msg + '| legend x,y');
733+
expect([w, h]).toBeWithinArray(wh, 25, msg + '| legend w,h');
734+
expect(x + w <= fullLayout.width).toBe(true, msg + '| fits in x');
735+
expect(y + h <= fullLayout.height).toBe(true, msg + '| fits in y');
736+
};
737+
}
738+
739+
Plotly.plot(gd, fig)
740+
.then(_assert('base', [5, 4.4], [512, 29]))
741+
.then(function() { return Plotly.relayout(gd, 'legend.x', 0.8); })
742+
.then(_assert('after relayout almost to right edge', [188, 4.4], [512, 29]))
743+
.catch(failTest)
744+
.then(done);
745+
});
718746
});
719747

720748
describe('legend orientation change:', function() {

0 commit comments

Comments
 (0)