Skip to content

Commit 9799196

Browse files
committed
fix area pointer position
1 parent 068c8ef commit 9799196

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/interactions/pointer.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, ...options} =
5454
if (!facetState) facetStates.set(mark, (facetState = new Map()));
5555
}
5656

57-
// The order of precedence when determining the point position is: px &
58-
// py; the middle of x1 & y1 and x2 & y2; or lastly x & y. If any
59-
// dimension is unspecified, we fallback to the frame anchor.
60-
const {x: X, y: Y, x1: X1, y1: Y1, x2: X2, y2: Y2, px: PX, py: PY} = values;
61-
const [cx, cy] = applyFrameAnchor(this, dimensions);
62-
const px = PX ? (i) => PX[i] : X2 ? (i) => (X1[i] + X2[i]) / 2 : X ? (i) => X[i] : () => cx;
63-
const py = PY ? (i) => PY[i] : Y2 ? (i) => (Y1[i] + Y2[i]) / 2 : Y ? (i) => Y[i] : () => cy;
57+
const [px, py] = pointerPosition(this, values, dimensions);
6458

6559
let i; // currently focused index
6660
let g; // currently rendered mark
@@ -173,3 +167,15 @@ export function pointerX(options) {
173167
export function pointerY(options) {
174168
return pointerK(0.01, 1, options);
175169
}
170+
171+
// The order of precedence when determining the pointer position is: px & py;
172+
// the middle of x1 & y1 and x2 & y2; or x1 & y1 (e.g., area); or lastly x & y.
173+
// If any dimension is unspecified, we fallback to the frame anchor.
174+
export function pointerPosition(mark, values, dimensions) {
175+
const {px: PX, py: PY, x1: X1, y1: Y1, x2: X2, y2: Y2, x: X = X1, y: Y = Y1} = values;
176+
const [cx, cy] = applyFrameAnchor(mark, dimensions);
177+
return [
178+
PX ? (i) => PX[i] : X1 && X2 ? (i) => (X1[i] + X2[i]) / 2 : X ? (i) => X[i] : () => cx,
179+
PY ? (i) => PY[i] : Y1 && Y2 ? (i) => (Y1[i] + Y2[i]) / 2 : Y ? (i) => Y[i] : () => cy
180+
];
181+
}

src/marks/tip.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import {getSource} from "../channel.js";
33
import {create} from "../context.js";
44
import {defined} from "../defined.js";
55
import {formatDefault} from "../format.js";
6+
import {pointerPosition} from "../interactions/pointer.js";
67
import {Mark} from "../mark.js";
78
import {maybeAnchor, maybeFrameAnchor, maybeTuple, number, string} from "../options.js";
8-
import {applyDirectStyles, applyFrameAnchor, applyIndirectStyles, applyTransform, impliedString} from "../style.js";
9+
import {applyDirectStyles, applyIndirectStyles, applyTransform, impliedString} from "../style.js";
910
import {inferTickFormat} from "./axis.js";
1011
import {applyIndirectTextStyles, defaultWidth, ellipsis, monospaceWidth} from "./text.js";
1112
import {cut, clipper, splitter, maybeTextOverflow} from "./text.js";
@@ -92,12 +93,10 @@ export class Tip extends Mark {
9293
// unspecified, we fallback to the frame anchor. We also need to know the
9394
// facet offsets to detect when the tip would draw outside the plot, and
9495
// thus we need to change the orientation.
95-
const {x: X, y: Y, x1: X1, y1: Y1, x2: X2, y2: Y2} = channels;
96-
const [cx, cy] = applyFrameAnchor(this, dimensions);
96+
const {x1: X1, y1: Y1, x2: X2, y2: Y2, x: X = X1 ?? X2, y: Y = Y1 ?? Y2} = channels;
9797
const ox = fx ? fx(index.fx) - marginLeft : 0;
9898
const oy = fy ? fy(index.fy) - marginTop : 0;
99-
const px = X2 ? (i) => (X1[i] + X2[i]) / 2 : X ? (i) => X[i] : () => cx;
100-
const py = Y2 ? (i) => (Y1[i] + Y2[i]) / 2 : Y ? (i) => Y[i] : () => cy;
99+
const [px, py] = pointerPosition(mark, channels, dimensions);
101100

102101
// Resolve the text metric implementation. We may need an ellipsis for text
103102
// truncation, so we optimistically compute the ellipsis width.
@@ -142,7 +141,7 @@ export class Tip extends Mark {
142141
const g = create("svg:g", context)
143142
.call(applyIndirectStyles, this, dimensions, context)
144143
.call(applyIndirectTextStyles, this)
145-
.call(applyTransform, this, {x: (X || X2) && x, y: (Y || Y2) && y})
144+
.call(applyTransform, this, {x: X && x, y: Y && y})
146145
.call((g) =>
147146
g
148147
.selectAll()

0 commit comments

Comments
 (0)