Skip to content

refactor legend draw #5999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2021
Merged
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
46 changes: 25 additions & 21 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,39 @@ function _draw(gd, legendObj) {
Plots.previousPromises,
function() { return computeLegendDimensions(gd, groups, traces, legendObj); },
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(!inHover && expandMargin(gd)) return;

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

var lx = gs.l + gs.w * legendObj.x - FROM_TL[getXanchor(legendObj)] * legendObj._width;
var ly = gs.t + gs.h * (1 - legendObj.y) - FROM_TL[getYanchor(legendObj)] * legendObj._effHeight;
if(!inHover) {
var expMargin = expandMargin(gd);

if(!inHover && fullLayout.margin.autoexpand) {
var lx0 = lx;
var ly0 = ly;
// 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(expMargin) return;

lx = Lib.constrain(lx, 0, fullLayout.width - legendObj._width);
ly = Lib.constrain(ly, 0, fullLayout.height - legendObj._effHeight);
var lx = gs.l + gs.w * legendObj.x - FROM_TL[getXanchor(legendObj)] * legendObj._width;
var ly = gs.t + gs.h * (1 - legendObj.y) - FROM_TL[getYanchor(legendObj)] * legendObj._effHeight;

if(lx !== lx0) {
Lib.log('Constrain legend.x to make legend fit inside graph');
}
if(ly !== ly0) {
Lib.log('Constrain legend.y to make legend fit inside graph');
if(fullLayout.margin.autoexpand) {
var lx0 = lx;
var ly0 = ly;

lx = Lib.constrain(lx, 0, fullLayout.width - legendObj._width);
ly = Lib.constrain(ly, 0, fullLayout.height - legendObj._effHeight);

if(lx !== lx0) {
Lib.log('Constrain legend.x to make legend fit inside graph');
}
if(ly !== ly0) {
Lib.log('Constrain legend.y to make legend fit inside graph');
}
}
}

// Set size and position of all the elements that make up a legend:
// legend, background and border, scroll box and scroll bar as well as title
if(!inHover) Drawing.setTranslate(legend, lx, ly);
// Set size and position of all the elements that make up a legend:
// legend, background and border, scroll box and scroll bar as well as title
Drawing.setTranslate(legend, lx, ly);
}

// to be safe, remove previous listeners
scrollBar.on('.drag', null);
Expand Down