@@ -53,6 +53,10 @@ var cartesianScatterPoints = {
5353 splom : true
5454} ;
5555
56+ function distanceSort ( a , b ) {
57+ return a . distance - b . distance ;
58+ }
59+
5660// fx.hover: highlight data on hover
5761// evt can be a mousemove event, or an object with data about what points
5862// to hover on
@@ -270,15 +274,21 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
270274 var hovermodeHasX = ( hovermode || '' ) . charAt ( 0 ) === 'x' ;
271275 var hovermodeHasY = ( hovermode || '' ) . charAt ( 0 ) === 'y' ;
272276
277+ var firstXaxis ;
278+ var firstYaxis ;
279+
273280 if ( hasCartesian && ( hovermodeHasX || hovermodeHasY ) && hoversubplots === 'axis' ) {
274281 var subplotsLength = subplots . length ;
275282 for ( var p = 0 ; p < subplotsLength ; p ++ ) {
276283 spId = subplots [ p ] ;
277284 if ( plots [ spId ] ) {
278285 // 'cartesian' case
279286
287+ firstXaxis = Axes . getFromId ( gd , spId , 'x' ) ;
288+ firstYaxis = Axes . getFromId ( gd , spId , 'y' ) ;
289+
280290 var subplotsWith = (
281- Axes . getFromId ( gd , spId , hovermodeHasX ? 'x' : 'y' )
291+ hovermodeHasX ? firstXaxis : firstYaxis
282292 ) . _subplotsWith ;
283293
284294 if ( subplotsWith && subplotsWith . length ) {
@@ -661,6 +671,9 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
661671 var thisSpikeDistance ;
662672
663673 for ( var i = 0 ; i < pointsData . length ; i ++ ) {
674+ if ( firstXaxis && firstXaxis . _id !== pointsData [ i ] . xa . _id ) continue ;
675+ if ( firstYaxis && firstYaxis . _id !== pointsData [ i ] . ya . _id ) continue ;
676+
664677 thisSpikeDistance = pointsData [ i ] . spikeDistance ;
665678 if ( spikeOnWinning && i === 0 ) thisSpikeDistance = - Infinity ;
666679
@@ -700,9 +713,23 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
700713 gd . _spikepoints = newspikepoints ;
701714
702715 var sortHoverData = function ( ) {
703- if ( hoversubplots !== 'axis' ) {
704- hoverData . sort ( function ( d1 , d2 ) { return d1 . distance - d2 . distance ; } ) ;
705- }
716+ var hoverDataInSubplot = hoverData . filter ( function ( a ) {
717+ return (
718+ ( firstXaxis && firstXaxis . _id === a . xa . _id ) &&
719+ ( firstYaxis && firstYaxis . _id === a . ya . _id )
720+ ) ;
721+ } ) ;
722+
723+ var hoverDataOutSubplot = hoverData . filter ( function ( a ) {
724+ return ! (
725+ ( firstXaxis && firstXaxis . _id === a . xa . _id ) &&
726+ ( firstYaxis && firstYaxis . _id === a . ya . _id )
727+ ) ;
728+ } ) ;
729+
730+ hoverDataInSubplot . sort ( distanceSort ) ;
731+ hoverDataOutSubplot . sort ( distanceSort ) ;
732+ hoverData = hoverDataInSubplot . concat ( hoverDataOutSubplot ) ;
706733
707734 // move period positioned points and box/bar-like traces to the end of the list
708735 hoverData = orderRangePoints ( hoverData , hovermode ) ;
0 commit comments