Skip to content

Commit e2976c8

Browse files
committed
hover on subplots when they anchored to same x or y
1 parent d20b8e2 commit e2976c8

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/components/fx/hover.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var isNumeric = require('fast-isnumeric');
55
var tinycolor = require('tinycolor2');
66

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

261+
var spId;
262+
260263
var fullLayout = gd._fullLayout;
264+
var hoversameaxis = fullLayout.hoversameaxis;
261265
var plots = fullLayout._plots || [];
262266
var plotinfo = plots[subplot];
263267
var hasCartesian = fullLayout._has('cartesian');
264268

269+
var hovermode = evt.hovermode || fullLayout.hovermode;
270+
var hovermodeHasX = (hovermode || '').charAt(0) === 'x';
271+
var hovermodeHasY = (hovermode || '').charAt(0) === 'y';
272+
273+
if(hoversameaxis && hasCartesian && (hovermodeHasX || hovermodeHasY)) {
274+
for(var p = 0; p < subplots.length; p++) {
275+
spId = subplots[p];
276+
if(plots[spId]) {
277+
// 'cartesian' case
278+
279+
var subplotsWith = (
280+
Axes.getFromId(gd, spId, hovermodeHasX ? 'x' : 'y')
281+
)._subplotsWith;
282+
283+
if(subplotsWith && subplotsWith.length) {
284+
for(var q = 0; q < subplotsWith.length; q++) {
285+
pushUnique(subplots, subplotsWith[q]);
286+
}
287+
}
288+
}
289+
}
290+
}
291+
265292
// list of all overlaid subplots to look at
266293
if(plotinfo) {
267294
var overlayedSubplots = plotinfo.overlays.map(function(pi) {
@@ -277,7 +304,7 @@ function _hover(gd, evt, subplot, noHoverEvent, eventTarget) {
277304
var supportsCompare = false;
278305

279306
for(var i = 0; i < len; i++) {
280-
var spId = subplots[i];
307+
spId = subplots[i];
281308

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

298-
var hovermode = evt.hovermode || fullLayout.hovermode;
299-
300325
if(hovermode && !supportsCompare) hovermode = 'closest';
301326

302327
if(['x', 'y', 'closest', 'x unified', 'y unified'].indexOf(hovermode) === -1 || !gd.calcdata ||

src/components/fx/hovermode_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ module.exports = function handleHoverModeDefaults(layoutIn, layoutOut) {
1212
}
1313

1414
coerce('clickmode');
15+
coerce('hoversameaxis');
1516
return coerce('hovermode');
1617
};

src/components/fx/layout_attributes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ module.exports = {
7878
'If false, hover interactions are disabled.'
7979
].join(' ')
8080
},
81+
hoversameaxis: {
82+
valType: 'boolean',
83+
dflt: false,
84+
editType: 'none',
85+
description: [
86+
'Determines expansion of hover effects to other subplots in case of sharing an axis.',
87+
'Has an effect only when `hovermode` is set to *x*, *x unified*, *y* or *y unified*.',
88+
].join(' ')
89+
},
8190
hoverdistance: {
8291
valType: 'integer',
8392
min: -1,

test/plot-schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,12 @@
26342634
"y unified"
26352635
]
26362636
},
2637+
"hoversameaxis": {
2638+
"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*.",
2639+
"dflt": false,
2640+
"editType": "none",
2641+
"valType": "boolean"
2642+
},
26372643
"images": {
26382644
"items": {
26392645
"image": {

0 commit comments

Comments
 (0)