Skip to content

Commit 09cb679

Browse files
committed
Add support for reservedMargins when container ref'd
1 parent 2454271 commit 09cb679

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

src/components/legend/draw.js

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,39 @@ function drawOne(gd, opts) {
154154
function() {
155155
var gs = fullLayout._size;
156156
var bw = legendObj.borderwidth;
157+
var isPaperX = legendObj.xref === 'paper';
158+
var isPaperY = legendObj.yref === 'paper';
157159

158160
if(!inHover) {
159-
var expMargin = expandMargin(gd, legendId);
160-
161-
// IF expandMargin return a Promise (which is truthy),
162-
// we're under a doAutoMargin redraw, so we don't have to
163-
// draw the remaining pieces below
164-
if(expMargin) return;
165-
166161
var lx, ly;
167162

168-
if(legendObj.xref === 'paper') {
163+
if(isPaperX) {
169164
lx = gs.l + gs.w * legendObj.x - FROM_TL[getXanchor(legendObj)] * legendObj._width;
170165
} else {
171166
legendObj.x = Lib.constrain(legendObj.x, 0, 1); // TODO: Move this to defaults setting?
172167
lx = fullLayout.width * legendObj.x - FROM_TL[getXanchor(legendObj)] * legendObj._width;
173168
}
174169

175-
if(legendObj.yref === 'paper') {
170+
if(isPaperY) {
176171
ly = gs.t + gs.h * (1 - legendObj.y) - FROM_TL[getYanchor(legendObj)] * legendObj._effHeight;
177172
} else {
178173
legendObj.y = Lib.constrain(legendObj.y, 0, 1); // TODO: Move this to defaults setting?
179174
ly = fullLayout.height * (1 - legendObj.y) - FROM_TL[getYanchor(legendObj)] * legendObj._effHeight;
180175
}
181176

182-
// TODO: Does this also apply if y/xref=container?
177+
var expMargin = expandMargin(gd, legendId, lx, ly);
178+
179+
// IF expandMargin return a Promise (which is truthy),
180+
// we're under a doAutoMargin redraw, so we don't have to
181+
// draw the remaining pieces below
182+
if(expMargin) return;
183+
183184
if(fullLayout.margin.autoexpand) {
184185
var lx0 = lx;
185186
var ly0 = ly;
186187

187-
lx = Lib.constrain(lx, 0, fullLayout.width - legendObj._width);
188-
ly = Lib.constrain(ly, 0, fullLayout.height - legendObj._effHeight);
188+
lx = isPaperX ? Lib.constrain(lx, 0, fullLayout.width - legendObj._width) : lx0;
189+
ly = isPaperY ? Lib.constrain(ly, 0, fullLayout.height - legendObj._effHeight) : ly0;
189190

190191
if(lx !== lx0) {
191192
Lib.log('Constrain ' + legendId + '.x to make legend fit inside graph');
@@ -890,20 +891,48 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
890891
});
891892
}
892893

893-
function expandMargin(gd, legendId) {
894+
function expandMargin(gd, legendId, lx, ly) {
894895
var fullLayout = gd._fullLayout;
895896
var legendObj = fullLayout[legendId];
896897
var xanchor = getXanchor(legendObj);
897898
var yanchor = getYanchor(legendObj);
898899

899-
return Plots.autoMargin(gd, legendId, {
900-
x: legendObj.x,
901-
y: legendObj.y,
902-
l: legendObj._width * (FROM_TL[xanchor]),
903-
r: legendObj._width * (FROM_BR[xanchor]),
904-
b: legendObj._effHeight * (FROM_BR[yanchor]),
905-
t: legendObj._effHeight * (FROM_TL[yanchor])
906-
});
900+
var isPaperX = legendObj.xref === 'paper';
901+
var isPaperY = legendObj.yref === 'paper';
902+
903+
gd._fullLayout._reservedMargin[legendId] = {};
904+
var sideY = legendObj.y < 0.5 ? 'b' : 't';
905+
var sideX = legendObj.x < 0.5 ? 'l' : 'r';
906+
var possibleReservedMargins = {
907+
r: (fullLayout.width - lx),
908+
l: lx + legendObj._width,
909+
b: (fullLayout.height - ly),
910+
t: ly + legendObj._effHeight
911+
};
912+
913+
if(isPaperX && isPaperY) {
914+
return Plots.autoMargin(gd, legendId, {
915+
x: legendObj.x,
916+
y: legendObj.y,
917+
l: legendObj._width * (FROM_TL[xanchor]),
918+
r: legendObj._width * (FROM_BR[xanchor]),
919+
b: legendObj._effHeight * (FROM_BR[yanchor]),
920+
t: legendObj._effHeight * (FROM_TL[yanchor])
921+
});
922+
} else if(isPaperX) {
923+
gd._fullLayout._reservedMargin[legendId][sideY] = possibleReservedMargins[sideY];
924+
return;
925+
} else if(isPaperY) {
926+
gd._fullLayout._reservedMargin[legendId][sideX] = possibleReservedMargins[sideX];
927+
return;
928+
} else {
929+
if(legendObj.orientation === 'v') {
930+
gd._fullLayout._reservedMargin[legendId][sideX] = possibleReservedMargins[sideX];
931+
} else {
932+
gd._fullLayout._reservedMargin[legendId][sideY] = possibleReservedMargins[sideY];
933+
}
934+
return;
935+
}
907936
}
908937

909938
function getXanchor(legendObj) {

0 commit comments

Comments
 (0)