@@ -1071,28 +1071,33 @@ function createHoverText(hoverData, opts, gd) {
10711071 legendDraw ( gd , mockLegend ) ;
10721072
10731073 // Position the hover
1074- var ly ;
1074+ var lyBottom , lyTop ;
10751075 if ( axLetter === 'y' ) {
1076- ly = Math . min . apply ( null , hoverData . map ( function ( c ) { return c . y1 ; } ) ) ;
1076+ lyTop = Math . min . apply ( null , hoverData . map ( function ( c ) { return Math . min ( c . y0 , c . y1 ) ; } ) ) ;
1077+ lyBottom = Math . max . apply ( null , hoverData . map ( function ( c ) { return Math . max ( c . y0 , c . y1 ) ; } ) ) ;
10771078 } else {
1078- ly = Lib . mean ( hoverData . map ( function ( c ) { return ( c . y0 + c . y1 ) / 2 ; } ) ) ;
1079+ lyTop = lyBottom = Lib . mean ( hoverData . map ( function ( c ) { return ( c . y0 + c . y1 ) / 2 ; } ) ) ;
10791080 }
10801081
10811082 var lxRight , lxLeft ;
10821083 if ( axLetter === 'x' ) {
1083- lxRight = Math . max . apply ( null , hoverData . map ( function ( c ) { return c . x1 ; } ) ) ;
1084- lxLeft = Math . min . apply ( null , hoverData . map ( function ( c ) { return c . x0 ; } ) ) ;
1084+ lxRight = Math . max . apply ( null , hoverData . map ( function ( c ) { return Math . max ( c . x0 , c . x1 ) ; } ) ) ;
1085+ lxLeft = Math . min . apply ( null , hoverData . map ( function ( c ) { return Math . min ( c . x0 , c . x1 ) ; } ) ) ;
10851086 } else {
10861087 lxRight = lxLeft = Lib . mean ( hoverData . map ( function ( c ) { return ( c . x0 + c . x1 ) / 2 ; } ) ) ;
10871088 }
10881089
10891090 var legendContainer = container . select ( 'g.legend' ) ;
10901091 var tbb = legendContainer . node ( ) . getBoundingClientRect ( ) ;
1091- lxRight += xa . _offset ;
1092- lxLeft += xa . _offset ;
1093- ly += ya . _offset - tbb . height / 2 ;
1092+ var xOffset = xa . _offset ;
1093+ var yOffset = ya . _offset ;
1094+ lxRight += xOffset ;
1095+ lxLeft += xOffset ;
1096+ lyTop += yOffset ;
1097+ lyBottom += yOffset ;
10941098
10951099 var lx = lxRight ;
1100+ var ly = lyTop ;
10961101
10971102 // Change horizontal alignment to end up on screen
10981103 var txWidth = tbb . width + 2 * HOVERTEXTPAD ;
@@ -1108,18 +1113,17 @@ function createHoverText(hoverData, opts, gd) {
11081113
11091114 // Change vertical alignement to end up on screen
11101115 var txHeight = tbb . height + 2 * HOVERTEXTPAD ;
1111- var overflowTop = ly <= outerTop ;
1112- var overflowBottom = ly + txHeight >= outerHeight ;
1113- var canFit = txHeight <= outerHeight ;
1114- if ( canFit ) {
1115- if ( overflowTop ) {
1116- ly = ya . _offset + 2 * HOVERTEXTPAD ;
1117- } else if ( overflowBottom ) {
1118- ly = outerHeight - txHeight ;
1119- }
1116+ var anchorBottomOK = lyBottom + txHeight <= outerHeight ;
1117+ var anchorTopOK = lyTop - txHeight >= 0 ;
1118+ if ( ! anchorTopOK && anchorBottomOK ) {
1119+ ly = lyBottom ;
1120+ ly += 2 * HOVERTEXTPAD ;
1121+ } else {
1122+ ly = lyTop ;
1123+ ly -= txHeight ;
11201124 }
1121- legendContainer . attr ( 'transform' , strTranslate ( lx , ly ) ) ;
11221125
1126+ legendContainer . attr ( 'transform' , strTranslate ( lx , ly ) ) ;
11231127 return legendContainer ;
11241128 }
11251129
0 commit comments