Skip to content

Commit 7ffcdd7

Browse files
authored
Merge pull request #6954 from mbant/issue-6917
fix: do not ovelap hoverlabels for different tracetypes
2 parents 1c647fc + 0631d43 commit 7ffcdd7

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

draftlogs/6954_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- fix for excessive hoverlabel removal and overlap for plots with both `scatter` and `bar` traces which reported in [#6917](https://github.com/plotly/plotly.js/issues/6917).

src/components/fx/hover.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1824,8 +1824,7 @@ function hoverAvoidOverlaps(hoverLabels, rotateLabels, fullLayout, commonLabelBo
18241824
var p1 = g1[0];
18251825
topOverlap = p0.pos + p0.dp + p0.size - p1.pos - p1.dp + p1.size;
18261826

1827-
// Only group points that lie on the same axes
1828-
if(topOverlap > 0.01 && (p0.pmin === p1.pmin) && (p0.pmax === p1.pmax)) {
1827+
if(topOverlap > 0.01) {
18291828
// push the new point(s) added to this group out of the way
18301829
for(j = g1.length - 1; j >= 0; j--) g1[j].dp += topOverlap;
18311830

test/jasmine/tests/hover_label_test.js

+57
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,10 @@ describe('hover info', function() {
16681668
return Math.max(0, overlap);
16691669
}
16701670

1671+
function labelCount() {
1672+
return d3Select(gd).selectAll('g.hovertext').size();
1673+
}
1674+
16711675
it('centered-aligned, should render labels inside boxes', function(done) {
16721676
var trace1 = {
16731677
x: ['giraffes'],
@@ -1787,6 +1791,59 @@ describe('hover info', function() {
17871791
})
17881792
.then(done, done.fail);
17891793
});
1794+
1795+
it('does not overlap lebels for different trace types', function(done) {
1796+
function trace(name, type, delta) {
1797+
return {
1798+
name: name,
1799+
type: type,
1800+
y: [0 + delta, 1 + delta, 2 + delta],
1801+
x: ['CAT 1', 'CAT 2', 'CAT 3'],
1802+
};
1803+
}
1804+
1805+
var scatterName = 'scatter_';
1806+
var barName = 'bar_';
1807+
var data = [];
1808+
var i;
1809+
for(i = 0; i < 3; i++) {
1810+
data.push(trace(barName + i, 'bar', 0.0));
1811+
data.push(trace(scatterName + i, 'scatter', 0.1));
1812+
}
1813+
var layout = {
1814+
width: 600,
1815+
height: 400,
1816+
hovermode: 'x',
1817+
};
1818+
1819+
Plotly.newPlot(gd, data, layout)
1820+
.then(function() {
1821+
_hoverNatural(gd, 200, 200);
1822+
})
1823+
.then(function() {
1824+
expect(labelCount()).toBe(6);
1825+
})
1826+
.then(function() {
1827+
var nodes = [];
1828+
for(i = 0; i < 3; i++) {
1829+
nodes.push(hoverInfoNodes(barName + i).secondaryBox.getBoundingClientRect());
1830+
nodes.push(hoverInfoNodes(scatterName + i).secondaryBox.getBoundingClientRect());
1831+
}
1832+
nodes.sort(function(a, b) { return a.top - b.top; });
1833+
1834+
for(i = 0; i < 5; i++) {
1835+
expect(
1836+
calcLineOverlap(
1837+
nodes[i].top,
1838+
nodes[i].bottom,
1839+
nodes[i + 1].top,
1840+
nodes[i + 1].bottom
1841+
)
1842+
).toBeWithin(2, 1);
1843+
}
1844+
})
1845+
.then(done, done.fail);
1846+
});
17901847
});
17911848

17921849
describe('constraints info graph viewport', function() {

0 commit comments

Comments
 (0)