Skip to content

Commit 82b7433

Browse files
Filmbostock
andauthored
pointer should compose with an existing render transform (#1616)
* pointer should compose with an existing render transform closes #1614 * add test * unify render composition * remove tipDotRender test --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 5c2f198 commit 82b7433

File tree

5 files changed

+443
-7
lines changed

5 files changed

+443
-7
lines changed

src/interactions/pointer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {pointer as pointof} from "d3";
2+
import {composeRender} from "../mark.js";
23
import {applyFrameAnchor} from "../style.js";
34

45
const states = new WeakMap();
56

6-
function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, ...options} = {}) {
7+
function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...options} = {}) {
78
maxRadius = +maxRadius;
89
// When px or py is used, register an extra channel that the pointer
910
// interaction can use to control which point is focused; this allows pointing
@@ -16,7 +17,10 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, ...options} =
1617
y,
1718
channels,
1819
...options,
19-
render(index, scales, values, dimensions, context, next) {
20+
// Unlike other composed transforms, the render transform must be the
21+
// outermost render function because it will re-render dynamically in
22+
// response to pointer events.
23+
render: composeRender(function (index, scales, values, dimensions, context, next) {
2024
const svg = context.ownerSVGElement;
2125

2226
// Isolate state per-pointer, per-plot; if the pointer is reused by
@@ -157,7 +161,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, ...options} =
157161
svg.addEventListener("pointerleave", pointerleave);
158162

159163
return render(null);
160-
}
164+
}, render)
161165
};
162166
}
163167

src/mark.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ export class Mark {
8383
}
8484
}
8585
if (render != null) {
86-
if (typeof render !== "function") throw new TypeError(`invalid render transform: ${render}`);
8786
this.render = composeRender(render, this.render);
8887
}
8988
}
@@ -136,9 +135,15 @@ export function marks(...marks) {
136135
return marks;
137136
}
138137

139-
function composeRender(r1, r2) {
140-
return function () {
141-
return r1.call(this, ...arguments, r2.bind(this));
138+
export function composeRender(r1, r2) {
139+
if (r1 == null) return r2 === null ? undefined : r2;
140+
if (r2 == null) return r1 === null ? undefined : r1;
141+
if (typeof r1 !== "function") throw new TypeError(`invalid render transform: ${r1}`);
142+
if (typeof r2 !== "function") throw new TypeError(`invalid render transform: ${r2}`);
143+
return function (i, s, v, d, c, next) {
144+
return r1.call(this, i, s, v, d, c, (i, s, v, d, c) => {
145+
return r2.call(this, i, s, v, d, c, next); // preserve this
146+
});
142147
};
143148
}
144149

0 commit comments

Comments
 (0)