Skip to content

Commit 3f42bd4

Browse files
committed
rotate hover labels under a few more circumstances
- when hovermode:'y' and one trace with multiple hover labels - when hovermode:'closest' and at leat one trace is 'horizontal' on the subplot. - lock behavior in violin suite.
1 parent 8c3580f commit 3f42bd4

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/components/fx/hover.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ 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)
242+
var hasOneHorizontalTrace = false;
243+
240244
// Figure out what we're hovering on:
241245
// mouse location or user-supplied data
242246

@@ -256,6 +260,9 @@ function _hover(gd, evt, subplot, noHoverEvent) {
256260
trace = cd[0].trace;
257261
if(trace.hoverinfo !== 'skip' && helpers.isTraceInSubplots(trace, subplots)) {
258262
searchData.push(cd);
263+
if(trace.orientation === 'h') {
264+
hasOneHorizontalTrace = true;
265+
}
259266
}
260267
}
261268

@@ -577,9 +584,10 @@ function _hover(gd, evt, subplot, noHoverEvent) {
577584

578585
gd._hoverdata = newhoverdata;
579586

580-
// if there's more than one horz bar trace,
581-
// rotate the labels so they don't overlap
582-
var rotateLabels = hovermode === 'y' && searchData.length > 1;
587+
var rotateLabels = (
588+
(hovermode === 'y' && (searchData.length > 1 || hoverData.length > 1)) ||
589+
(hovermode === 'closest' && hasOneHorizontalTrace)
590+
);
583591

584592
var bgColor = Color.combine(
585593
fullLayout.plot_bgcolor || Color.background,

test/jasmine/assets/custom_assertions.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ function count(selector) {
9090
* - nums {string || array of strings}
9191
* - name {string || array of strings}
9292
* - axis {string}
93+
* - vOrder {array of number}
94+
* - hOrder {array of number}
9395
* @param {string} msg
9496
*/
9597
exports.assertHoverLabelContent = function(expectation, msg) {

test/jasmine/tests/violin_test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,48 @@ describe('Test violin hover:', function() {
431431
nums: 'x: 42.43046, kde: 0.083',
432432
name: '',
433433
axis: 'Saturday'
434+
}, {
435+
desc: 'single horizontal violin',
436+
mock: require('@mocks/violin_non-linear.json'),
437+
pos: [310, 160],
438+
nums: ['median: C', 'min: A', 'q1: B', 'q3: D', 'max: G', 'upper fence: D', 'x: C, kde: 1.005'],
439+
name: ['categories', '', '', '', '', '', ''],
440+
axis: 'categories',
441+
hOrder: [4, 5, 3, 6, 0, 2, 1],
442+
}, {
443+
desc: 'multiple horizontal violins',
444+
mock: require('@mocks/box_grouped_horz.json'),
445+
patch: function(fig) {
446+
fig.data.forEach(function(t) {
447+
t.type = 'violin';
448+
t.hoveron = 'violins';
449+
});
450+
fig.layout.violinmode = 'group';
451+
return fig;
452+
},
453+
nums: ['median: 0.4', 'min: 0.1', 'q1: 0.2', 'q3: 0.7', 'max: 0.9'],
454+
name: ['kale', '', '', '', ''],
455+
axis: 'day 2',
456+
hOrder: [4, 3, 0, 2, 1],
457+
}, {
458+
desc: 'multiple horizontal violins (under hovermode:closest)',
459+
mock: require('@mocks/box_grouped_horz.json'),
460+
patch: function(fig) {
461+
fig.data.forEach(function(t) {
462+
t.type = 'violin';
463+
t.hoveron = 'violins';
464+
});
465+
fig.layout.violinmode = 'group';
466+
fig.layout.hovermode = 'closest';
467+
return fig;
468+
},
469+
pos: [200, 175],
470+
nums: [
471+
'(median: 0.7, day 2)', '(min: 0.2, day 2)', '(q1: 0.5, day 2)',
472+
'(q3: 0.8, day 2)', '(max: 0.9, day 2)'
473+
],
474+
name: ['radishes', '', '', '', ''],
475+
hOrder: [4, 3, 0, 2, 1],
434476
}]
435477
.forEach(function(specs) {
436478
it('should generate correct hover labels ' + specs.desc, function(done) {

0 commit comments

Comments
 (0)