@@ -1088,6 +1088,8 @@ function createHoverText(hoverData, opts, gd) {
1088
1088
var tWidth = tbb . width + 2 * HOVERTEXTPAD ;
1089
1089
var tHeight = tbb . height + 2 * HOVERTEXTPAD ;
1090
1090
var winningPoint = hoverData [ 0 ] ;
1091
+ var avgX = ( winningPoint . x0 + winningPoint . x1 ) / 2 ;
1092
+ var avgY = ( winningPoint . y0 + winningPoint . y1 ) / 2 ;
1091
1093
// When a scatter (or e.g. heatmap) point wins, it's OK for the hovelabel to occlude the bar and other points.
1092
1094
var pointWon = ! (
1093
1095
Registry . traceIs ( winningPoint . trace , 'bar-like' ) ||
@@ -1097,8 +1099,8 @@ function createHoverText(hoverData, opts, gd) {
1097
1099
var lyBottom , lyTop ;
1098
1100
if ( axLetter === 'y' ) {
1099
1101
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 ;
1102
1104
} else {
1103
1105
lyTop = Math . min . apply ( null , hoverData . map ( function ( c ) { return Math . min ( c . y0 , c . y1 ) ; } ) ) ;
1104
1106
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) {
1110
1112
var lxRight , lxLeft ;
1111
1113
if ( axLetter === 'x' ) {
1112
1114
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 ;
1115
1117
} else {
1116
1118
lxRight = Math . max . apply ( null , hoverData . map ( function ( c ) { return Math . max ( c . x0 , c . x1 ) ; } ) ) ;
1117
1119
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) {
1137
1139
} else if ( xOffset + tWidth < outerWidth ) {
1138
1140
lx = xOffset ; // subplot left corner
1139
1141
} 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
+ }
1141
1148
}
1142
1149
lx += HOVERTEXTPAD ;
1143
1150
@@ -1149,7 +1156,12 @@ function createHoverText(hoverData, opts, gd) {
1149
1156
} else if ( yOffset + tHeight < outerHeight ) {
1150
1157
ly = yOffset ; // subplot top corner
1151
1158
} 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
+ }
1153
1165
}
1154
1166
ly += HOVERTEXTPAD ;
1155
1167
0 commit comments