Skip to content

Commit c4a5d17

Browse files
authored
pointer[XY] for crosshair[XY] (#1599)
* pointer[XY] for crosshair[XY] * propagate maxRadius
1 parent ac9aa58 commit c4a5d17

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

src/marks/crosshair.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface CrosshairOptions extends MarkOptions {
2020
export function crosshair(data?: Data, options?: CrosshairOptions): CompoundMark;
2121

2222
/** TODO */
23-
export function crosshairX(data?: Data, options?: Omit<CrosshairOptions, "y">): CompoundMark;
23+
export function crosshairX(data?: Data, options?: CrosshairOptions): CompoundMark;
2424

2525
/** TODO */
26-
export function crosshairY(data?: Data, options?: Omit<CrosshairOptions, "x">): CompoundMark;
26+
export function crosshairY(data?: Data, options?: CrosshairOptions): CompoundMark;

src/marks/crosshair.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ import {initializer} from "../transforms/basic.js";
55
import {ruleX, ruleY} from "./rule.js";
66
import {text} from "./text.js";
77

8-
export function crosshair(data, options = {}) {
9-
const p = pointer({px: options.x, py: options.y});
10-
return marks(pruleX(data, p, options), pruleY(data, p, options), ptextX(data, p, options), ptextY(data, p, options));
8+
export function crosshair(data, options) {
9+
return crosshairK(pointer, data, options);
1110
}
1211

1312
export function crosshairX(data, options = {}) {
14-
const p = pointerX({px: options.x});
15-
return marks(pruleX(data, p, options), ptextX(data, p, options));
13+
return crosshairK(pointerX, data, options);
1614
}
1715

1816
export function crosshairY(data, options = {}) {
19-
const p = pointerY({py: options.y});
20-
return marks(pruleY(data, p, options), ptextY(data, p, options));
17+
return crosshairK(pointerY, data, options);
18+
}
19+
20+
function crosshairK(pointer, data, options = {}) {
21+
const {x, y, maxRadius} = options;
22+
const p = pointer({px: x, py: y, maxRadius});
23+
return marks(
24+
x == null ? null : ruleX(data, ruleOptions("x", p, options)),
25+
y == null ? null : ruleY(data, ruleOptions("y", p, options)),
26+
x == null ? null : text(data, textOptions("x", {...p, dy: 9, frameAnchor: "bottom", lineAnchor: "top"}, options)),
27+
y == null ? null : text(data, textOptions("y", {...p, dx: -9, frameAnchor: "left", textAnchor: "end"}, options))
28+
);
2129
}
2230

2331
function markOptions(
@@ -55,14 +63,6 @@ function pxpy(k, i) {
5563
};
5664
}
5765

58-
function pruleX(data, pointerOptions, options) {
59-
return ruleX(data, ruleOptions("x", pointerOptions, options));
60-
}
61-
62-
function pruleY(data, pointerOptions, options) {
63-
return ruleY(data, ruleOptions("y", pointerOptions, options));
64-
}
65-
6666
function ruleOptions(k, pointerOptions, options) {
6767
const {
6868
color = "currentColor",
@@ -73,14 +73,6 @@ function ruleOptions(k, pointerOptions, options) {
7373
return {...markOptions(k, pointerOptions, options), stroke, strokeOpacity, strokeWidth};
7474
}
7575

76-
function ptextX(data, pointerOptions, options) {
77-
return text(data, textOptions("x", {...pointerOptions, dy: 9, frameAnchor: "bottom", lineAnchor: "top"}, options));
78-
}
79-
80-
function ptextY(data, pointerOptions, options) {
81-
return text(data, textOptions("y", {...pointerOptions, dx: -9, frameAnchor: "left", textAnchor: "end"}, options));
82-
}
83-
8476
function textOptions(k, pointerOptions, options) {
8577
const {
8678
color = "currentColor",

0 commit comments

Comments
 (0)