@@ -751,6 +751,9 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
751751 legendObj = fullLayout [ legendId ] ;
752752 }
753753 var gs = fullLayout . _size ;
754+ const plotBottom = gs . b ;
755+ const plotTop = gs . b + gs . h ;
756+ const plotHeight = gs . h ;
754757
755758 var isVertical = helpers . isVertical ( legendObj ) ;
756759 var isGrouped = helpers . isGrouped ( legendObj ) ;
@@ -770,31 +773,52 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
770773
771774 const { orientation, yref, y } = legendObj ;
772775 let { maxheight } = legendObj ;
776+ let yPixels ;
773777
774778 if ( yref === 'paper' ) {
775- isBelowPlotArea = y < 0 || ( y === 0 && yanchor === 'top' ) ;
776- isAbovePlotArea = y > 1 || ( y === 1 && yanchor === 'bottom' ) ;
777- } else {
778- const yPixels = legendObj . y * fullLayout . height ;
779- isBelowPlotArea = yPixels < gs . b || ( yPixels === gs . b && yanchor === 'top' ) ;
780- isAbovePlotArea = yPixels > gs . b + gs . h || ( yPixels === gs . b + gs . h && yanchor === 'bottom' ) ;
779+ // Calculate pixel value of y position
780+ yPixels = ( y * plotHeight ) + plotBottom ;
781+ // Check if legend anchor point is below or above plot area
782+ isBelowPlotArea = yPixels < plotBottom || ( yPixels === plotBottom && yanchor === 'top' ) ;
783+ 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+ }
781794 }
782-
783- const useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || orientation !== "v" || yref !== "paper" ;
784- // Set default maxheight here since it depends on values passed in by user
785- maxheight ||= useFullLayoutHeight ? 0.5 : 1 ;
786- const heightToBeScaled = useFullLayoutHeight ? fullLayout . height : gs . h ;
787- maxheight = maxheight > 1 ? maxheight : maxheight * heightToBeScaled ;
788-
789- if ( yref === 'container' ) {
790- const maxAvailableHeight = yanchor === 'top'
791- ? y * fullLayout . height
792- : yanchor === 'bottom'
793- ? ( 1 - y ) * fullLayout . height
794- : 2 * Math . min ( 1 - y , y ) * fullLayout . height ; // yanchor is 'middle' or 'auto'
795- maxheight = Math . min ( maxheight , maxAvailableHeight ) ;
795+ else {
796+ // Calculate pixel value of y position
797+ yPixels = y * fullLayout . height ;
798+ // Check if legend anchor point is below or above plot area
799+ isBelowPlotArea = yPixels < plotBottom || ( yPixels === plotBottom && yanchor === 'top' ) ;
800+ 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+ }
808+ }
809+
810+ // Calculate the maximum available height based on the anchor point
811+ let maxAvailableHeight ;
812+ if ( yanchor === 'top' ) {
813+ maxAvailableHeight = yPixels ;
814+ } else if ( yanchor === 'bottom' ) {
815+ maxAvailableHeight = fullLayout . height - yPixels ;
816+ } else {
817+ // If yanchor is 'middle'
818+ maxAvailableHeight = 2 * Math . min ( yPixels , fullLayout . height - yPixels ) ;
796819 }
797820
821+ maxheight = Math . min ( maxheight , maxAvailableHeight ) ;
798822 legendObj . _maxHeight = Math . max ( maxheight , 30 ) ;
799823
800824 var toggleRectWidth = 0 ;
0 commit comments