Skip to content

brush facets; target root element #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/marks/brush.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,45 @@ export class Brush extends Mark {
options,
defaults
);
this.faceted = [];
this.clears = [];
}
render(index, scales, {x: X, y: Y}, dimensions) {
const {marginLeft, width, marginRight, marginTop, height, marginBottom} = dimensions;
const left = marginLeft;
const top = marginTop;
const right = width - marginRight;
const bottom = height - marginBottom;
return create("svg:g")
.call((X && Y ? brusher : X ? brusherX : brusherY)()
.extent([[left, top], [right, bottom]])
.on("start brush end", function(event) {
const {selection} = event;
let S = index;
if (selection) {
if (X) {
const [x0, x1] = Y ? [selection[0][0], selection[1][0]] : selection;
S = S.filter(i => x0 <= X[i] && X[i] <= x1);
}
if (Y) {
const [y0, y1] = X ? [selection[0][1], selection[1][1]] : selection;
S = S.filter(i => y0 <= Y[i] && Y[i] <= y1);
}
}
this.selection = S;
this.dispatchEvent(new Event("input", {bubbles: true}));
}))
.property("selection", index)
.node();
const {faceted, clears} = this;
const f = faceted.push(index) - 1;
const brush = (X && Y ? brusher : X ? brusherX : brusherY)()
.extent([[left, top], [right, bottom]])
.on("start brush end", function({selection, sourceEvent, type}) {
if (!sourceEvent) return; // cleared by a brush in a sister facet
let S = index;
if (selection) {
if (X) {
const [x0, x1] = Y ? [selection[0][0], selection[1][0]] : selection;
S = S.filter(i => x0 <= X[i] && X[i] <= x1);
}
if (Y) {
const [y0, y1] = X ? [selection[0][1], selection[1][1]] : selection;
S = S.filter(i => y0 <= Y[i] && Y[i] <= y1);
}
this.selection = S;
} else {
this.selection = faceted.flat();
}
if (type === "start") {
for (let i = 0; i < clears.length; i++) if (i !== f) clears[i]();
}
this.dispatchEvent(new Event("input", {bubbles: true}));
});
const g = create("svg:g")
.call(brush)
.property("selection", index);
clears.push(() => g.call(brush.clear));
return g.node();
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function plot(options = {}) {
// TODO Will the name “selection” lead to a false positive on random SVG elements?
if (node.selection !== undefined) {
value = take(mark.data, node.selection);
node.addEventListener("input", () => figure.value = take(mark.data, node.selection));
node.addEventListener("input", event => figure.value = take(mark.data, event.target.selection));
}
svg.appendChild(node);
}
Expand Down Expand Up @@ -280,7 +280,8 @@ class Facet extends Mark {
const fxMargins = fx && {marginRight: 0, marginLeft: 0, width: fx.bandwidth()};
const subdimensions = {...dimensions, ...fxMargins, ...fyMargins};
const marksValues = marksChannels.map(channels => applyScales(channels, scales));
return create("svg:g")
let selection = [];
const node = create("svg:g")
.call(g => {
if (fy && axes.y) {
const axis1 = axes.y, axis2 = nolabel(axis1);
Expand Down Expand Up @@ -324,10 +325,13 @@ class Facet extends Mark {
const values = marksValues[i];
const index = filter(marksFacetIndex[i], marksChannels[i], values);
const node = marks[i].render(index, scales, values, subdimensions);
if (node.selection !== undefined) selection = new Set([...selection, ...node.selection]);
if (node != null) this.appendChild(node);
}
}))
.node();
if (selection) node.selection = selection;
return node;
}
}

Expand Down