Skip to content

Commit c0e6fd6

Browse files
committed
Refactor legend maxheight/maxwidth computation for readability
1 parent 5be429b commit c0e6fd6

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

‎src/components/legend/draw.js‎

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -772,39 +772,36 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
772772
var legendGroupWidths = {};
773773

774774
const { orientation, yref, y } = legendObj;
775-
let { maxheight } = legendObj;
775+
let { maxheight, maxwidth } = legendObj;
776776
let yPixels;
777+
let referenceHeight;
778+
let useFullLayoutHeight;
777779

778780
if (yref === 'paper') {
779781
// Calculate pixel value of y position
780782
yPixels = (y * plotHeight) + plotBottom;
781783
// Check if legend anchor point is below or above plot area
782784
isBelowPlotArea = yPixels < plotBottom || (yPixels === plotBottom && yanchor === 'top');
783785
isAbovePlotArea = yPixels > plotTop || (yPixels === plotTop && yanchor === 'bottom');
784-
console.log(yPixels, plotBottom,isBelowPlotArea, isAbovePlotArea);
785-
786-
const useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || orientation !== "v";
787-
// Set default maxheight if not provided by user
788-
maxheight ||= useFullLayoutHeight ? 0.5 : 1;
789-
// Convert maxheight to pixels if it's a ratio (≤1), otherwise use as-is
790-
if (maxheight <= 1) {
791-
const referenceHeight = useFullLayoutHeight ? fullLayout.height : plotHeight;
792-
maxheight = maxheight * referenceHeight;
793-
}
786+
useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || orientation !== "v";
787+
// Use the appropriate reference height
788+
referenceHeight = useFullLayoutHeight ? fullLayout.height : plotHeight;
794789
}
795790
else {
796791
// Calculate pixel value of y position
797792
yPixels = y * fullLayout.height;
798793
// Check if legend anchor point is below or above plot area
799794
isBelowPlotArea = yPixels < plotBottom || (yPixels === plotBottom && yanchor === 'top');
800795
isAbovePlotArea = yPixels > plotTop || (yPixels === plotTop && yanchor === 'bottom');
801-
802-
// Set default maxheight if not provided by user
803-
maxheight ||= 0.5;
804-
// If maxheight is greater than 1 (pixel value), use as is, otherwise multiply by the full layout height
805-
if (maxheight <= 1) {
806-
maxheight = maxheight * fullLayout.height;
807-
}
796+
// Use the container height as the reference height
797+
referenceHeight = fullLayout.height;
798+
}
799+
800+
// Set default maxheight if not provided by user
801+
maxheight ||= (yref === 'paper' && !useFullLayoutHeight) ? 1 : 0.5;
802+
// Convert maxheight to pixels if it's a ratio (≤1), otherwise use as-is
803+
if (maxheight <= 1) {
804+
maxheight = maxheight * referenceHeight;
808805
}
809806

810807
// Calculate the maximum available height based on the anchor point
@@ -865,12 +862,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
865862
2 * textGap);
866863
} else {
867864
if(xanchor === 'right')
868-
legendObj._maxWidth = legendObj.x * fullLayout.width;
865+
maxwidth = legendObj.x * fullLayout.width;
869866
else if(xanchor === 'left')
870-
legendObj._maxWidth = (1 - legendObj.x) * fullLayout.width;
867+
maxwidth = (1 - legendObj.x) * fullLayout.width;
871868
else // if (xanchor === 'center')
872-
legendObj._maxWidth = 2 * Math.min(1 - legendObj.x, legendObj.x) * fullLayout.width;
873-
legendObj._maxWidth = Math.max(legendObj._maxWidth, 2 * textGap);
869+
maxwidth = 2 * Math.min(1 - legendObj.x, legendObj.x) * fullLayout.width;
870+
legendObj._maxWidth = Math.max(maxwidth, 2 * textGap);
874871
}
875872

876873
var maxItemWidth = 0;

0 commit comments

Comments
 (0)