@@ -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+ }
0 commit comments