Skip to content

Commit f25a940

Browse files
committed
Merge branch 'fil/brush-x' into fil/brush-merge
2 parents e468497 + 9c81d85 commit f25a940

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"};
@@ -319,12 +319,25 @@ function renderFilter(initialTest, channelDefaults = {}) {
319319
const {x: X, y: Y, x1: X1, x2: X2, y1: Y1, y2: Y2} = values;
320320
const MX = X ?? (X1 && X2 ? Float64Array.from(X1, (v, i) => (v + X2[i]) / 2) : undefined);
321321
const MY = Y ?? (Y1 && Y2 ? Float64Array.from(Y1, (v, i) => (v + Y2[i]) / 2) : undefined);
322-
const filter = (test) =>
323-
typeof test === "function" ? index.filter((i) => test(MX?.[i], MY?.[i])) : test ? index : [];
324-
let g = next(filter(initialTest), scales, values, dimensions, context);
322+
const render = (test) => {
323+
if (typeof test !== "function") test = constant(test);
324+
let run = [];
325+
const runs = [run];
326+
for (const i of index) {
327+
if (test(MX?.[i], MY?.[i])) run.push(i);
328+
else if (run.length) runs.push((run = []));
329+
}
330+
const g = next(runs[0], scales, values, dimensions, context);
331+
for (const run of runs.slice(1)) {
332+
const h = next(run, scales, values, dimensions, context);
333+
while (h.firstChild) g.appendChild(h.firstChild);
334+
}
335+
return g;
336+
};
337+
let g = render(initialTest);
325338
updatePerFacet.push((test) => {
326339
const transform = g.getAttribute("transform");
327-
g.replaceWith((g = next(filter(test), scales, values, dimensions, context)));
340+
g.replaceWith((g = render(test)));
328341
if (transform) g.setAttribute("transform", transform);
329342
});
330343
return g;

0 commit comments

Comments
 (0)