Skip to content

Commit 2eb8b47

Browse files
committed
more documentation
1 parent 567b830 commit 2eb8b47

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

docs/features/interactions.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,21 @@ These values are displayed atop the axes on the edge of the frame; unlike the ti
6060

6161
## Selecting
6262

63-
The [brush transform](../interactions/brush.md) allows the interactive selection of discrete elements, such as dots in a scatterplot, by direct manipulation of the chart. A brush listens to mouse and touch events on the chart, allowing the user to define a rectangular region. All the data points that fall within the region are included in the selection.
63+
The [brush transform](../interactions/brush.md) allows the interactive selection of discrete elements by direct manipulation of the chart.
6464

65-
:::plot defer https://observablehq.com/@observablehq/plot-brush-interaction-dev
65+
:::plot defer https://observablehq.com/@observablehq/brushing-plot--1653
6666
```js
67-
Plot.plot({
68-
marks: [
69-
Plot.dot(penguins, { x: "culmen_length_mm", y: "culmen_depth_mm" }),
70-
Plot.dot(penguins, Plot.brush({ x: "culmen_length_mm", y: "culmen_depth_mm", fill: "species", stroke: "currentColor" }))
71-
]
72-
})
67+
Plot.dot(
68+
penguins,
69+
Plot.brush({
70+
x: "culmen_length_mm",
71+
y: "culmen_depth_mm",
72+
stroke: "currentColor",
73+
fill: "#fff",
74+
unselected: {strokeOpacity: 0.5},
75+
selected: {fill: "species"}
76+
})
77+
).plot()
7378
```
7479
:::
7580

docs/interactions/brush.md

+33-34
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,57 @@ import * as Plot from "@observablehq/plot";
44
import * as d3 from "d3";
55
import {ref, shallowRef, onMounted} from "vue";
66

7-
// const pointered = ref(true);
8-
// const aapl = shallowRef([]);
9-
// const industries = shallowRef([]);
10-
// const olympians = shallowRef([]);
117
const penguins = shallowRef([]);
12-
// const linetip = ref("x");
13-
// const recttip = ref("x");
148

159
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));
1910
d3.csv("../data/penguins.csv", d3.autoType).then((data) => (penguins.value = data));
2011
});
2112

2213
</script>
2314

2415
# Brush transform
2516

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:
17+
The **brush transform** allows the interactive selection of discrete elements, such as dots in a scatterplot, by direct manipulation of the chart. A brush listens to mouse and touch events on the chart, allowing the user to define a rectangular region. All the data points that fall within the region are included in the selection.
2718

2819
:::plot defer
2920
```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-
})
21+
Plot.dot(
22+
penguins,
23+
Plot.brush({
24+
x: "culmen_length_mm",
25+
y: "culmen_depth_mm",
26+
stroke: "currentColor",
27+
fill: "#fff",
28+
unselected: {strokeOpacity: 0.5},
29+
selected: {fill: "species"}
30+
})
31+
).plot()
3632
```
3733
:::
3834

3935
When the chart has a dominant axis, an horizontal or vertical brush is recommended; for example, to select bars in a histogram:
4036

4137
:::plot defer
4238
```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-
})
39+
Plot.rectY(
40+
penguins,
41+
Plot.brushX(
42+
Plot.binX(
43+
{y: "count"},
44+
{
45+
x: "body_mass_g",
46+
thresholds: 40,
47+
unselected: {opacity: 0.1},
48+
}
49+
)
50+
)
51+
).plot()
4952
```
5053
:::
5154

52-
The brush transform is similar to the [pointer](./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.
55+
The brush transform interactively partitions the mark’s index in two: the unselected subset — for points outside the region —, and the selected subset for points inside. As the selection changes, the mark is replaced by two derived marks: below, a mark for the unselected data, with the mark options combined with the **unselected** option; above, a mark for the selected data, with the mark options combined with the **selected** option. All the channel values are incorporated into default scale domains, allowing *e.g.* a color scale to include the fill channel of the selected mark.
5356

54-
The brush transform supports both one- and two-dimensional brushing modes. The two-dimensional mode, [brush](#brush), 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) and [brushY](#brushY), 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.
57+
The brush transform supports both one- and two-dimensional brushing modes. The two-dimensional mode, [brush](#brush), 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) and [brushY](#brushY), 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.
5558

5659
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.
5760

@@ -65,20 +68,16 @@ The following options control the brush transform:
6568
- **y2** - the ending vertical↕︎ target position; bound to the *y* scale
6669
- **x** - the fallback horizontal↔︎ target position; bound to the *x* scale
6770
- **y** - the fallback vertical↕︎ target position; bound to the *y* scale
68-
- **selectionMode** - controls the value exposed to listeners of the *input* events.
71+
- **selected** - additional options for the derived mark representing the selection
72+
- **unselected** - additional options for the derived mark representing non-selected data
6973

7074
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.
7175

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.
76+
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 derived marks are re-rendered.
7377

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:
78+
The selected data exposed as the value of the plot is an array of the (possibly transformed) data rendered by the *selected* derived 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.
7579

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.
80+
The value is decorated with the brush’s coordinates (in data space) as its **x1** and **x2** properties for a quantitative scale *x*, and its **x** property if *x* is ordinal — and likewise for *y*. The value is also decorated with a **done** property set to false while brushing, true when the user releases the pointer, and undefined when the brush is canceled. Additionally, when faceting, it exposes the brushed facet’s *fx* and *fy* properties.
8281

8382
For details on the user interface (including touch events, pointer events and modifier keys), see [d3-brush](https://github.com/d3/d3-brush).
8483

@@ -88,7 +87,7 @@ For details on the user interface (including touch events, pointer events and mo
8887
Plot.dot(penguins, Plot.brush({x: "culmen_length_mm", y: "culmen_depth_mm"}))
8988
```
9089

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.
90+
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.
9291

9392
## brushX(*options*) {#brushX}
9493

0 commit comments

Comments
 (0)