Skip to content

Commit 1c58c43

Browse files
committed
fix initial setup of the brush
1 parent a8c46fe commit 1c58c43

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/marks/brush.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {create} from "d3-selection";
44
import {filter} from "../defined.js";
55
import {Mark, identity, first, second} from "../mark.js";
66
import {Style} from "../style.js";
7+
const {max, min} = Math;
78

89
export class Brush extends Mark {
910
constructor(
@@ -36,12 +37,14 @@ export class Brush extends Mark {
3637
) {
3738
let svg;
3839
const g = create("svg:g");
40+
const data = this.data;
3941

42+
const bounds = [
43+
[Math.floor(marginLeft), Math.floor(marginTop)],
44+
[Math.ceil(width - marginRight), Math.ceil(height - marginBottom)]
45+
];
4046
const brush = (x && y ? brusher : x ? brusherX : brusherY)()
41-
.extent([
42-
[Math.floor(marginLeft), Math.floor(marginTop)],
43-
[Math.ceil(width - marginRight), Math.ceil(height - marginBottom)]
44-
])
47+
.extent(bounds)
4548
.on("start brush end", ({type, selection, sourceEvent}) => {
4649
let index = filter(I, X, Y);
4750
if (selection) {
@@ -56,7 +59,7 @@ export class Brush extends Mark {
5659
index = index.filter(i => Y[i] >= y0 && Y[i] < y1);
5760
}
5861
}
59-
const dots = selection ? Array.from(index, i => J[i]) : this.data;
62+
const dots = selection ? Array.from(index, i => J[i]) : data;
6063

6164
if (svg) {
6265
svg.value = dots;
@@ -80,19 +83,28 @@ export class Brush extends Mark {
8083
svg = g.node().ownerSVGElement;
8184
if (!svg.__brushes) svg.__brushes = [];
8285
svg.__brushes.push({b: brush, g});
83-
if (this.initialSelection) {
84-
const s = this.initialSelection;
85-
if (x && y) {
86-
const [x0, x1] = extent([x(s[0][0]), x(s[1][0])]);
87-
const [y0, y1] = extent([y(s[0][1]), y(s[1][1])]);
88-
g.call(brush.move, [[x0, y0], [x1, y1]]);
89-
} else if (x) {
90-
g.call(brush.move, extent(s.map(x)));
91-
} else if (y) {
92-
g.call(brush.move, extent(s.map(y)));
86+
87+
// initial setup works only on one facet
88+
if (svg.__brushes.length === 1) {
89+
if (this.initialSelection) {
90+
const s = this.initialSelection;
91+
if (x && y) {
92+
const [x0, x1] = extent([x(s[0][0]), x(s[1][0])]);
93+
const [y0, y1] = extent([y(s[0][1]), y(s[1][1])]);
94+
g.call(brush.move, [
95+
[ max(x0, bounds[0][0]), max(y0, bounds[0][1]) ],
96+
[ min(x1, bounds[1][0]), min(y1, bounds[1][1]) ]
97+
]);
98+
} else if (x) {
99+
const [x0, x1] = extent(s.map(x));
100+
g.call(brush.move, [ max(x0, bounds[0][0]), min(x1, bounds[1][0]) ]);
101+
} else if (y) {
102+
const [y0, y1] = extent(s.map(y));
103+
g.call(brush.move, [ max(y0, bounds[0][1]), min(y1, bounds[1][1]) ]);
104+
}
105+
} else {
106+
g.call(brush.clear);
93107
}
94-
} else {
95-
g.call(brush.clear);
96108
}
97109
}, 1);
98110

0 commit comments

Comments
 (0)