Skip to content

Commit f487390

Browse files
authoredSep 2, 2021
Merge pull request #5916 from plotly/adjust-hover-with-css-transform
Adjust position of hover in respect to CSS transform
2 parents b4f44b1 + 09720a8 commit f487390

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed
 

‎draftlogs/5916_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Adjust position of hover in respect to CSS transform [[#5916](https://github.com/plotly/plotly.js/pull/5916)]

‎src/components/fx/hover.js

+46-12
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ function createHoverText(hoverData, opts) {
891891
var ya = c0.ya;
892892
var axLetter = hovermode.charAt(0);
893893
var t0 = c0[axLetter + 'Label'];
894-
var outerContainerBB = outerContainer.getBoundingClientRect();
894+
var outerContainerBB = getBoundingClientRect(gd, outerContainer);
895895
var outerTop = outerContainerBB.top;
896896
var outerWidth = outerContainerBB.width;
897897
var outerHeight = outerContainerBB.height;
@@ -966,7 +966,7 @@ function createHoverText(hoverData, opts) {
966966

967967
label.attr('transform', '');
968968

969-
var tbb = ltext.node().getBoundingClientRect();
969+
var tbb = getBoundingClientRect(gd, ltext.node());
970970
var lx, ly;
971971

972972
if(hovermode === 'x') {
@@ -1059,7 +1059,7 @@ function createHoverText(hoverData, opts) {
10591059
var dummy = Drawing.tester.append('text')
10601060
.text(s.text())
10611061
.call(Drawing.font, commonLabelFont);
1062-
var dummyBB = dummy.node().getBoundingClientRect();
1062+
var dummyBB = getBoundingClientRect(gd, dummy.node());
10631063
if(Math.round(dummyBB.width) < Math.round(tbb.width)) {
10641064
s.attr('x', ltx - dummyBB.width);
10651065
}
@@ -1148,7 +1148,7 @@ function createHoverText(hoverData, opts) {
11481148

11491149
// Position the hover
11501150
var legendContainer = container.select('g.legend');
1151-
var tbb = legendContainer.node().getBoundingClientRect();
1151+
var tbb = getBoundingClientRect(gd, legendContainer.node());
11521152
var tWidth = tbb.width + 2 * HOVERTEXTPAD;
11531153
var tHeight = tbb.height + 2 * HOVERTEXTPAD;
11541154
var winningPoint = hoverData[0];
@@ -1313,7 +1313,7 @@ function createHoverText(hoverData, opts) {
13131313
.call(svgTextUtils.positionText, 0, 0)
13141314
.call(svgTextUtils.convertToTspans, gd);
13151315

1316-
var t2bb = tx2.node().getBoundingClientRect();
1316+
var t2bb = getBoundingClientRect(gd, tx2.node());
13171317
tx2width = t2bb.width + 2 * HOVERTEXTPAD;
13181318
tx2height = t2bb.height + 2 * HOVERTEXTPAD;
13191319
} else {
@@ -1326,22 +1326,26 @@ function createHoverText(hoverData, opts) {
13261326
stroke: contrastColor
13271327
});
13281328

1329-
var tbb = tx.node().getBoundingClientRect();
13301329
var htx = d.xa._offset + (d.x0 + d.x1) / 2;
13311330
var hty = d.ya._offset + (d.y0 + d.y1) / 2;
13321331
var dx = Math.abs(d.x1 - d.x0);
13331332
var dy = Math.abs(d.y1 - d.y0);
1334-
var txTotalWidth = tbb.width + HOVERARROWSIZE + HOVERTEXTPAD + tx2width;
1335-
var anchorStartOK, anchorEndOK;
13361333

1337-
d.ty0 = outerTop - tbb.top;
1338-
d.bx = tbb.width + 2 * HOVERTEXTPAD;
1339-
d.by = Math.max(tbb.height + 2 * HOVERTEXTPAD, tx2height);
1334+
var tbb = getBoundingClientRect(gd, tx.node());
1335+
var tbbWidth = tbb.width / fullLayout._invScaleX;
1336+
var tbbHeight = tbb.height / fullLayout._invScaleY;
1337+
1338+
d.ty0 = (outerTop - tbb.top) / fullLayout._invScaleY;
1339+
d.bx = tbbWidth + 2 * HOVERTEXTPAD;
1340+
d.by = Math.max(tbbHeight + 2 * HOVERTEXTPAD, tx2height);
13401341
d.anchor = 'start';
1341-
d.txwidth = tbb.width;
1342+
d.txwidth = tbbWidth;
13421343
d.tx2width = tx2width;
13431344
d.offset = 0;
13441345

1346+
var txTotalWidth = (tbbWidth + HOVERARROWSIZE + HOVERTEXTPAD + tx2width) * fullLayout._invScaleX;
1347+
var anchorStartOK, anchorEndOK;
1348+
13451349
if(rotateLabels) {
13461350
d.pos = htx;
13471351
anchorStartOK = hty + dy / 2 + txTotalWidth <= outerHeight;
@@ -2100,3 +2104,33 @@ function getCoord(axLetter, winningPoint, fullLayout) {
21002104
// the offset parent, whatever that may be.
21012105
function getTopOffset(gd) { return gd.offsetTop + gd.clientTop; }
21022106
function getLeftOffset(gd) { return gd.offsetLeft + gd.clientLeft; }
2107+
2108+
function getBoundingClientRect(gd, node) {
2109+
var fullLayout = gd._fullLayout;
2110+
2111+
var rect = node.getBoundingClientRect();
2112+
2113+
var x0 = rect.x;
2114+
var y0 = rect.y;
2115+
var x1 = x0 + rect.width;
2116+
var y1 = y0 + rect.height;
2117+
2118+
var A = Lib.apply3DTransform(fullLayout._invTransform)(x0, y0);
2119+
var B = Lib.apply3DTransform(fullLayout._invTransform)(x1, y1);
2120+
2121+
var Ax = A[0];
2122+
var Ay = A[1];
2123+
var Bx = B[0];
2124+
var By = B[1];
2125+
2126+
return {
2127+
x: Ax,
2128+
y: Ay,
2129+
width: Bx - Ax,
2130+
height: By - Ay,
2131+
top: Math.min(Ay, By),
2132+
left: Math.min(Ax, Bx),
2133+
right: Math.max(Ax, Bx),
2134+
bottom: Math.max(Ay, By),
2135+
};
2136+
}

0 commit comments

Comments
 (0)
Please sign in to comment.