8000 Add `layout.subplots` to enable (x|y) hover effects across multiple cartesian and splom suplots sharing one axis by archmoj · Pull Request #6947 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Add layout.subplots to enable (x|y) hover effects across multiple cartesian and splom suplots sharing one axis #6947

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 17 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
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
Next Next commit
hover on subplots when they anchored to same x or y
  • Loading branch information
archmoj committed Apr 3, 2024
commit e2976c89e0d38824331c9d4f00a8f9635a04950f
31 changes: 28 additions & 3 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var isNumeric = require('fast-isnumeric');
var tinycolor = require('tinycolor2');

var Lib = require('../../lib');
var pushUnique = Lib.pushUnique;
var strTranslate = Lib.strTranslate;
var strRotate = Lib.strRotate;
var Events = require('../../lib/events');
Expand Down Expand Up @@ -257,11 +258,37 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
// use those instead of finding overlayed plots
var subplots = Array.isArray(subplot) ? subplot : [subplot];

var spId;

var fullLayout = gd._fullLayout;
var hoversameaxis = fullLayout.hoversameaxis;
var plots = fullLayout._plots || [];
var plotinfo = plots[subplot];
var hasCartesian = fullLayout._has('cartesian');

var hovermode = evt.hovermode || fullLayout.hovermode;
var hovermodeHasX = (hovermode || '').charAt(0) === 'x';
var hovermodeHasY = (hovermode || '').charAt(0) === 'y';

if(hoversameaxis && hasCartesian && (hovermodeHasX || hovermodeHasY)) {
for(var p = 0; p < subplots.length; p++) {
spId = subplots[p];
if(plots[spId]) {
// 'cartesian' case

var subplotsWith = (
Axes.getFromId(gd, spId, hovermodeHasX ? 'x' : 'y')
)._subplotsWith;

if(subplotsWith && subplotsWith.length) {
for(var q = 0; q < subplotsWith.length; q++) {
pushUnique(subplots, subplotsWith[q]);
}
}
}
}
}

// list of all overlaid subplots to look at
if(plotinfo) {
var overlayedSubplots = plotinfo.overlays.map(function(pi) {
Expand All @@ -277,7 +304,7 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
var supportsCompare = false;

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

if(plots[spId]) {
// 'cartesian' case
Expand All @@ -295,8 +322,6 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
}
}

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

if(hovermode && !supportsCompare) hovermode = 'closest';

if(['x', 'y', 'closest', 'x unified', 'y unified'].indexOf(hovermode) === -1 || !gd.calcdata ||
Expand Down
1 change: 1 addition & 0 deletions src/components/fx/hovermode_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = function handleHoverModeDefaults(layoutIn, layoutOut) {
}

coerce('clickmode');
coerce('hoversameaxis');
return coerce('hovermode');
};
9 changes: 9 additions & 0 deletions src/components/fx/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ module.exports = {
'If false, hover interactions are disabled.'
].join(' ')
},
hoversameaxis: {
valType: 'boolean',
dflt: false,
editType: 'none',
description: [
'Determines expansion of hover effects to other subplots in case of sharing an axis.',
'Has an effect only when `hovermode` is set to *x*, *x unified*, *y* or *y unified*.',
].join(' ')
},
hoverdistance: {
valType: 'integer',
min: -1,
Expand Down
6 changes: 6 additions & 0 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,12 @@
"y unified"
]
},
"hoversameaxis": {
"description": "Determines expansion of hover effects to other subplots in case of sharing an axis. Has an effect only when `hovermode` is set to *x*, *x unified*, *y* or *y unified*.",
"dflt": false,
"editType": "none",
"valType": "boolean"
},
"images": {
"items": {
"image": {
Expand Down
0