Skip to content

Commit 9716ef0

Browse files
committed
improve unified hover box positioning
1 parent 5a80515 commit 9716ef0

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/components/fx/hover.js

+19-24
Original file line numberDiff line numberDiff line change
@@ -1089,39 +1089,34 @@ function createHoverText(hoverData, opts, gd) {
10891089

10901090
var legendContainer = container.select('g.legend');
10911091
var tbb = legendContainer.node().getBoundingClientRect();
1092-
var txWidth = tbb.width + 2 * HOVERTEXTPAD;
1093-
var txHeight = tbb.height + 2 * HOVERTEXTPAD;
1092+
var tWidth = tbb.width;
1093+
var tHeight = tbb.height;
10941094

10951095
var xOffset = xa._offset;
10961096
var yOffset = ya._offset;
1097-
lxRight += xOffset;
1098-
lxLeft += xOffset;
1099-
lyTop += yOffset;
1100-
lyBottom += yOffset;
1101-
1102-
var lx = lxRight;
1103-
var ly = lyTop;
1104-
1105-
// Change horizontal alignment to end up on screen
1106-
var anchorRightOK = lxRight + txWidth <= outerWidth;
1107-
var anchorLeftOK = lxLeft - txWidth >= 0;
1108-
if(!anchorRightOK && anchorLeftOK) {
1097+
lyBottom += yOffset + HOVERTEXTPAD;
1098+
lxRight += xOffset + HOVERTEXTPAD;
1099+
lxLeft += xOffset - tWidth - HOVERTEXTPAD;
1100+
lyTop += yOffset - tHeight - HOVERTEXTPAD;
1101+
1102+
var lx, ly;
1103+
1104+
// horizontal alignment to end up on screen
1105+
if(lxRight + tWidth + HOVERTEXTPAD <= outerWidth && lxRight - HOVERTEXTPAD >= 0) {
1106+
lx = lxRight;
1107+
} else if(lxLeft + HOVERTEXTPAD <= outerWidth && lxLeft - HOVERTEXTPAD >= 0) {
11091108
lx = lxLeft;
1110-
lx -= txWidth;
11111109
} else {
1112-
lx = lxRight;
1113-
lx += 2 * HOVERTEXTPAD;
1110+
lx = xOffset;
11141111
}
11151112

1116-
// Change vertical alignement to end up on screen
1117-
var anchorBottomOK = lyBottom + txHeight <= outerHeight;
1118-
var anchorTopOK = lyTop - txHeight >= 0;
1119-
if(!anchorTopOK && anchorBottomOK) {
1113+
// vertical alignement to end up on screen
1114+
if(lyBottom + tHeight + HOVERTEXTPAD <= outerHeight && lyBottom - HOVERTEXTPAD >= 0) {
11201115
ly = lyBottom;
1121-
ly += 2 * HOVERTEXTPAD;
1122-
} else {
1116+
} else if(lyTop + HOVERTEXTPAD <= outerHeight && lyTop - HOVERTEXTPAD >= 0) {
11231117
ly = lyTop;
1124-
ly -= txHeight;
1118+
} else {
1119+
ly = yOffset;
11251120
}
11261121

11271122
legendContainer.attr('transform', strTranslate(lx, ly));

0 commit comments

Comments
 (0)