@@ -1071,25 +1071,38 @@ function createHoverText(hoverData, opts, gd) {
10711071 legendDraw ( gd , mockLegend ) ;
10721072
10731073 // Position the hover
1074- var ly = axLetter === 'y' ?
1075- Math . min . apply ( null , hoverData . map ( function ( c ) { return c . y1 ; } ) ) :
1076- Lib . mean ( hoverData . map ( function ( c ) { return ( c . y0 + c . y1 ) / 2 ; } ) ) ;
1077- var lx = axLetter === 'x' ?
1078- Math . max . apply ( null , hoverData . map ( function ( c ) { return c . x1 ; } ) ) :
1079- Lib . mean ( hoverData . map ( function ( c ) { return ( c . x0 + c . x1 ) / 2 ; } ) ) ;
1074+ var ly ;
1075+ if ( axLetter === 'y' ) {
1076+ ly = Math . min . apply ( null , hoverData . map ( function ( c ) { return c . y1 ; } ) ) ;
1077+ } else {
1078+ ly = Lib . mean ( hoverData . map ( function ( c ) { return ( c . y0 + c . y1 ) / 2 ; } ) ) ;
1079+ }
1080+
1081+ var lxRight , lxLeft ;
1082+ 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 ; } ) ) ;
1085+ } else {
1086+ lxRight = lxLeft = Lib . mean ( hoverData . map ( function ( c ) { return ( c . x0 + c . x1 ) / 2 ; } ) ) ;
1087+ }
10801088
10811089 var legendContainer = container . select ( 'g.legend' ) ;
10821090 var tbb = legendContainer . node ( ) . getBoundingClientRect ( ) ;
1083- lx += xa . _offset ;
1091+ lxRight += xa . _offset ;
1092+ lxLeft += xa . _offset ;
10841093 ly += ya . _offset - tbb . height / 2 ;
10851094
1095+ var lx = lxRight ;
1096+
10861097 // Change horizontal alignment to end up on screen
10871098 var txWidth = tbb . width + 2 * HOVERTEXTPAD ;
1088- var anchorStartOK = lx + txWidth <= outerWidth ;
1089- var anchorEndOK = lx - txWidth >= 0 ;
1090- if ( ! anchorStartOK && anchorEndOK ) {
1099+ var anchorRightOK = lxRight + txWidth <= outerWidth ;
1100+ var anchorLeftOK = lxLeft - txWidth >= 0 ;
1101+ if ( ! anchorRightOK && anchorLeftOK ) {
1102+ lx = lxLeft ;
10911103 lx -= txWidth ;
10921104 } else {
1105+ lx = lxRight ;
10931106 lx += 2 * HOVERTEXTPAD ;
10941107 }
10951108
0 commit comments