Skip to content
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

simplify splom hover #6950

Merged
merged 2 commits into from
Apr 9, 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
2 changes: 1 addition & 1 deletion draftlogs/6947_add.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Add `layout.hoversubplots` to enable hover effects across multiple cartesian suplots sharing one axis [[#6947](https://github.com/plotly/plotly.js/pull/6947)]
- Add `layout.hoversubplots` to enable hover effects across multiple cartesian suplots sharing one axis [[#6947](https://github.com/plotly/plotly.js/pull/6947), [#6950](https://github.com/plotly/plotly.js/pull/6950)]
84 changes: 29 additions & 55 deletions src/traces/splom/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,47 @@ function hoverPoints(pointData, xval, yval, hovermode, opts) {
var hovermodeHasX = (hovermode || '').charAt(0) === 'x';
var hovermodeHasY = (hovermode || '').charAt(0) === 'y';

var xpx = pointData.xa.c2p(xval);
var ypx = pointData.ya.c2p(yval);
var points = _hoverPoints(pointData, xval, yval);

var points = _hoverPoints(pointData, xpx, ypx);
if((hovermodeHasX || hovermodeHasY) && opts.hoversubplots === 'axis' && points[0]) {
var subplotsWith = (
hovermodeHasX ?
pointData.xa :
pointData.ya
)._subplotsWith;

if((hovermodeHasX || hovermodeHasY) && opts.hoversubplots === 'axis') {
var _xpx = points[0]._xpx;
var _ypx = points[0]._ypx;
var gd = opts.gd;

if(
(hovermodeHasX && _xpx !== undefined) ||
(hovermodeHasY && _ypx !== undefined)
) {
var subplotsWith = (
hovermodeHasX ?
pointData.xa :
pointData.ya
)._subplotsWith;
var _pointData = extendFlat({}, pointData);

var gd = opts.gd;
for(var i = 0; i < subplotsWith.length; i++) {
var spId = subplotsWith[i];

var _pointData = extendFlat({}, pointData);

for(var i = 0; i < subplotsWith.length; i++) {
var spId = subplotsWith[i];

if(hovermodeHasY) {
_pointData.xa = getFromId(gd, spId, 'x');
} else { // hovermodeHasX
_pointData.ya = getFromId(gd, spId, 'y');
}
if(hovermodeHasY) {
_pointData.xa = getFromId(gd, spId, 'x');
} else { // hovermodeHasX
_pointData.ya = getFromId(gd, spId, 'y');
}

var newPoints = _hoverPoints(_pointData, _xpx, _ypx, hovermodeHasX, hovermodeHasY);
var axisHoversubplots = hovermodeHasX || hovermodeHasY;
var newPoints = _hoverPoints(_pointData, xval, yval, axisHoversubplots);

points = points.concat(newPoints);
}
points = points.concat(newPoints);
}
}

return points;
}

function _hoverPoints(pointData, xpx, ypx, hoversubplotsX, hoversubplotsY) {
function _hoverPoints(pointData, xval, yval, axisHoversubplots) {
var cd = pointData.cd;
var trace = cd[0].trace;
var scene = pointData.scene;
var cdata = scene.matrixOptions.cdata;
var xa = pointData.xa;
var ya = pointData.ya;
var xpx = xa.c2p(xval);
var ypx = ya.c2p(yval);
var maxDistance = pointData.distance;

var xi = helpers.getDimIndex(trace, xa);
Expand All @@ -69,36 +61,21 @@ function _hoverPoints(pointData, xpx, ypx, hoversubplotsX, hoversubplotsY) {
var x = cdata[xi];
var y = cdata[yi];

var id, dxy, _xpx, _ypx;
var id, dxy;
var minDist = maxDistance;

for(var i = 0; i < x.length; i++) {
if((hoversubplotsX || hoversubplotsY) && i !== pointData.index) continue;
if(axisHoversubplots && i !== pointData.index) continue;

var ptx = x[i];
var pty = y[i];
var thisXpx = xa.c2p(ptx);
var thisYpx = ya.c2p(pty);

var dx = thisXpx - xpx;
var dy = thisYpx - ypx;
var dist = 0;

var pick = false;
if(hoversubplotsX) {
if(dx === 0) pick = true;
} else if(hoversubplotsY) {
if(dy === 0) pick = true;
} else {
dist = Math.sqrt(dx * dx + dy * dy);
if(dist < minDist) pick = true;
}
var dx = xa.c2p(ptx) - xpx;
var dy = ya.c2p(pty) - ypx;
var dist = Math.sqrt(dx * dx + dy * dy);

if(pick) {
if(axisHoversubplots || dist < minDist) {
minDist = dxy = dist;
id = i;
_xpx = thisXpx;
_ypx = thisYpx;
}
}

Expand All @@ -108,10 +85,7 @@ function _hoverPoints(pointData, xpx, ypx, hoversubplotsX, hoversubplotsY) {

if(id === undefined) return [pointData];

var out = calcHover(pointData, x, y, trace);
out._xpx = _xpx;
out._ypx = _ypx;
return [out];
return [calcHover(pointData, x, y, trace)];
}

module.exports = {
Expand Down