Skip to content

Commit 04746ca

Browse files
committed
Merge branch 'fil/brush-x' into fil/brush-data-options
2 parents 6ef6573 + 9c81d85 commit 04746ca

3 files changed

Lines changed: 126 additions & 5 deletions

File tree

src/interactions/brush.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ascending
1010
} from "d3";
1111
import {composeRender, Mark} from "../mark.js";
12-
import {dataify, identity, isIterable, keyword, maybeInterval, maybeTuple, take} from "../options.js";
12+
import {constant, dataify, identity, isIterable, keyword, maybeInterval, maybeTuple, take} from "../options.js";
1313
import {applyAttr} from "../style.js";
1414

1515
const defaults = {ariaLabel: "brush", fill: "#777", fillOpacity: 0.3, stroke: "#fff"};
@@ -317,12 +317,25 @@ function renderFilter(initialTest, channelDefaults = {}) {
317317
const {x: X, y: Y, x1: X1, x2: X2, y1: Y1, y2: Y2} = values;
318318
const MX = X ?? (X1 && X2 ? Float64Array.from(X1, (v, i) => (v + X2[i]) / 2) : undefined);
319319
const MY = Y ?? (Y1 && Y2 ? Float64Array.from(Y1, (v, i) => (v + Y2[i]) / 2) : undefined);
320-
const filter = (test) =>
321-
typeof test === "function" ? index.filter((i) => test(MX?.[i], MY?.[i])) : test ? index : [];
322-
let g = next(filter(initialTest), scales, values, dimensions, context);
320+
const render = (test) => {
321+
if (typeof test !== "function") test = constant(test);
322+
let run = [];
323+
const runs = [run];
324+
for (const i of index) {
325+
if (test(MX?.[i], MY?.[i])) run.push(i);
326+
else if (run.length) runs.push((run = []));
327+
}
328+
const g = next(runs[0], scales, values, dimensions, context);
329+
for (const run of runs.slice(1)) {
330+
const h = next(run, scales, values, dimensions, context);
331+
while (h.firstChild) g.appendChild(h.firstChild);
332+
}
333+
return g;
334+
};
335+
let g = render(initialTest);
323336
updatePerFacet.push((test) => {
324337
const transform = g.getAttribute("transform");
325-
g.replaceWith((g = next(filter(test), scales, values, dimensions, context)));
338+
g.replaceWith((g = render(test)));
326339
if (transform) g.setAttribute("transform", transform);
327340
});
328341
return g;

0 commit comments

Comments
 (0)