Skip to content

Commit 75c1555

Browse files
committed
constrain centered hover label to graph's x span
1 parent 9253732 commit 75c1555

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/components/fx/hover.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,13 +1062,22 @@ function createHoverText(hoverData, opts, gd) {
10621062
d.pos = hty;
10631063
anchorStartOK = htx + dx / 2 + txTotalWidth <= outerWidth;
10641064
anchorEndOK = htx - dx / 2 - txTotalWidth >= 0;
1065+
10651066
if((d.idealAlign === 'left' || !anchorStartOK) && anchorEndOK) {
10661067
htx -= dx / 2;
10671068
d.anchor = 'end';
10681069
} else if(anchorStartOK) {
10691070
htx += dx / 2;
10701071
d.anchor = 'start';
1071-
} else d.anchor = 'middle';
1072+
} else {
1073+
d.anchor = 'middle';
1074+
1075+
var txHalfWidth = txTotalWidth / 2;
1076+
var overflowR = htx + txHalfWidth - outerWidth;
1077+
var overflowL = htx - txHalfWidth;
1078+
if(overflowR > 0) htx -= overflowR;
1079+
if(overflowL < 0) htx += -overflowL;
1080+
}
10721081
}
10731082

10741083
tx.attr('text-anchor', d.anchor);

test/jasmine/tests/pie_test.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,57 @@ describe('pie hovering', function() {
11461146
'hovertemplate arrayOK'
11471147
);
11481148
})
1149-
.catch(fail)
1149+
.catch(failTest)
1150+
.then(done);
1151+
});
1152+
});
1153+
1154+
describe('should fit labels within graph div', function() {
1155+
var gd;
1156+
1157+
beforeEach(function() { gd = createGraphDiv(); });
1158+
1159+
afterEach(destroyGraphDiv);
1160+
1161+
it('- when labels overflow left and right', function(done) {
1162+
Plotly.plot(gd, [{
1163+
hole: 0.33,
1164+
hoverinfo: 'label+text+percent',
1165+
values: ['22238.58', '3145.82', '2865.21', '1664.58'],
1166+
labels: ['label 1 - another long text label', 'label 2', 'label 3 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16', 'label 4'],
1167+
hovertext: ['$22,238.58', '$3,145.82', '$2,865.21', '$1,664.58'],
1168+
type: 'pie'
1169+
}], {
1170+
showlegend: false,
1171+
width: 220,
1172+
height: 220,
1173+
margin: {l: 0, r: 0, t: 0, b: 0}
1174+
})
1175+
.then(function() { mouseEvent('mouseover', 50, 50); })
1176+
.then(function() {
1177+
assertHoverLabelContent({
1178+
nums: 'label 3 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n$2,865.21\n9.58%'
1179+
}, 'long label to the left');
1180+
1181+
var label = d3.select('g.hovertext');
1182+
var bbox = label.node().getBoundingClientRect();
1183+
1184+
expect(bbox.left).toBeWithin(1, 10, 'bbox left bound');
1185+
expect(bbox.right).toBeWithin(275, 10, 'bbox right bound (beyond graph)');
1186+
})
1187+
.then(function() { mouseEvent('mouseover', 150, 150); })
1188+
.then(function() {
1189+
assertHoverLabelContent({
1190+
nums: 'label 1 - another long text label\n$22,238.58\n74.3%'
1191+
}, 'long label to the right');
1192+
1193+
var label = d3.select('g.hovertext');
1194+
var bbox = label.node().getBoundingClientRect();
1195+
1196+
expect(bbox.left).toBeWithin(30, 10, 'bbox left bound');
1197+
expect(bbox.right).toBeWithin(220, 10, 'bbox right bound');
1198+
})
1199+
.catch(failTest)
11501200
.then(done);
11511201
});
11521202
});

0 commit comments

Comments
 (0)