Skip to content

Commit

Permalink
end Legend.draw early when margin-redraw in-progress
Browse files Browse the repository at this point in the history
... to avoid race conditions leading to wrong legend
    position and size.
  • Loading branch information
etpinard committed Sep 10, 2019
1 parent 35b0c92 commit 42d5e4e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ module.exports = function draw(gd) {
Lib.syncOrAsync([
Plots.previousPromises,
function() { return computeLegendDimensions(gd, groups, traces); },
function() { return expandMargin(gd); },
function() {
// IF expandMargin return a Promise (which is truthy),
// we're under a doAutoMargin redraw, so we don't have to
// draw the remaining pieces below
if(expandMargin(gd)) return;

var gs = fullLayout._size;
var bw = opts.borderwidth;

Expand Down Expand Up @@ -632,8 +636,7 @@ function expandMargin(gd) {
var xanchor = getXanchor(opts);
var yanchor = getYanchor(opts);


Plots.autoMargin(gd, 'legend', {
return Plots.autoMargin(gd, 'legend', {
x: opts.x,
y: opts.y,
l: opts._width * (FROM_TL[xanchor]),
Expand Down
2 changes: 1 addition & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ plots.autoMargin = function(gd, id, o) {
}

if(!fullLayout._replotting) {
plots.doAutoMargin(gd);
return plots.doAutoMargin(gd);
}
}
};
Expand Down
28 changes: 28 additions & 0 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,34 @@ describe('legend relayout update', function() {
.then(done);
});
});

it('should make legend fit in graph viewport', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/legend_negative_x.json'));

function _assert(msg, xy, wh) {
return function() {
var fullLayout = gd._fullLayout;
var legend3 = d3.select('g.legend');
var bg3 = legend3.select('rect.bg');
var translate = Drawing.getTranslate(legend3);
var x = translate.x;
var y = translate.y;
var w = +bg3.attr('width');
var h = +bg3.attr('height');
expect([x, y]).toBeWithinArray(xy, 25, msg + '| legend x,y');
expect([w, h]).toBeWithinArray(wh, 25, msg + '| legend w,h');
expect(x + w <= fullLayout.width).toBe(true, msg + '| fits in x');
expect(y + h <= fullLayout.height).toBe(true, msg + '| fits in y');
};
}

Plotly.plot(gd, fig)
.then(_assert('base', [5, 4.4], [512, 29]))
.then(function() { return Plotly.relayout(gd, 'legend.x', 0.8); })
.then(_assert('after relayout almost to right edge', [188, 4.4], [512, 29]))
.catch(failTest)
.then(done);
});
});

describe('legend orientation change:', function() {
Expand Down

0 comments on commit 42d5e4e

Please sign in to comment.