Skip to content

Generalize hover picking routine #575

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 6 commits into from
May 31, 2016
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
58 changes: 38 additions & 20 deletions src/plots/cartesian/graph_interact.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,27 +310,45 @@ function hover(gd, evt, subplot) {

if(!subplot) subplot = 'xy';

// if the user passed in an array of subplots,
// use those instead of finding overlayed plots
var subplots = Array.isArray(subplot) ? subplot : [subplot];

var fullLayout = gd._fullLayout,
plotinfo = fullLayout._plots[subplot],

//If the user passed in an array of subplots, use those instead of finding overlayed plots
subplots = Array.isArray(subplot) ?
subplot :
// list of all overlaid subplots to look at
[subplot].concat(plotinfo.overlays
.map(function(pi) { return pi.id; })),

xaArray = subplots.map(function(spId) {
var ternary = (gd._fullLayout[spId] || {})._ternary;
if(ternary) return ternary.xaxis;
return Axes.getFromId(gd, spId, 'x');
}),
yaArray = subplots.map(function(spId) {
var ternary = (gd._fullLayout[spId] || {})._ternary;
if(ternary) return ternary.yaxis;
return Axes.getFromId(gd, spId, 'y');
}),
hovermode = evt.hovermode || fullLayout.hovermode;
plots = fullLayout._plots || [],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMPORTANT keep fullLayout._plots for cartesian subplots (and gl2d although I'm having second doubt about this) only.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to keep them separate? In my mind, this seems like it will just introduce more surface area for inconsistent state bugs.

Copy link
Contributor Author

@etpinard etpinard May 30, 2016

Choose a reason for hiding this comment

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

Mostly for convenience.

Most of our per-subplot code post-supply-defaults is designed as:

  1. loop over all subplots of a given type found fullLayout
  2. create a subplot object or update an existing subplot object in fullLayout[/* name of subplot */]._subplot

So, it becomes convenient to store a ref to the subplot object in its corresponding fullLayout attribute container.

Moreover, as finding all xy subplots in a given figure is somewhat expensive (see Axes.getSubplots), it becomes advantages to look for them in fullLayout._plots directly instead of calling Axes.getSubplots every time we need to loop over all the x/y subplots.

plotinfo = plots[subplot];

// list of all overlaid subplots to look at
if(plotinfo) {
var overlayedSubplots = plotinfo.overlays.map(function(pi) {
return pi.id;
});

subplots = subplots.concat(overlayedSubplots);
}

var len = subplots.length,
xaArray = new Array(len),
yaArray = new Array(len);

for(var i = 0; i < len; i++) {
var spId = subplots[i];

// 'cartesian' case
var plotObj = plots[spId];
Copy link
Contributor Author

@etpinard etpinard May 27, 2016

Choose a reason for hiding this comment

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

look in:

  • fullLayout._plots[/* subplot id */] then in
  • fullLayout[/* subplot id */]._subplot

if(plotObj) {
xaArray[i] = plotObj.xaxis;
yaArray[i] = plotObj.yaxis;
continue;
}

// other subplot types
var _subplot = fullLayout[spId]._subplot;
xaArray[i] = _subplot.xaxis;
Copy link
Contributor Author

@etpinard etpinard May 27, 2016

Choose a reason for hiding this comment

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

Mocking xaxis and yaxis for hover is pretty easy and also pretty convenient for hover, as the x/y axes are parallel to the pixel coordinate axes.

yaArray[i] = _subplot.yaxis;
}

var hovermode = evt.hovermode || fullLayout.hovermode;

if(['x', 'y', 'closest'].indexOf(hovermode) === -1 || !gd.calcdata ||
gd.querySelector('.zoombox') || gd._dragging) {
Expand Down