Skip to content

Fix hoverlabel.align for centered labels #3781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -1264,10 +1264,14 @@ function alignHoverText(hoverLabels, rotateLabels) {
if(textAlign !== 'auto') {
if(textAlign === 'left' && anchor !== 'start') {
tx.attr('text-anchor', 'start');
posX = -d.bx - HOVERTEXTPAD;
posX = anchor === 'middle' ?
-d.bx / 2 - d.tx2width / 2 + HOVERTEXTPAD :
-d.bx - HOVERTEXTPAD;
} else if(textAlign === 'right' && anchor !== 'end') {
tx.attr('text-anchor', 'end');
posX = d.bx + HOVERTEXTPAD;
posX = anchor === 'middle' ?
d.bx / 2 - d.tx2width / 2 - HOVERTEXTPAD :
d.bx + HOVERTEXTPAD;
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/jasmine/tests/hover_label_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,45 @@ describe('hover info', function() {
.catch(failTest)
.then(done);
});

it('should honor *hoverlabel.align (centered label case)', function(done) {
var gd = createGraphDiv();

function _assert(msg, exp) {
var tx = d3.select('g.hovertext').select('text.nums');
expect(tx.attr('text-anchor')).toBe(exp.textAnchor, 'text anchor|' + msg);
expect(Number(tx.attr('x'))).toBeWithin(exp.posX, 3, 'x position|' + msg);
delete gd._hoverdata;
}

Plotly.plot(gd, [{
x: ['giraffes'],
y: [5],
name: 'LA Zoo',
type: 'bar',
text: ['Way tooooo long hover info!'],
hoverinfo: 'all'
}], {
margin: {l: 0, t: 0, b: 0, r: 0},
hovermode: 'closest',
width: 400,
height: 400
})
.then(function() { _hoverNatural(gd, 200, 200); })
.then(function() { _assert('base', {textAnchor: 'middle', posX: -24.3}); })
.then(function() {
return Plotly.relayout(gd, 'hoverlabel.align', 'left');
})
.then(function() { _hoverNatural(gd, 200, 200); })
.then(function() { _assert('align:left', {textAnchor: 'start', posX: -105.7}); })
.then(function() {
return Plotly.restyle(gd, 'hoverlabel.align', 'right');
})
.then(function() { _hoverNatural(gd, 200, 200); })
.then(function() { _assert('align:right', {textAnchor: 'end', posX: 57}); })
.catch(failTest)
.then(done);
});
});

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