Skip to content

Commit 21a1a93

Browse files
committed
simplify logic and handle the case of not fitting inside the subplot
1 parent c62d520 commit 21a1a93

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/components/fx/hover.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -1089,35 +1089,41 @@ function createHoverText(hoverData, opts, gd) {
10891089

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

10951095
var xOffset = xa._offset;
10961096
var yOffset = ya._offset;
1097-
lyBottom += yOffset + HOVERTEXTPAD;
1098-
lxRight += xOffset + HOVERTEXTPAD;
1099-
lxLeft += xOffset - tWidth - HOVERTEXTPAD;
1100-
lyTop += yOffset - tHeight - HOVERTEXTPAD;
1097+
lyBottom += yOffset;
1098+
lxRight += xOffset;
1099+
lxLeft += xOffset - tWidth;
1100+
lyTop += yOffset - tHeight;
11011101

1102-
var lx, ly;
1102+
var lx, ly; // top and left positions of the hover box
11031103

11041104
// horizontal alignment to end up on screen
1105-
if(lxRight + tWidth + HOVERTEXTPAD <= outerWidth && lxRight - HOVERTEXTPAD >= 0) {
1105+
if(lxRight + tWidth <= outerWidth && lxRight >= 0) {
11061106
lx = lxRight;
1107-
} else if(lxLeft + HOVERTEXTPAD <= outerWidth && lxLeft - HOVERTEXTPAD >= 0) {
1107+
} else if(lxLeft + tWidth <= outerWidth && lxLeft >= 0) {
11081108
lx = lxLeft;
1109+
} else if(xOffset + tWidth <= outerWidth) {
1110+
lx = xOffset; // subplot left corner
11091111
} else {
1110-
lx = xOffset;
1112+
lx = 0; // paper left corner
11111113
}
1114+
lx += HOVERTEXTPAD;
11121115

11131116
// vertical alignement to end up on screen
1114-
if(lyBottom + tHeight + HOVERTEXTPAD <= outerHeight && lyBottom - HOVERTEXTPAD >= 0) {
1117+
if(lyBottom + tHeight <= outerHeight && lyBottom >= 0) {
11151118
ly = lyBottom;
1116-
} else if(lyTop + HOVERTEXTPAD <= outerHeight && lyTop - HOVERTEXTPAD >= 0) {
1119+
} else if(lyTop + tHeight <= outerHeight && lyTop >= 0) {
11171120
ly = lyTop;
1121+
} else if(yOffset + tHeight <= outerHeight) {
1122+
ly = yOffset; // subplot top corner
11181123
} else {
1119-
ly = yOffset;
1124+
ly = 0; // paper top corner
11201125
}
1126+
ly += HOVERTEXTPAD;
11211127

11221128
legendContainer.attr('transform', strTranslate(lx, ly));
11231129
return legendContainer;

0 commit comments

Comments
 (0)