Skip to content

Fix closest hover and click+select with zorder #6953

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 4 commits into from
Apr 10, 2024
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
3 changes: 2 additions & 1 deletion draftlogs/6918_add.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Add a new attribute `zorder` to SVG based Cartesian traces (not to WebGL traces). Traces with higher `zorder` values are drawn in front of traces with lower `zorder` values. This feature was anonymously sponsored: thank you to our sponsor!
- Add zorder attribute to control stacking order of SVG traces drawn into cartesian subplots [[#6918](https://github.com/plotly/plotly.js/pull/6918), [#6953](https://github.com/plotly/plotly.js/pull/6953)],
This feature was anonymously sponsored: thank you to our sponsor!
12 changes: 10 additions & 2 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,16 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
}
}
} else {
for(curvenum = 0; curvenum < gd.calcdata.length; curvenum++) {
cd = gd.calcdata[curvenum];
// take into account zorder
var zorderedCalcdata = gd.calcdata.slice();
zorderedCalcdata.sort(function(a, b) {
var aZorder = a[0].trace.zorder || 0;
var bZorder = b[0].trace.zorder || 0;
return aZorder - bZorder;
});

for(curvenum = 0; curvenum < zorderedCalcdata.length; curvenum++) {
cd = zorderedCalcdata[curvenum];
trace = cd[0].trace;
if(trace.hoverinfo !== 'skip' && helpers.isTraceInSubplots(trace, subplots)) {
searchData.push(cd);
Expand Down
46 changes: 46 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4780,6 +4780,52 @@ describe('hover distance', function() {
});
});

describe('hover working with zorder', function() {
'use strict';

var mock = {
data: [{
zorder: 100,
marker: {size: 50},
text: ['A', 'B'],
y: [0, 1]
}, {
marker: {size: 50},
text: ['C', 'D'],
y: [2, 1]
}],
layout: {
width: 400,
height: 400,
showlegend: false,
hovermode: 'closest'
}
};

afterEach(destroyGraphDiv);

beforeEach(function(done) {
Plotly.newPlot(createGraphDiv(), mock).then(done);
});

it('pick the trace on top', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', {xval: 1}, 'xy');

expect(gd._hoverdata.length).toEqual(1);

assertHoverLabelContent({
nums: '(1, 1)\nB',
name: 'trace 0'
});

var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.text).toEqual('B');
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(1);
});
});

describe('hover label rotation:', function() {
var gd;

Expand Down
2 changes: 1 addition & 1 deletion test/jasmine/tests/select_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('Click-to-select', function() {
return _immediateClickPt({ x: 130, y: 250 });
})
.then(function() {
assertSelectedPoints([], [1], []);
assertSelectedPoints([0], [], []);
})
.then(done, done.fail);
});
Expand Down