@@ -1088,6 +1088,8 @@ function createHoverText(hoverData, opts, gd) {
10881088 var tWidth = tbb . width + 2 * HOVERTEXTPAD ;
10891089 var tHeight = tbb . height + 2 * HOVERTEXTPAD ;
10901090 var winningPoint = hoverData [ 0 ] ;
1091+ var avgX = ( winningPoint . x0 + winningPoint . x1 ) / 2 ;
1092+ var avgY = ( winningPoint . y0 + winningPoint . y1 ) / 2 ;
10911093 // When a scatter (or e.g. heatmap) point wins, it's OK for the hovelabel to occlude the bar and other points.
10921094 var pointWon = ! (
10931095 Registry . traceIs ( winningPoint . trace , 'bar-like' ) ||
@@ -1097,8 +1099,8 @@ function createHoverText(hoverData, opts, gd) {
10971099 var lyBottom , lyTop ;
10981100 if ( axLetter === 'y' ) {
10991101 if ( pointWon ) {
1100- lyTop = ( winningPoint . y0 + winningPoint . y1 ) / 2 - HOVERTEXTPAD ;
1101- lyBottom = ( winningPoint . y0 + winningPoint . y1 ) / 2 + HOVERTEXTPAD ;
1102+ lyTop = avgY - HOVERTEXTPAD ;
1103+ lyBottom = avgY + HOVERTEXTPAD ;
11021104 } else {
11031105 lyTop = Math . min . apply ( null , hoverData . map ( function ( c ) { return Math . min ( c . y0 , c . y1 ) ; } ) ) ;
11041106 lyBottom = Math . max . apply ( null , hoverData . map ( function ( c ) { return Math . max ( c . y0 , c . y1 ) ; } ) ) ;
@@ -1110,8 +1112,8 @@ function createHoverText(hoverData, opts, gd) {
11101112 var lxRight , lxLeft ;
11111113 if ( axLetter === 'x' ) {
11121114 if ( pointWon ) {
1113- lxRight = ( winningPoint . x0 + winningPoint . x1 ) / 2 + HOVERTEXTPAD ;
1114- lxLeft = ( winningPoint . x0 + winningPoint . x1 ) / 2 - HOVERTEXTPAD ;
1115+ lxRight = avgX + HOVERTEXTPAD ;
1116+ lxLeft = avgX - HOVERTEXTPAD ;
11151117 } else {
11161118 lxRight = Math . max . apply ( null , hoverData . map ( function ( c ) { return Math . max ( c . x0 , c . x1 ) ; } ) ) ;
11171119 lxLeft = Math . min . apply ( null , hoverData . map ( function ( c ) { return Math . min ( c . x0 , c . x1 ) ; } ) ) ;
@@ -1137,7 +1139,12 @@ function createHoverText(hoverData, opts, gd) {
11371139 } else if ( xOffset + tWidth < outerWidth ) {
11381140 lx = xOffset ; // subplot left corner
11391141 } else {
1140- lx = 0 ; // paper left corner
1142+ // closest left or right side of the paper
1143+ if ( lxRight - avgX < avgX - lxLeft + tWidth ) {
1144+ lx = outerWidth - tWidth ;
1145+ } else {
1146+ lx = 0 ;
1147+ }
11411148 }
11421149 lx += HOVERTEXTPAD ;
11431150
@@ -1149,7 +1156,12 @@ function createHoverText(hoverData, opts, gd) {
11491156 } else if ( yOffset + tHeight < outerHeight ) {
11501157 ly = yOffset ; // subplot top corner
11511158 } else {
1152- ly = 0 ; // paper top corner
1159+ // closest top or bottom side of the paper
1160+ if ( lyBottom - avgY < avgY - lyTop + tHeight ) {
1161+ ly = outerHeight - tHeight ;
1162+ } else {
1163+ ly = 0 ;
1164+ }
11531165 }
11541166 ly += HOVERTEXTPAD ;
11551167
0 commit comments