Skip to content

Commit d9f3db7

Browse files
committed
do not rotate single labels in 'closest'
... and if the corresponding trace is 'horizontal'
1 parent 579bd8d commit d9f3db7

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed

src/components/fx/hover.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ function _hover(gd, evt, subplot, noHoverEvent) {
237237
vLinePoint: null
238238
};
239239

240-
// does subplot have one (or more) horizontal traces,
241-
// (to determine whether we rotate the labels or not)
240+
// does subplot have one (or more) horizontal traces?
241+
// This is used to determine whether we rotate the labels or not
242242
var hasOneHorizontalTrace = false;
243243

244244
// Figure out what we're hovering on:
@@ -590,7 +590,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
590590

591591
var rotateLabels = (
592592
(hovermode === 'y' && (searchData.length > 1 || hoverData.length > 1)) ||
593-
(hovermode === 'closest' && hasOneHorizontalTrace)
593+
(hovermode === 'closest' && hasOneHorizontalTrace && hoverData.length > 1)
594594
);
595595

596596
var bgColor = Color.combine(

test/jasmine/tests/hover_label_test.js

+107
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,113 @@ describe('hover distance', function() {
24702470
});
24712471
});
24722472

2473+
describe('hover label rotation:', function() {
2474+
var gd;
2475+
2476+
function _hover(gd, opts) {
2477+
Fx.hover(gd, opts);
2478+
Lib.clearThrottle();
2479+
}
2480+
2481+
describe('when a single pt is picked', function() {
2482+
afterAll(destroyGraphDiv);
2483+
2484+
beforeAll(function(done) {
2485+
gd = createGraphDiv();
2486+
2487+
Plotly.plot(gd, [{
2488+
type: 'bar',
2489+
orientation: 'h',
2490+
y: [0, 1, 2],
2491+
x: [1, 2, 1]
2492+
}, {
2493+
type: 'bar',
2494+
orientation: 'h',
2495+
y: [3, 4, 5],
2496+
x: [1, 2, 1]
2497+
}], {
2498+
hovermode: 'y'
2499+
})
2500+
.catch(failTest)
2501+
.then(done);
2502+
});
2503+
2504+
it('should rotate labels under *hovermode:y*', function() {
2505+
_hover(gd, { xval: 2, yval: 1 });
2506+
assertHoverLabelContent({
2507+
nums: '2',
2508+
name: 'trace 0',
2509+
axis: '1',
2510+
// N.B. could be changed to be made consistent with 'closest'
2511+
isRotated: true
2512+
});
2513+
});
2514+
2515+
it('should not rotate labels under *hovermode:closest*', function(done) {
2516+
Plotly.relayout(gd, 'hovermode', 'closest').then(function() {
2517+
_hover(gd, { xval: 1.9, yval: 1 });
2518+
assertHoverLabelContent({
2519+
nums: '(2, 1)',
2520+
name: 'trace 0',
2521+
axis: '',
2522+
isRotated: false
2523+
});
2524+
})
2525+
.catch(failTest)
2526+
.then(done);
2527+
});
2528+
});
2529+
2530+
describe('when mulitple pts are picked', function() {
2531+
afterAll(destroyGraphDiv);
2532+
2533+
beforeAll(function(done) {
2534+
gd = createGraphDiv();
2535+
2536+
Plotly.plot(gd, [{
2537+
type: 'bar',
2538+
orientation: 'h',
2539+
y: [0, 1, 2],
2540+
x: [1, 2, 1]
2541+
}, {
2542+
type: 'bar',
2543+
orientation: 'h',
2544+
y: [0, 1, 2],
2545+
x: [1, 2, 1]
2546+
}], {
2547+
hovermode: 'y'
2548+
})
2549+
.catch(failTest)
2550+
.then(done);
2551+
});
2552+
2553+
it('should rotate labels under *hovermode:y*', function() {
2554+
_hover(gd, { xval: 2, yval: 1 });
2555+
assertHoverLabelContent({
2556+
nums: ['2', '2'],
2557+
name: ['trace 0', 'trace 1'],
2558+
axis: '1',
2559+
isRotated: true
2560+
});
2561+
});
2562+
2563+
it('should not rotate labels under *hovermode:closest*', function(done) {
2564+
Plotly.relayout(gd, 'hovermode', 'closest').then(function() {
2565+
_hover(gd, { xval: 1.9, yval: 1 });
2566+
assertHoverLabelContent({
2567+
nums: '(2, 1)',
2568+
// N.B. only showing the 'top' trace
2569+
name: 'trace 1',
2570+
axis: '',
2571+
isRotated: false
2572+
});
2573+
})
2574+
.catch(failTest)
2575+
.then(done);
2576+
});
2577+
});
2578+
});
2579+
24732580
describe('hovermode defaults to', function() {
24742581
var gd;
24752582

test/jasmine/tests/violin_test.js

+13
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ describe('Test violin hover:', function() {
476476
name: ['radishes', '', '', '', ''],
477477
hOrder: [4, 3, 0, 2, 1],
478478
isRotated: true
479+
}, {
480+
desc: 'hovering over single pt on horizontal violin should not rotate labels',
481+
mock: require('@mocks/violin_old-faithful.json'),
482+
patch: function(fig) {
483+
fig.data[0].x = fig.data[0].y;
484+
delete fig.data[0].y;
485+
fig.layout = {hovermode: 'closest'};
486+
return fig;
487+
},
488+
pos: [539, 293],
489+
nums: '(96, Old Faithful)',
490+
name: '',
491+
isRotated: false
479492
}]
480493
.forEach(function(specs) {
481494
it('should generate correct hover labels ' + specs.desc, function(done) {

0 commit comments

Comments
 (0)