Skip to content

Commit 5f9ec5a

Browse files
committed
Limit legend size caps to container references
1 parent 8d98c89 commit 5f9ec5a

2 files changed

Lines changed: 72 additions & 73 deletions

File tree

‎src/components/legend/draw.js‎

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -865,9 +865,6 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
865865
legendObj = fullLayout[legendId];
866866
}
867867
var gs = fullLayout._size;
868-
const plotBottom = gs.b;
869-
const plotTop = gs.b + gs.h;
870-
const plotHeight = gs.h;
871868

872869
var isVertical = helpers.isVertical(legendObj);
873870
var isGrouped = helpers.isGrouped(legendObj);
@@ -880,58 +877,25 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
880877
var endPad = 2 * (bw + itemGap);
881878

882879
var yanchor = getYanchor(legendObj);
883-
let isBelowPlotArea, isAbovePlotArea;
880+
var isBelowPlotArea = legendObj.y < 0 || (legendObj.y === 0 && yanchor === 'top');
881+
var isAbovePlotArea = legendObj.y > 1 || (legendObj.y === 1 && yanchor === 'bottom');
884882

885883
var traceGroupGap = legendObj.tracegroupgap;
886884
var legendGroupWidths = {};
887885

888-
const { orientation, yref, y } = legendObj;
889-
let { maxheight, maxwidth } = legendObj;
890-
let yPixels;
891-
let referenceHeight;
892-
let useFullLayoutHeight;
893-
894-
if (yref === 'paper') {
895-
// Calculate pixel value of y position
896-
yPixels = (y * plotHeight) + plotBottom;
897-
// Check if legend anchor point is below or above plot area
898-
isBelowPlotArea = yPixels < plotBottom || (yPixels === plotBottom && yanchor === 'top');
899-
isAbovePlotArea = yPixels > plotTop || (yPixels === plotTop && yanchor === 'bottom');
900-
useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || orientation !== "v";
901-
// Use the appropriate reference height
902-
referenceHeight = useFullLayoutHeight ? fullLayout.height : plotHeight;
886+
const { orientation, yref } = legendObj;
887+
let { maxheight } = legendObj;
888+
const useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || orientation !== "v" || yref !== "paper"
889+
// Set default maxheight here since it depends on values passed in by user
890+
maxheight ||= useFullLayoutHeight ? 0.5 : 1;
891+
const heightToBeScaled = useFullLayoutHeight ? fullLayout.height : gs.h;
892+
legendObj._maxHeight = Math.max(maxheight > 1 ? maxheight : maxheight * heightToBeScaled, 30);
893+
894+
// Margins do not expand for a container legend, so keep the legend inside the container
895+
if (yref === 'container') {
896+
const roomHeight = getContainerRoom(legendObj.y, yanchor) * fullLayout.height;
897+
legendObj._maxHeight = Math.max(Math.min(legendObj._maxHeight, roomHeight), 30);
903898
}
904-
else {
905-
// Calculate pixel value of y position
906-
yPixels = y * fullLayout.height;
907-
// Check if legend anchor point is below or above plot area
908-
isBelowPlotArea = yPixels < plotBottom || (yPixels === plotBottom && yanchor === 'top');
909-
isAbovePlotArea = yPixels > plotTop || (yPixels === plotTop && yanchor === 'bottom');
910-
// Use the container height as the reference height
911-
useFullLayoutHeight = false;
912-
referenceHeight = fullLayout.height;
913-
}
914-
915-
// Set default maxheight if not provided by user
916-
maxheight ||= (useFullLayoutHeight) ? 0.5 : 1;
917-
// Convert maxheight to pixels if it's a ratio (≤1), otherwise use as-is
918-
if (maxheight <= 1) {
919-
maxheight = maxheight * referenceHeight;
920-
}
921-
922-
// Calculate the maximum available height based on the anchor point
923-
let maxAvailableHeight;
924-
if (yanchor === 'top') {
925-
maxAvailableHeight = yPixels;
926-
} else if (yanchor === 'bottom') {
927-
maxAvailableHeight = fullLayout.height - yPixels;
928-
} else {
929-
// If yanchor is 'middle'
930-
maxAvailableHeight = 2 * Math.min(yPixels, fullLayout.height - yPixels);
931-
}
932-
933-
maxheight = Math.min(maxheight, maxAvailableHeight);
934-
legendObj._maxHeight = Math.max(maxheight, 30);
935899

936900
var toggleRectWidth = 0;
937901
legendObj._width = 0;
@@ -961,28 +925,24 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
961925
}
962926
} else {
963927
var xanchor = getXanchor(legendObj);
964-
if(legendObj.xref === 'paper') {
965-
var isLeftOfPlotArea = legendObj.x < 0 || (legendObj.x === 0 && xanchor === 'right');
966-
var isRightOfPlotArea = legendObj.x > 1 || (legendObj.x === 1 && xanchor === 'left');
967-
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea;
968-
var hw = fullLayout.width / 2;
969-
970-
// - if placed within x-margins, extend the width of the plot area
971-
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
972-
// - otherwise give it the maximum potential margin-push value
973-
legendObj._maxWidth = Math.max(
974-
isLeftOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'left') ? gs.l + gs.w : hw) :
975-
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
976-
gs.w,
977-
2 * textGap);
978-
} else {
979-
if(xanchor === 'right')
980-
maxwidth = legendObj.x * fullLayout.width;
981-
else if(xanchor === 'left')
982-
maxwidth = (1 - legendObj.x) * fullLayout.width;
983-
else // if (xanchor === 'center')
984-
maxwidth = 2 * Math.min(1 - legendObj.x, legendObj.x) * fullLayout.width;
985-
legendObj._maxWidth = Math.max(maxwidth, 2 * textGap);
928+
var isLeftOfPlotArea = legendObj.x < 0 || (legendObj.x === 0 && xanchor === 'right');
929+
var isRightOfPlotArea = legendObj.x > 1 || (legendObj.x === 1 && xanchor === 'left');
930+
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea;
931+
var hw = fullLayout.width / 2;
932+
933+
// - if placed within x-margins, extend the width of the plot area
934+
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
935+
// - otherwise give it the maximum potential margin-push value
936+
legendObj._maxWidth = Math.max(
937+
isLeftOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'left') ? gs.l + gs.w : hw) :
938+
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
939+
gs.w,
940+
2 * textGap);
941+
942+
// The plot-area rules above do not apply to a container legend, so use the room on its anchor side
943+
if (legendObj.xref === 'container') {
944+
const roomWidth = getContainerRoom(legendObj.x, xanchor) * fullLayout.width;
945+
legendObj._maxWidth = Math.max(roomWidth, 2 * textGap);
986946
}
987947

