Skip to content

Commit fe0bd1e

Browse files
authored
Merge pull request #3781 from plotly/hoverlabel-align-centered-label-fix
Fix hoverlabel.align for centered labels
2 parents 172c3e6 + e5b6f0c commit fe0bd1e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Diff for: src/components/fx/hover.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1264,10 +1264,14 @@ function alignHoverText(hoverLabels, rotateLabels) {
12641264
if(textAlign !== 'auto') {
12651265
if(textAlign === 'left' && anchor !== 'start') {
12661266
tx.attr('text-anchor', 'start');
1267-
posX = -d.bx - HOVERTEXTPAD;
1267+
posX = anchor === 'middle' ?
1268+
-d.bx / 2 - d.tx2width / 2 + HOVERTEXTPAD :
1269+
-d.bx - HOVERTEXTPAD;
12681270
} else if(textAlign === 'right' && anchor !== 'end') {
12691271
tx.attr('text-anchor', 'end');
1270-
posX = d.bx + HOVERTEXTPAD;
1272+
posX = anchor === 'middle' ?
1273+
d.bx / 2 - d.tx2width / 2 - HOVERTEXTPAD :
1274+
d.bx + HOVERTEXTPAD;
12711275
}
12721276
}
12731277

Diff for: test/jasmine/tests/hover_label_test.js

+39
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,45 @@ describe('hover info', function() {
20822082
.catch(failTest)
20832083
.then(done);
20842084
});
2085+
2086+
it('should honor *hoverlabel.align (centered label case)', function(done) {
2087+
var gd = createGraphDiv();
2088+
2089+
function _assert(msg, exp) {
2090+
var tx = d3.select('g.hovertext').select('text.nums');
2091+
expect(tx.attr('text-anchor')).toBe(exp.textAnchor, 'text anchor|' + msg);
2092+
expect(Number(tx.attr('x'))).toBeWithin(exp.posX, 3, 'x position|' + msg);
2093+
delete gd._hoverdata;
2094+
}
2095+
2096+
Plotly.plot(gd, [{
2097+
x: ['giraffes'],
2098+
y: [5],
2099+
name: 'LA Zoo',
2100+
type: 'bar',
2101+
text: ['Way tooooo long hover info!'],
2102+
hoverinfo: 'all'
2103+
}], {
2104+
margin: {l: 0, t: 0, b: 0, r: 0},
2105+
hovermode: 'closest',
2106+
width: 400,
2107+
height: 400
2108+
})
2109+
.then(function() { _hoverNatural(gd, 200, 200); })
2110+
.then(function() { _assert('base', {textAnchor: 'middle', posX: -24.3}); })
2111+
.then(function() {
2112+
return Plotly.relayout(gd, 'hoverlabel.align', 'left');
2113+
})
2114+
.then(function() { _hoverNatural(gd, 200, 200); })
2115+
.then(function() { _assert('align:left', {textAnchor: 'start', posX: -105.7}); })
2116+
.then(function() {
2117+
return Plotly.restyle(gd, 'hoverlabel.align', 'right');
2118+
})
2119+
.then(function() { _hoverNatural(gd, 200, 200); })
2120+
.then(function() { _assert('align:right', {textAnchor: 'end', posX: 57}); })
2121+
.catch(failTest)
2122+
.then(done);
2123+
});
20852124
});
20862125

20872126
describe('hover info on stacked subplots', function() {

0 commit comments

Comments
 (0)