@@ -313,9 +313,15 @@ function hover(gd, evt, subplot){
313
313
314
314
var fullLayout = gd . _fullLayout ,
315
315
plotinfo = fullLayout . _plots [ subplot ] ,
316
+ //If the user passed in an array of subplots, use those instead of finding overlayed plots
317
+ subplots = ( Object . prototype . toString . call ( subplot ) == "[object Array]" ) ?
318
+ subplot :
319
+
320
+ subplots = [ subplot ] . concat ( plotinfo . overlays
321
+ . map ( function ( pi ) { return pi . id ; } ) ) ,
322
+
316
323
// list of all overlaid subplots to look at
317
- subplots = [ subplot ] . concat ( plotinfo . overlays
318
- . map ( function ( pi ) { return pi . id ; } ) ) ,
324
+
319
325
xaArray = subplots . map ( function ( spId ) {
320
326
return Plotly . Axes . getFromId ( gd , spId , 'x' ) ;
321
327
} ) ,
@@ -533,7 +539,7 @@ function hover(gd, evt, subplot){
533
539
} ;
534
540
var hoverLabels = createHoverText ( hoverData , labelOpts ) ;
535
541
536
- hoverAvoidOverlaps ( hoverData , rotateLabels ? xaArray [ 0 ] : yaArray [ 0 ] ) ;
542
+ hoverAvoidOverlaps ( hoverData , rotateLabels ? "xa" : "ya" ) ;
537
543
538
544
alignHoverText ( hoverLabels , rotateLabels ) ;
539
545
@@ -866,7 +872,7 @@ function createHoverText(hoverData, opts) {
866
872
// first create the objects
867
873
var hoverLabels = container . selectAll ( 'g.hovertext' )
868
874
. data ( hoverData , function ( d ) {
869
- return [ d . trace . index , d . index , d . x0 , d . y0 , d . name , d . attr || '' ] . join ( ',' ) ;
875
+ return [ d . trace . index , d . index , d . x0 , d . y0 , d . name , d . attr , d . xa , d . ya || '' ] . join ( ',' ) ;
870
876
} ) ;
871
877
hoverLabels . enter ( ) . append ( 'g' )
872
878
. classed ( 'hovertext' , true )
@@ -969,8 +975,8 @@ function createHoverText(hoverData, opts) {
969
975
g . select ( 'path' )
970
976
. style ( { fill :traceColor , stroke :contrastColor } ) ;
971
977
var tbb = tx . node ( ) . getBoundingClientRect ( ) ,
972
- htx = xa . _offset + ( d . x0 + d . x1 ) / 2 ,
973
- hty = ya . _offset + ( d . y0 + d . y1 ) / 2 ,
978
+ htx = d . xa . _offset + ( d . x0 + d . x1 ) / 2 ,
979
+ hty = d . ya . _offset + ( d . y0 + d . y1 ) / 2 ,
974
980
dx = Math . abs ( d . x1 - d . x0 ) ,
975
981
dy = Math . abs ( d . y1 - d . y0 ) ,
976
982
txTotalWidth = tbb . width + HOVERARROWSIZE + HOVERTEXTPAD + tx2width ,
@@ -1033,18 +1039,19 @@ function createHoverText(hoverData, opts) {
1033
1039
// information then.
1034
1040
function hoverAvoidOverlaps ( hoverData , ax ) {
1035
1041
var nummoves = 0 ,
1036
- pmin = ax . _offset ,
1037
- pmax = ax . _offset + ax . _length ,
1038
1042
1039
1043
// make groups of touching points
1040
1044
pointgroups = hoverData
1041
1045
. map ( function ( d , i ) {
1046
+ var axis = d [ ax ] ;
1042
1047
return [ {
1043
1048
i : i ,
1044
1049
dp : 0 ,
1045
1050
pos : d . pos ,
1046
1051
posref : d . posref ,
1047
- size : d . by * ( ax . _id . charAt ( 0 ) === 'x' ? YFACTOR : 1 ) / 2
1052
+ size : d . by * ( axis . _id . charAt ( 0 ) === 'x' ? YFACTOR : 1 ) / 2 ,
1053
+ pmin : axis . _offset ,
1054
+ pmax : axis . _offset + axis . _length
1048
1055
} ] ;
1049
1056
} )
1050
1057
. sort ( function ( a , b ) { return a [ 0 ] . posref - b [ 0 ] . posref ; } ) ,
@@ -1060,10 +1067,10 @@ function hoverAvoidOverlaps(hoverData, ax) {
1060
1067
maxPt = grp [ grp . length - 1 ] ;
1061
1068
1062
1069
// overlap with the top - positive vals are overlaps
1063
- topOverlap = pmin - minPt . pos - minPt . dp + minPt . size ;
1070
+ topOverlap = minPt . pmin - minPt . pos - minPt . dp + minPt . size ;
1064
1071
1065
1072
// overlap with the bottom - positive vals are overlaps
1066
- bottomOverlap = maxPt . pos + maxPt . dp + maxPt . size - pmax ;
1073
+ bottomOverlap = maxPt . pos + maxPt . dp + maxPt . size - minPt . pmax ;
1067
1074
1068
1075
// check for min overlap first, so that we always
1069
1076
// see the largest labels
@@ -1087,7 +1094,7 @@ function hoverAvoidOverlaps(hoverData, ax) {
1087
1094
var deleteCount = 0 ;
1088
1095
for ( i = 0 ; i < grp . length ; i ++ ) {
1089
1096
pti = grp [ i ] ;
1090
- if ( pti . pos + pti . dp + pti . size > pmax ) deleteCount ++ ;
1097
+ if ( pti . pos + pti . dp + pti . size > minPt . pmax ) deleteCount ++ ;
1091
1098
}
1092
1099
1093
1100
// start by deleting points whose data is off screen
@@ -1097,7 +1104,7 @@ function hoverAvoidOverlaps(hoverData, ax) {
1097
1104
1098
1105
// pos has already been constrained to [pmin,pmax]
1099
1106
// so look for points close to that to delete
1100
- if ( pti . pos > pmax - 1 ) {
1107
+ if ( pti . pos > minPt . pmax - 1 ) {
1101
1108
pti . del = true ;
1102
1109
deleteCount -- ;
1103
1110
}
@@ -1108,7 +1115,7 @@ function hoverAvoidOverlaps(hoverData, ax) {
1108
1115
1109
1116
// pos has already been constrained to [pmin,pmax]
1110
1117
// so look for points close to that to delete
1111
- if ( pti . pos < pmin + 1 ) {
1118
+ if ( pti . pos < minPt . pmin + 1 ) {
1112
1119
pti . del = true ;
1113
1120
deleteCount -- ;
1114
1121
@@ -1121,7 +1128,7 @@ function hoverAvoidOverlaps(hoverData, ax) {
1121
1128
for ( i = grp . length - 1 ; i >= 0 ; i -- ) {
1122
1129
if ( deleteCount <= 0 ) break ;
1123
1130
pti = grp [ i ] ;
1124
- if ( pti . pos + pti . dp + pti . size > pmax ) {
1131
+ if ( pti . pos + pti . dp + pti . size > minPt . pmax ) {
1125
1132
pti . del = true ;
1126
1133
deleteCount -- ;
1127
1134
}
@@ -1149,7 +1156,9 @@ function hoverAvoidOverlaps(hoverData, ax) {
1149
1156
p0 = g0 [ g0 . length - 1 ] ,
1150
1157
p1 = g1 [ 0 ] ;
1151
1158
topOverlap = p0 . pos + p0 . dp + p0 . size - p1 . pos - p1 . dp + p1 . size ;
1152
- if ( topOverlap > 0.01 ) {
1159
+
1160
+ //Only group points that lie on the same axes
1161
+ if ( topOverlap > 0.01 && ( p0 . pmin == p1 . pmin ) && ( p0 . pmax == p1 . pmax ) ) {
1153
1162
// push the new point(s) added to this group out of the way
1154
1163
for ( j = g1 . length - 1 ; j >= 0 ; j -- ) g1 [ j ] . dp += topOverlap ;
1155
1164
0 commit comments