This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
d3-brush-test.ts
185 lines (136 loc) · 6.67 KB
/
d3-brush-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Typescript definition tests for d3/d3-brush module
*
* Note: These tests are intended to test the definitions only
* in the sense of typing and call signature consistency. They
* are not intended as functional tests.
*/
import * as d3Brush from '../../src/d3-brush';
import { ArrayLike, event, select, Selection } from '../../src/d3-selection';
import { Transition } from '../../src/d3-transition';
// -----------------------------------------------------------------------------
// Preparatory Steps
// -----------------------------------------------------------------------------
interface BrushDatum {
extent: [[number, number], [number, number]];
filterZoomEvent: boolean;
}
// -----------------------------------------------------------------------------
// Test Define BrushBehavior
// -----------------------------------------------------------------------------
let brush: d3Brush.BrushBehavior<BrushDatum> = d3Brush.brush<BrushDatum>();
let brushX: d3Brush.BrushBehavior<BrushDatum> = d3Brush.brushX<BrushDatum>();
let brushY: d3Brush.BrushBehavior<BrushDatum> = d3Brush.brushY<BrushDatum>();
// extent() ----------------------------------------------------------------------
let extent: (this: SVGGElement, d: BrushDatum, i: number, group: Array<SVGGElement> | ArrayLike<SVGGElement>) => [[number, number], [number, number]];
extent = brush.extent();
// chainable with array
brush = brush.extent([[0, 0], [300, 200]]);
// chainable with function
brush = brush.extent(function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
return d.extent; // datum of type BrushDatum
});
// filter() ----------------------------------------------------------------
// chainable
brush = brush.filter(function (d, i, group) {
// Cast d3 event to D3ZoomEvent to be used in filter logic
let e = <d3Brush.D3BrushEvent<BrushDatum>>event;
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
return e.sourceEvent.type !== 'zoom' || !d.filterZoomEvent; // datum type is BrushDatum (as propagated to SVGGElement with brush event attached)
});
let filterFn: (this: SVGGElement, d?: BrushDatum, index?: number, group?: Array<SVGGElement>) => boolean;
filterFn = brush.filter();
// handleSize() ----------------------------------------------------------------
// chainable
brush = brush.handleSize(7);
let handleSize: number = brush.handleSize();
// on() ------------------------------------------------------------------------
let brushed: (this: SVGGElement, datum: BrushDatum, index: number, group: Array<SVGGElement> | ArrayLike<SVGGElement>) => void;
let wrongHandler1: (this: SVGSVGElement, datum: BrushDatum, index: number, group: Array<SVGSVGElement> | ArrayLike<SVGSVGElement>) => void;
let wrongHandler2: (this: SVGGElement, datum: { test: string }, index: number, group: Array<SVGGElement> | ArrayLike<SVGGElement>) => void;
// chainable
brush = brush.on('end', brushed);
// brush = brush.on('end', wrongHandler1); // fails, wrongHandler event handler has wrong this
// brush = brush.on('end', wrongHandler2); // fails, wrongHandler event handler has wrong datum type
brushed = brush.on('end');
// chainable remove handler
brush = brush.on('end', null);
// re-apply
brush.on('end', function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
console.log('Extent as per data: ', d.extent); // datum of type BrushDatum
// do anything
});
// -----------------------------------------------------------------------------
// Test Attach Brush Behavior
// -----------------------------------------------------------------------------
let g = select<SVGSVGElement, any>('svg')
.append<SVGGElement>('g')
.classed('brush', true)
.datum<BrushDatum>({
extent: [[0, 0], [300, 200]],
filterZoomEvent: true
});
g.call(brush);
let gX = select<SVGSVGElement, any>('svg')
.append<SVGGElement>('g')
.classed('brush', true)
.datum<BrushDatum>({
extent: [[0, 0], [300, 200]],
filterZoomEvent: true
});
gX.call(brushX);
// -----------------------------------------------------------------------------
// Test Use Brush Behavior
// -----------------------------------------------------------------------------
let gTransition = g.transition();
// 2d brush move with Selection
brush.move(g, [[10, 10], [50, 50]]); // two-dimensional brush
brush.move(g, function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
let selection: [[number, number], [number, number]];
selection[0][0] = d.extent[0][0] + 10; // datum type is brushDatum
selection[0][1] = d.extent[0][1] + 10;
selection[1][0] = d.extent[0][0] + 40;
selection[1][1] = d.extent[0][1] + 40;
return selection;
});
// 2d brush move with Transition
brush.move(gTransition, [[10, 10], [50, 50]]); // two-dimensional brush
brush.move(gTransition, function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
let selection: [[number, number], [number, number]];
selection[0][0] = d.extent[0][0] + 10; // datum type is brushDatum
selection[0][1] = d.extent[0][1] + 10;
selection[1][0] = d.extent[0][0] + 40;
selection[1][1] = d.extent[0][1] + 40;
return selection;
});
let gXTransition = gX.transition();
// 1d brush move with Selection
brush.move(gX, [10, 40]); // two-dimensional brush
brush.move(gX, function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
let selection: [number, number];
selection[0] = d.extent[0][0] + 10; // datum type is brushDatum
selection[1] = d.extent[0][0] + 40;
return selection;
});
// 1d brush move with Transition
brush.move(gXTransition, [10, 40]); // two-dimensional brush
brush.move(gXTransition, function (d, i, group) {
console.log('Owner SVG Element of svg group: ', this.ownerSVGElement); // this is of type SVGGElement
let selection: [number, number];
selection[0] = d.extent[0][0] + 10; // datum type is brushDatum
selection[1] = d.extent[0][0] + 40;
return selection;
});
// -----------------------------------------------------------------------------
// Test Brush Event Interface
// -----------------------------------------------------------------------------
let e: d3Brush.D3BrushEvent<BrushDatum>;
let target: d3Brush.BrushBehavior<BrushDatum> = e.target;
let type: 'start' | 'brush' | 'end' | string = e.type;
let brushSelection: d3Brush.BrushSelection = e.selection;
let sourceEvent: any = e.sourceEvent;