Skip to content

Commit 87d0497

Browse files
committed
Default hovermode to closest if clickmode is select [1852]
1 parent a5a90f9 commit 87d0497

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

src/components/fx/layout_attributes.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ module.exports = {
5959
role: 'info',
6060
values: ['x', 'y', 'closest', false],
6161
editType: 'modebar',
62-
description: 'Determines the mode of hover interactions.'
62+
description: [
63+
'Determines the mode of hover interactions.',
64+
'If `clickmode` includes the *select* flag,',
65+
'`hovermode` defaults to *closest*.',
66+
'If `clickmode` lacks the *select* flag,',
67+
'it defaults to *x* or *y* (depending on the trace\'s',
68+
'`orientation` value) for plots based on',
69+
'cartesian coordinates. For anything else the default',
70+
'value is *closest*.',
71+
].join(' ')
6372
},
6473
hoverdistance: {
6574
valType: 'integer',

src/components/fx/layout_defaults.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
2323

2424
var hovermodeDflt;
2525
if(layoutOut._has('cartesian')) {
26-
// flag for 'horizontal' plots:
27-
// determines the state of the mode bar 'compare' hovermode button
28-
layoutOut._isHoriz = isHoriz(fullData);
29-
hovermodeDflt = layoutOut._isHoriz ? 'y' : 'x';
26+
if(layoutOut.clickmode.indexOf('select') > -1) {
27+
hovermodeDflt = 'closest';
28+
} else {
29+
// flag for 'horizontal' plots:
30+
// determines the state of the mode bar 'compare' hovermode button
31+
layoutOut._isHoriz = isHoriz(fullData);
32+
hovermodeDflt = layoutOut._isHoriz ? 'y' : 'x';
33+
}
3034
}
3135
else hovermodeDflt = 'closest';
3236

test/jasmine/tests/hover_label_test.js

+49
Original file line numberDiff line numberDiff line change
@@ -2369,3 +2369,52 @@ describe('hover distance', function() {
23692369
});
23702370
});
23712371
});
2372+
2373+
describe('hovermode defaults to', function() {
2374+
var gd;
2375+
2376+
beforeEach(function() {
2377+
gd = createGraphDiv();
2378+
});
2379+
2380+
afterEach(destroyGraphDiv);
2381+
2382+
it('\'closest\' for cartesian plots if clickmode includes \'select\'', function(done) {
2383+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [4, 5, 6] }], { clickmode: 'event+select' })
2384+
.then(function() {
2385+
expect(gd._fullLayout.hovermode).toBe('closest');
2386+
})
2387+
.catch(failTest)
2388+
.then(done);
2389+
});
2390+
2391+
it('\'x\' for horizontal cartesian plots if clickmode lacks \'select\'', function(done) {
2392+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [4, 5, 6], type: 'bar', orientation: 'h' }], { clickmode: 'event' })
2393+
.then(function() {
2394+
expect(gd._fullLayout.hovermode).toBe('y');
2395+
})
2396+
.catch(failTest)
2397+
.then(done);
2398+
});
2399+
2400+
it('\'y\' for vertical cartesian plots if clickmode lacks \'select\'', function(done) {
2401+
Plotly.plot(gd, [{ x: [1, 2, 3], y: [4, 5, 6], type: 'bar', orientation: 'v' }], { clickmode: 'event' })
2402+
.then(function() {
2403+
expect(gd._fullLayout.hovermode).toBe('x');
2404+
})
2405+
.catch(failTest)
2406+
.then(done);
2407+
});
2408+
2409+
it('\'closest\' for a non-cartesian plot', function(done) {
2410+
var mock = require('@mocks/polar_scatter.json');
2411+
expect(mock.layout.hovermode).toBeUndefined();
2412+
2413+
Plotly.plot(gd, mock.data, mock.layout)
2414+
.then(function() {
2415+
expect(gd._fullLayout.hovermode).toBe('closest');
2416+
})
2417+
.catch(failTest)
2418+
.then(done);
2419+
});
2420+
});

0 commit comments

Comments
 (0)