Skip to content

Commit ce185e5

Browse files
committed
document crosshair
1 parent 06bac48 commit ce185e5

File tree

1 file changed

+89
-4
lines changed

1 file changed

+89
-4
lines changed

docs/interactions/crosshair.md

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,106 @@
1+
<script setup>
2+
3+
import * as Plot from "@observablehq/plot";
4+
import * as d3 from "d3";
5+
import alphabet from "../data/alphabet.ts";
6+
import aapl from "../data/aapl.ts";
7+
import penguins from "../data/penguins.ts";
8+
9+
</script>
10+
111
# Crosshair mark
212

3-
The **crosshair mark**
13+
The **crosshair mark** shows the *x* (horizontal↔︎ position) and *y* (vertical↕︎ position) value of the point closest to the [pointer](./pointer.md) on the bottom and left sides of the frame, respectively.
14+
15+
:::plot defer
16+
```js
17+
Plot.plot({
18+
marks: [
19+
Plot.dot(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm", stroke: "sex"}),
20+
Plot.crosshair(penguins, {x: "culmen_length_mm", y: "culmen_depth_mm"})
21+
]
22+
})
23+
```
24+
:::
25+
26+
If either **x** or **y** is not specified, the crosshair is be one-dimensional.
27+
28+
:::plot defer
29+
```js
30+
Plot.plot({
31+
marks: [
32+
Plot.tickX(penguins, {x: "body_mass_g"}),
33+
Plot.crosshair(penguins, {x: "body_mass_g"})
34+
]
35+
})
36+
```
37+
:::
38+
39+
For charts which have an independent variable, typically a time series with dates on the *x* axis, the crosshairX mark is recommended to track the horizontal position of the pointer.
40+
41+
:::plot defer
42+
```js
43+
Plot.plot({
44+
marks: [
45+
Plot.lineY(aapl, {x: "Date", y: "Close"}),
46+
Plot.crosshairX(aapl, {x: "Date", y: "Close"})
47+
]
48+
})
49+
```
50+
:::
51+
52+
Conversely, for charts where the independent variable is along the *y* axis, the crosshairY mark is recommended to track the vertical position of the pointer.
53+
54+
:::plot defer
55+
```js
56+
Plot.plot({
57+
marks: [
58+
Plot.barX(alphabet, {x: "frequency", y: "letter", fillOpacity: 0.25, sort: {y: "-x"}}),
59+
Plot.crosshairY(alphabet, {x: "frequency", y: "letter", ruleStrokeOpacity: 1})
60+
]
61+
})
62+
```
63+
:::
464

565
## Crosshair options
666

767
The following options are supported:
868

9-
69+
* **maxRadius** - reach of the crosshair pointer; defaults to 40 pixels
70+
* **ruleStroke** - the rule color
71+
* **textFill** - the text fill color
72+
* **textStroke** - the text stroke color; defaults to *white* to improve legibility
73+
* **color** - shorthand for setting both **ruleStroke** and **textFill**
74+
* **ruleStrokeOpacity** - the rule stroke opacity; defaults to 0.2
75+
* **ruleStrokeWidth** - the rule stroke width; defaults to 1
76+
* **ruleStrokeOpacity** - the rule stroke opacity; defaults to 0.2
77+
* **textStrokeOpacity** - the text stroke opacity; defaults to 1
78+
* **textStrokeOpacity** - the text stroke width; defaults to 5
79+
* **dx** - the horizontal shift of the text relative to the *y* axis, in pixels; defaults to -9
80+
* **dy** - the vertical shift of the text relative to the *x* axis, in pixels; defaults to 9
81+
82+
1083

1184
## crosshair(*data*, *options*)
1285

1386
```js
14-
Plot.crosshair(flare, {path: "name", delimiter: "."})
87+
Plot.crosshair(cars, {x: "economy (mpg)", y: "cylinders"})
1588
```
1689

17-
Returns a new crosshair mark with the given *data* and *options*.
90+
Returns a new crosshair for the given *data* and *options*, drawing horizontal and vertical rules centered at the point closest to the [pointer](./pointer.md#pointeroptions). The corresponding **x** and **y** values are also drawn just outside the bottom and left sides of the frame, respectively, typically on top of the axes. If either **x** or **y** is not specified, the crosshair will be one-dimensional.
1891

1992
## crosshairX(*data*, *options*)
2093

94+
```js
95+
Plot.crosshairX(aapl, {x: "Date", y: "Close"})
96+
```
97+
98+
Like crosshair, but uses the [pointerX](./pointer.md#pointerxoptions) transform: the determination of the closest point is heavily weighted by the *x* (horizontal↔︎) position; this should be used for plots where *x* represents the independent variable, such as time in a time-series chart, or the aggregated dimension when grouping or binning.
99+
21100
## crosshairY(*data*, *options*)
101+
102+
```js
103+
Plot.crosshairY(alphabet, {x: "frequency", y: "letter"})
104+
```
105+
106+
Like crosshair, but uses the [pointerY](./pointer.md#pointeryoptions) transform: the determination of the closest point is heavily weighted by the *y* (vertical↕︎) position; this should be used for plots where *y* represents the independent variable, such as time in a time-series chart, or the aggregated dimension when grouping or binning.

0 commit comments

Comments
 (0)