988948
var maxItemWidth = 0;
@@ -1202,3 +1162,23 @@ function getYanchor(legendObj) {
12021162
Lib.isMiddleAnchor(legendObj) ? 'middle' :
12031163
'top';
12041164
}
1165+
1166+
/**
1167+
* Get the fraction of the container that a legend has room to fill on its anchor side.
1168+
*
1169+
* @param pos - Legend `x` or `y` as a fraction of the container
1170+
* @param anchor - Legend anchor from `getXanchor` or `getYanchor`
1171+
* @returns The room as a fraction of the container width or height
1172+
*/
1173+
function getContainerRoom(pos, anchor) {
1174+
switch (anchor) {
1175+
case 'left':
1176+
case 'bottom':
1177+
return 1 - pos;
1178+
case 'right':
1179+
case 'top':
1180+
return pos;
1181+
default:
1182+
return 2 * Math.min(pos, 1 - pos);
1183+
}
1184+
}

‎test/jasmine/tests/legend_test.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ describe('legend relayout update', function () {
16191619
}
16201620

16211621
Plotly.newPlot(gd, fig)
1622-
.then(_assert('base', [539, 0], [91, 390]))
1622+
.then(_assert('base', [539, 0], [101, 225]))
16231623
.then(function() { return Plotly.relayout(gd, 'legend.y', 0.6); })
16241624
.then(function() { return Plotly.relayout(gd, 'legend.yanchor', 'bottom'); })
16251625
.then(_assert('after moving legend towards the top edge', [539, 0], [101, 180]))
@@ -1628,6 +1628,25 @@ describe('legend relayout update', function () {
16281628
.then(_assert('after moving legend towards the bottom edge', [539, 270], [101, 180]))
16291629
.then(done, done.fail);
16301630
});
1631+
1632+
it('should constrain horizontal legend width when xref is container', async () => {
1633+
const fig = {
1634+
data: Array.from({ length: 20 }, () => ({ x: [1, 2, 3], y: [1, 2, 3] })),
1635+
layout: {
1636+
legend: { orientation: 'h', xref: 'container', yref: 'container', x: 0.5 }
1637+
}
1638+
};
1639+
1640+
await Plotly.newPlot(gd, fig);
1641+
1642+
const { width } = gd._fullLayout;
1643+
const legend = d3Select('g.legend');
1644+
const x = Drawing.getTranslate(legend).x;
1645+
const w = +legend.select('rect.bg').attr('width');
1646+
1647+
expect(x).toBeCloseTo(width / 2, 0, 'legend x');
1648+
expect(x + w).toBeLessThanOrEqual(width, 'fits in x');
1649+
});
16311650
});
16321651

16331652
describe('legend orientation change:', function () {

0 commit comments

Comments
 (0)