From befabea376d96e96239d8d14b9ca8519dd1e9b4b Mon Sep 17 00:00:00 2001 From: archmoj Date: Fri, 22 Oct 2021 12:37:30 -0400 Subject: [PATCH] refactor legend --- src/components/legend/draw.js | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index ef87d4bd1c9..0f5e3538fe4 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -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);