Skip to content

Commit ac37c68

Browse files
authored
Merge pull request #5999 from plotly/refactor-legend-expand
refactor legend draw
2 parents ed3823b + befabea commit ac37c68

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/components/legend/draw.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,39 @@ function _draw(gd, legendObj) {
123123
Plots.previousPromises,
124124
function() { return computeLegendDimensions(gd, groups, traces, legendObj); },
125125
function() {
126-
// IF expandMargin return a Promise (which is truthy),
127-
// we're under a doAutoMargin redraw, so we don't have to
128-
// draw the remaining pieces below
129-
if(!inHover && expandMargin(gd)) return;
130-
131126
var gs = fullLayout._size;
132127
var bw = legendObj.borderwidth;
133128

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

137-
if(!inHover && fullLayout.margin.autoexpand) {
138-
var lx0 = lx;
139-
var ly0 = ly;
132+
// IF expandMargin return a Promise (which is truthy),
133+
// we're under a doAutoMargin redraw, so we don't have to
134+
// draw the remaining pieces below
135+
if(expMargin) return;
140136

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

144-
if(lx !== lx0) {
145-
Lib.log('Constrain legend.x to make legend fit inside graph');
146-
}
147-
if(ly !== ly0) {
148-
Lib.log('Constrain legend.y to make legend fit inside graph');
140+
if(fullLayout.margin.autoexpand) {
141+
var lx0 = lx;
142+
var ly0 = ly;
143+
144+
lx = Lib.constrain(lx, 0, fullLayout.width - legendObj._width);
145+
ly = Lib.constrain(ly, 0, fullLayout.height - legendObj._effHeight);
146+
147+
if(lx !== lx0) {
148+
Lib.log('Constrain legend.x to make legend fit inside graph');
149+
}
150+
if(ly !== ly0) {
151+
Lib.log('Constrain legend.y to make legend fit inside graph');
152+
}
149153
}
150-
}
151154

152-
// Set size and position of all the elements that make up a legend:
153-
// legend, background and border, scroll box and scroll bar as well as title
154-
if(!inHover) Drawing.setTranslate(legend, lx, ly);
155+
// Set size and position of all the elements that make up a legend:
156+
// legend, background and border, scroll box and scroll bar as well as title
157+
Drawing.setTranslate(legend, lx, ly);
158+
}
155159

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

0 commit comments

Comments
 (0)