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 all 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)], with thanks to @Domino987 for the contribution!
29 changes: 17 additions & 12 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ function createHoverText(hoverData, opts, gd) {
var container = opts.container;
var outerContainer = opts.outerContainer;
var commonLabelOpts = opts.commonLabelOpts || {};
// Early exit if no labels are drawn
if(hoverData.length === 0) return [[]];

// opts.fontFamily/Size are used for the common label
// and as defaults for each hover label, though the individual labels
Expand Down Expand Up @@ -1024,9 +1026,9 @@ function createHoverText(hoverData, opts, gd) {
if(helpers.isUnifiedHover(hovermode)) {
// Delete leftover hover labels from other hovermodes
container.selectAll('g.hovertext').remove();

var groupedHoverData = hoverData.filter(function(data) {return data.hoverinfo !== 'none';});
// Return early if nothing is hovered on
if(hoverData.length === 0) return;
if(groupedHoverData.length === 0) return;

// mock legend
var mockLayoutIn = {
Expand All @@ -1048,11 +1050,14 @@ 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 < groupedHoverData.length; j++) {
var pt = groupedHoverData[j];
if(pt.hoverinfo === 'none') continue;

var texts = getHoverLabelText(pt, true, hovermode, fullLayout, t0);
var text = texts[0];
var name = texts[1];
var pt = hoverData[j];

pt.name = name;
if(name !== '') {
pt.text = name + ' : ' + text;
Expand Down Expand Up @@ -1087,7 +1092,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 = groupedHoverData[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 +1107,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, groupedHoverData.map(function(c) { return Math.min(c.y0, c.y1); }));
lyBottom = Math.max.apply(null, groupedHoverData.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(groupedHoverData.map(function(c) { return (c.y0 + c.y1) / 2; })) - tHeight / 2;
}

var lxRight, lxLeft;
Expand All @@ -1115,11 +1120,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, groupedHoverData.map(function(c) { return Math.max(c.x0, c.x1); }));
lxLeft = Math.min.apply(null, groupedHoverData.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(groupedHoverData.map(function(c) { return (c.x0 + c.x1) / 2; })) - tWidth / 2;
}

var xOffset = xa._offset;
Expand Down
32 changes: 32 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4601,6 +4601,38 @@ describe('hovermode: (x|y)unified', function() {
.then(done, done.fail);
});

it('should not display hover for display: none', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This test fails on CircleCI. Does it pass on your machine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it does not and I have problems with the tests in the first place on my machine, since its a windows.
But yes, that should work.

Copy link
Contributor

Choose a reason for hiding this comment

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

The test is not passing.
Please simply replace it with

it('should not display hover for display: none', function(done) {
        Plotly.newPlot(gd, {
            data: [{
                name: 'A',
                y: [1]
            }, {
                name: 'B',
                y: [2],
                hoverinfo: 'none'
            }],
            layout: {
                hovermode: 'x unified',
                showlegend: false,
                width: 500,
                height: 500,
                margin: {
                    t: 50,
                    b: 50,
                    l: 50,
                    r: 50
                }
            }
        })
        .then(function() {
            _hover(gd, { xpx: 200, ypx: 200 });
            assertLabel({title: '0', items: [
                'A : 1'
            ]});
        })
        .then(done, done.fail);
    });

Copy link
Contributor

Choose a reason for hiding this comment

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

@Domino987 Could you please adjust the test as mentioned above? It looks like the only remaining item here.

Plotly.newPlot(gd, {
data: [{
name: 'A',
y: [1]
}, {
name: 'B',
y: [2],
hoverinfo: 'none'
}],
layout: {
hovermode: 'x unified',
showlegend: false,
width: 500,
height: 500,
margin: {
t: 50,
b: 50,
l: 50,
r: 50
}
}
})
.then(function() {
_hover(gd, { xpx: 200, ypx: 200 });
assertLabel({title: '0', items: [
'A : 1'
]});
})
.then(done, done.fail);
});

it('y unified should work for x/y cartesian traces', function(done) {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.layout.hovermode = 'y unified';
Expand Down