8000 Ensure winning points of hover are listed first when `hoversubplots` is set to "axis" and sorting by distance by archmoj · Pull Request #6963 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Ensure winning points of hover are listed first when hoversubplots is set to "axis" and sorting by distance #6963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix (x|y) spike position and ensure main subplot hover points are sor…
…ted first
  • Loading branch information
archmoj committed Apr 12, 2024
commit 0eaec35add44f533b565b6d5188c3c1f43eb725e
35 changes: 31 additions & 4 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var cartesianScatterPoints = {
splom: true
};

function distanceSort(a, b) {
return a.distance - b.distance;
}

// fx.hover: highlight data on hover
// evt can be a mousemove event, or an object with data about what points
// to hover on
Expand Down Expand Up @@ -270,15 +274,21 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
var hovermodeHasX = (hovermode || '').charAt(0) === 'x';
var hovermodeHasY = (hovermode || '').charAt(0) === 'y';

var firstXaxis;
var firstYaxis;

if(hasCartesian && (hovermodeHasX || hovermodeHasY) && hoversubplots === 'axis') {
var subplotsLength = subplots.length;
for(var p = 0; p < subplotsLength; p++) {
spId = subplots[p];
if(plots[spId]) {
// 'cartesian' case

firstXaxis = Axes.getFromId(gd, spId, 'x');
firstYaxis = Axes.getFromId(gd, spId, 'y');

var subplotsWith = (
Axes.getFromId(gd, spId, hovermodeHasX ? 'x' : 'y')
hovermodeHasX ? firstXaxis : firstYaxis
)._subplotsWith;

if(subplotsWith && subplotsWith.length) {
Expand Down Expand Up @@ -661,6 +671,9 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
var thisSpikeDistance;

for(var i = 0; i < pointsData.length; i++) {
if(firstXaxis && firstXaxis._id !== pointsData[i].xa._id) continue;
if(firstYaxis && firstYaxis._id !== pointsData[i].ya._id) continue;

thisSpikeDistance = pointsData[i].spikeDistance;
if(spikeOnWinning && i === 0) thisSpikeDistance = -Infinity;

Expand Down Expand Up @@ -700,9 +713,23 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
gd._spikepoints = newspikepoints;

var sortHoverData = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archmoj Could you add a comment here explaining the new sorting logic?

if(hoversubplots !== 'axis') {
hoverData.sort(function(d1, d2) { return d1.distance - d2.distance; });
}
var hoverDataInSubplot = hoverData.filter(function(a) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming suggestion -- should these functions be called hoverDataInMainSubplot and hoverDataNotInMainSubplot then?

return (
(firstXaxis && firstXaxis._id === a.xa._id) &&
(firstYaxis && firstYaxis._id === a.ya._id)
);
});

var hoverDataOutSubplot = hoverData.filter(function(a) {
return !(
(firstXaxis && firstXaxis._id === a.xa._id) &&
(firstYaxis && firstYaxis._id === a.ya._id)
);
});

hoverDataInSubplot.sort(distanceSort);
hoverDataOutSubplot.sort(distanceSort);
hoverData = hoverDataInSubplot.concat(hoverDataOutSubplot);

// move period positioned points and box/bar-like traces to the end of the list
hoverData = orderRangePoints(hoverData, hovermode);
Expand Down
0