Skip to content

Skip "hoverinfo": "none" trace display for hover modes #5854

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 19 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
1 change: 1 addition & 0 deletions draftlogs/5854_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Skip `"hoverinfo": "none"` trace display for hover modes [[#5854](https://github.com/plotly/plotly.js/pull/5854)]
32 changes: 19 additions & 13 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,12 @@ function _hover(gd, evt, subplot, noHoverEvent) {
hoverdistance: fullLayout.hoverdistance
};

var hoverLabels = createHoverText(hoverData, labelOpts, gd);
var actualHoverData = hoverData.filter(function(d) {
return d.hoverinfo !== 'none';
});
var hoverLabels = actualHoverData.length && createHoverText(actualHoverData, labelOpts, gd);

if(!helpers.isUnifiedHover(hovermode)) {
if(hoverLabels && !helpers.isUnifiedHover(hovermode)) {
hoverAvoidOverlaps(hoverLabels, rotateLabels ? 'xa' : 'ya', fullLayout);
alignHoverText(hoverLabels, rotateLabels, fullLayout._invScaleX, fullLayout._invScaleY);
} // TODO: tagName hack is needed to appease geo.js's hack of using evt.target=true
Expand Down Expand Up @@ -1022,11 +1025,14 @@ function createHoverText(hoverData, opts, gd) {

// Show a single hover label
if(helpers.isUnifiedHover(hovermode)) {
var unifiedHoverData = hoverData.filter(function(d) {
return d.hoverinfo !== 'none';
});
// Delete leftover hover labels from other hovermodes
container.selectAll('g.hovertext').remove();

// Return early if nothing is hovered on
if(hoverData.length === 0) return;
if(unifiedHoverData.length === 0) return;

// mock legend
var mockLayoutIn = {
Expand All @@ -1048,11 +1054,11 @@ function createHoverText(hoverData, opts, gd) {

// prepare items for the legend
mockLegend.entries = [];
for(var j = 0; j < hoverData.length; j++) {
var texts = getHoverLabelText(hoverData[j], true, hovermode, fullLayout, t0);
for(var j = 0; j < unifiedHoverData.length; j++) {
var texts = getHoverLabelText(unifiedHoverData[j], true, hovermode, fullLayout, t0);
var text = texts[0];
var name = texts[1];
var pt = hoverData[j];
var pt = unifiedHoverData[j];
pt.name = name;
if(name !== '') {
pt.text = name + ' : ' + text;
Expand Down Expand Up @@ -1087,7 +1093,7 @@ function createHoverText(hoverData, opts, gd) {
var tbb = legendContainer.node().getBoundingClientRect();
var tWidth = tbb.width + 2 * HOVERTEXTPAD;
var tHeight = tbb.height + 2 * HOVERTEXTPAD;
var winningPoint = hoverData[0];
var winningPoint = unifiedHoverData[0];
var avgX = (winningPoint.x0 + winningPoint.x1) / 2;
var avgY = (winningPoint.y0 + winningPoint.y1) / 2;
// When a scatter (or e.g. heatmap) point wins, it's OK for the hovelabel to occlude the bar and other points.
Expand All @@ -1102,11 +1108,11 @@ function createHoverText(hoverData, opts, gd) {
lyTop = avgY - HOVERTEXTPAD;
lyBottom = avgY + HOVERTEXTPAD;
} else {
lyTop = Math.min.apply(null, hoverData.map(function(c) { return Math.min(c.y0, c.y1); }));
lyBottom = Math.max.apply(null, hoverData.map(function(c) { return Math.max(c.y0, c.y1); }));
lyTop = Math.min.apply(null, unifiedHoverData.map(function(c) { return Math.min(c.y0, c.y1); }));
lyBottom = Math.max.apply(null, unifiedHoverData.map(function(c) { return Math.max(c.y0, c.y1); }));
}
} else {
lyTop = lyBottom = Lib.mean(hoverData.map(function(c) { return (c.y0 + c.y1) / 2; })) - tHeight / 2;
lyTop = lyBottom = Lib.mean(unifiedHoverData.map(function(c) { return (c.y0 + c.y1) / 2; })) - tHeight / 2;
}

var lxRight, lxLeft;
Expand All @@ -1115,11 +1121,11 @@ function createHoverText(hoverData, opts, gd) {
lxRight = avgX + HOVERTEXTPAD;
lxLeft = avgX - HOVERTEXTPAD;
} else {
lxRight = Math.max.apply(null, hoverData.map(function(c) { return Math.max(c.x0, c.x1); }));
lxLeft = Math.min.apply(null, hoverData.map(function(c) { return Math.min(c.x0, c.x1); }));
lxRight = Math.max.apply(null, unifiedHoverData.map(function(c) { return Math.max(c.x0, c.x1); }));
lxLeft = Math.min.apply(null, unifiedHoverData.map(function(c) { return Math.min(c.x0, c.x1); }));
}
} else {
lxRight = lxLeft = Lib.mean(hoverData.map(function(c) { return (c.x0 + c.x1) / 2; })) - tWidth / 2;
lxRight = lxLeft = Lib.mean(unifiedHoverData.map(function(c) { return (c.x0 + c.x1) / 2; })) - tWidth / 2;
}

var xOffset = xa._offset;
Expand Down