Skip to content

Commit 79ee28d

Browse files
committed
fix: Apply hoverlabel.align to unified hover label items
Unified hover labels draw their items through the legend, which always anchors item text at the start. The resolved hoverlabel.align of each point was never read there, so layout and trace values had no effect. Items with align 'right' now anchor at the right edge of the label. Closes #8100
1 parent c759b27 commit 79ee28d

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎draftlogs/XXXX_fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix `hoverlabel.align` so that it aligns the item text in `x unified` and `y unified` hover labels [[#XXXX](https://github.com/plotly/plotly.js/pull/XXXX)]

‎src/components/legend/draw.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,17 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
10641064
Drawing.setRect(traceToggle, 0, -h / 2, w, h);
10651065
});
10661066

1067+
if (legendObj._inHover) {
1068+
const textRight = legendObj._width - bw2 - itemGap - titleSize[0];
1069+
traces.each(function (d) {
1070+
if (d[0].textAlign !== 'right') return;
1071+
d3.select(this)
1072+
.select('.' + legendId + 'text')
1073+
.attr('text-anchor', 'end')
1074+
.call(svgTextUtils.positionText, textRight);
1075+
});
1076+
}
1077+
10671078
// align legend title horizontally
10681079
var titleEl = scrollBox.select('.' + legendId + 'titletext');
10691080
if(titleEl.node()) {

‎test/jasmine/tests/hover_test.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7395,6 +7395,51 @@ describe('hovermode: (x|y)unified', function () {
73957395
.then(done, done.fail);
73967396
});
73977397

7398+
it('should align item text with hoverlabel.align', (done) => {
7399+
const getItems = () => {
7400+
const items = [];
7401+
getHoverLabel()
7402+
.selectAll('g.traces text.legendtext')
7403+
.each(function () {
7404+
const bBox = this.getBoundingClientRect();
7405+
items.push({ anchor: this.getAttribute('text-anchor'), left: bBox.left, right: bBox.right });
7406+
});
7407+
return items;
7408+
};
7409+
7410+
Plotly.newPlot(gd, [{ y: [1, 2, 3] }, { y: [1000000, 2000000, 3000000] }], {
7411+
hovermode: 'x unified',
7412+
hoverlabel: { align: 'right' },
7413+
showlegend: false,
7414+
width: 500,
7415+
height: 500
7416+
})
7417+
.then(() => {
7418+
_hover(gd, { xval: 1 });
7419+
const [short, long] = getItems();
7420+
expect([short.anchor, long.anchor]).toEqual(['end', 'end']);
7421+
expect(short.right).toBeCloseTo(long.right, 0);
7422+
expect(short.left).toBeGreaterThan(long.left);
7423+
7424+
return Plotly.relayout(gd, 'hoverlabel.align', 'left');
7425+
})
7426+
.then(() => {
7427+
_hover(gd, { xval: 1 });
7428+
const [short, long] = getItems();
7429+
expect([short.anchor, long.anchor]).toEqual(['start', 'start']);
7430+
expect(short.left).toBeCloseTo(long.left, 0);
7431+
7432+
return Plotly.restyle(gd, 'hoverlabel.align', 'right', [0]);
7433+
})
7434+
.then(() => {
7435+
_hover(gd, { xval: 1 });
7436+
const [short, long] = getItems();
7437+
expect([short.anchor, long.anchor]).toEqual(['end', 'start']);
7438+
expect(short.right).toBeCloseTo(long.right, 0);
7439+
})
7440+
.then(done, done.fail);
7441+
});
7442+
73987443
it('should work with hovertemplate', function (done) {
73997444
var mockCopy = Lib.extendDeep({}, mock);
74007445
mockCopy.data[0].hovertemplate = 'hovertemplate: %{y:0.2f}';

0 commit comments

Comments
 (0)