@@ -4,6 +4,7 @@ import {create} from "d3-selection";
4
4
import { filter } from "../defined.js" ;
5
5
import { Mark , identity , first , second } from "../mark.js" ;
6
6
import { Style } from "../style.js" ;
7
+ const { max, min} = Math ;
7
8
8
9
export class Brush extends Mark {
9
10
constructor (
@@ -36,12 +37,14 @@ export class Brush extends Mark {
36
37
) {
37
38
let svg ;
38
39
const g = create ( "svg:g" ) ;
40
+ const data = this . data ;
39
41
42
+ const bounds = [
43
+ [ Math . floor ( marginLeft ) , Math . floor ( marginTop ) ] ,
44
+ [ Math . ceil ( width - marginRight ) , Math . ceil ( height - marginBottom ) ]
45
+ ] ;
40
46
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 )
45
48
. on ( "start brush end" , ( { type, selection, sourceEvent} ) => {
46
49
let index = filter ( I , X , Y ) ;
47
50
if ( selection ) {
@@ -56,7 +59,7 @@ export class Brush extends Mark {
56
59
index = index . filter ( i => Y [ i ] >= y0 && Y [ i ] < y1 ) ;
57
60
}
58
61
}
59
- const dots = selection ? Array . from ( index , i => J [ i ] ) : this . data ;
62
+ const dots = selection ? Array . from ( index , i => J [ i ] ) : data ;
60
63
61
64
if ( svg ) {
62
65
svg . value = dots ;
@@ -80,19 +83,28 @@ export class Brush extends Mark {
80
83
svg = g . node ( ) . ownerSVGElement ;
81
84
if ( ! svg . __brushes ) svg . __brushes = [ ] ;
82
85
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 ) ;
93
107
}
94
- } else {
95
- g . call ( brush . clear ) ;
96
108
}
97
109
} , 1 ) ;
98
110
0 commit comments