Skip to content

Commit 6f0487c

Browse files
committed
document
1 parent 8e9a835 commit 6f0487c

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

docs/interactions/brush.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,107 @@
1+
<script setup>
2+
3+
import * as Plot from "@observablehq/plot";
4+
import * as d3 from "d3";
5+
import {ref, shallowRef, onMounted} from "vue";
6+
7+
// const pointered = ref(true);
8+
// const aapl = shallowRef([]);
9+
// const industries = shallowRef([]);
10+
// const olympians = shallowRef([]);
11+
const penguins = shallowRef([]);
12+
// const linetip = ref("x");
13+
// const recttip = ref("x");
14+
15+
onMounted(() => {
16+
// d3.csv("../data/aapl.csv", d3.autoType).then((data) => (aapl.value = data));
17+
// d3.csv("../data/athletes.csv", d3.autoType).then((data) => (olympians.value = data));
18+
// d3.csv("../data/bls-industry-unemployment.csv", d3.autoType).then((data) => (industries.value = data));
19+
d3.csv("../data/penguins.csv", d3.autoType).then((data) => (penguins.value = data));
20+
});
21+
22+
</script>
23+
124
# Brush transform
225

26+
The **brush transform** filters a mark interactively such that only the data that fall within the rectangular region defined by the user are rendered. It is typically used to select discrete elements, such as dots in a scatterplot:
27+
28+
:::plot defer
29+
```js
30+
Plot.plot({
31+
marks: [
32+
Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm", fill: "currentColor"}),
33+
Plot.dot(penguins, Plot.brush({x: "culmen_length_mm", y: "culmen_depth_mm", fill: "species", stroke: "currentColor", r: 4}))
34+
]
35+
})
36+
```
37+
:::
38+
39+
When the chart has a dominant axis, an horizontal or vertical brush is recommended; for example, to select bars in a histogram:
40+
41+
:::plot defer
42+
```js
43+
Plot.plot({
44+
marks: [
45+
Plot.rectY(penguins, Plot.binX({y: "count"}, {x: "body_mass_g", thresholds: 40, fillOpacity: 0.2})),
46+
Plot.rectY(penguins, Plot.brushX(Plot.binX({y: "count"}, {fill:"currentColor", x: "body_mass_g", thresholds: 40}))),
47+
]
48+
})
49+
```
50+
:::
51+
52+
The brush transform is similar to the [pointer](../interaction/pointer.md) transform: it interactively filters the mark’s index to show a subset of the data, and re-renders the mark as the selection changes. Since the mark is lazily rendered during interaction, it is fast: only the visible elements are rendered as needed. And, like the filter and select transforms, unfiltered channel values are incorporated into default scale domains.
53+
54+
The brush transform supports both one- and two-dimensional brushing modes. The two-dimensional mode, [brush](#brush-options-1), is used above and is suitable for scatterplots and the general case: it allows the user to define a rectangular region by clicking on a corner (_e.g._ the top-left corner) and dragging the pointer to the bottom-right corner. The one-dimensional modes, [brushX](#brushx-options) and [brushY](#brushy-options), in contrast only consider one dimension; this is desirable when a chart has a “dominant” dimension, such as time in a time-series chart, the binned quantitative dimension in a histogram, or the categorical dimension of a bar chart.
55+
56+
The brush transform emits an [*input* event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event) whenever the selection changes, and sets the value of the plot element to the selected data. This allows you to use a plot as an [Observable view](https://observablehq.com/@observablehq/views) (viewof), or to register an *input* event listener to react to brushing.
357

458
## Brush options
559

60+
The following options control the brush transform:
61+
62+
- **x1** - the starting horizontal↔︎ target position; bound to the *x* scale
63+
- **y1** - the starting vertical↕︎ target position; bound to the *y* scale
64+
- **x2** - the ending horizontal↔︎ target position; bound to the *x* scale
65+
- **y2** - the ending vertical↕︎ target position; bound to the *y* scale
66+
- **x** - the fallback horizontal↔︎ target position; bound to the *x* scale
67+
- **y** - the fallback vertical↕︎ target position; bound to the *y* scale
68+
- **selectionMode** - controls the value exposed to listeners of the *input* events.
69+
70+
The positional options define a sensitive surface for each data point, defined on the horizontal axis as the extent between *x1* and *x2* if specified, between *x* and *x + bandwidth* if *x* is a band scale, or the value *x* otherwise. The sensitive surface’s vertical extent likewise spans from *y1* to *y2* if specified, from *y* to *y + bandwidth* if *y* is a band scale, or is equal to the *y* value otherwise.
71+
72+
When the user interacts with the plot by clicking and dragging the brush to define a rectangular region, all the elements whose sensitive surface intersect with the brushed region are selected, and the mark is re-rendered.
73+
74+
The brush’s selection mode determines the contents of the plot’s value property when the user manipulates the brush. It supports the following options:
75+
76+
* **data** - default; the selected data
77+
* **extent** - the selection extent, in data space
78+
79+
The selected data is an array of the possibly transformed data rendered by the mark. For example, in the case of the histogram above, the selected data is an array of bins, each containing the penguins whose body mass is between the bin’s lower and upper bounds.
80+
81+
The selection extent is an object with properties *x*: [x1, x2] for brushX, *y*: [y1, y2] for brushY, and both *x* and *y* for brush. Additionally, when faceting, it contains the facet’s *fx* and *fy* properties.
82+
83+
For details on the user interface (including touch events, pointer events and modifier keys), see [d3-brush](https://github.com/d3/d3-brush).
684

785
## brush(*options*)
886

87+
```js
88+
Plot.dot(penguins, Plot.brush({x: "culmen_length_mm", y: "culmen_depth_mm"}))
89+
```
90+
91+
Applies the brush render transform to the specified *options* to filter the mark index such that the points whose sensitive surface intersect with the brushed region the point closest to the pointer is rendered; the mark will re-render interactively in response to brush events.
992

1093
## brushX(*options*)
1194

95+
```js
96+
Plot.tip(aapl, Plot.pointerX({x: "Date", y: "Close"}))
97+
```
98+
99+
Like [brush](#brush-options-1), except the determination of the intersection exclusively considers the *x* (horizontal↔︎) position; this should be used for plots where *x* is the dominant dimension, such as the binned quantitative dimension in a histogram, or the categorical dimension of a bar chart.
12100

13101
## brushY(*options*)
14102

103+
```js
104+
Plot.tip(alphabet, Plot.pointerY({x: "frequency", y: "letter"}))
105+
```
106+
107+
Like [brush](#brush-options-1), except the determination of the intersection exclusively considers the *y* (vertical↕) position; this should be used for plots where *y* is the dominant dimension.

src/interactions/brush.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ type BrushOptions = {
88
* * **data** - default; the selected data
99
* * **extent** - the selection extent, in data space
1010
*
11-
* The extent is an object with properties *x*: [x1, x2], *y*: [y1, y2] for
12-
* brushY, and both *x* and *y* for brush. Additionally, the *fx* and *fy*
13-
* properties are also set when faceting.
11+
* The extent is an object with properties *x*: [x1, x2] for brushX, *y*: [y1,
12+
* y2] for brushY, and both *x* and *y* for brush. Additionally, when
13+
* faceting, it contains the facet’s *fx* and *fy* properties.
1414
*/
1515
selectionMode?: "data" | "extent";
1616
};

0 commit comments

Comments
 (0)