Skip to content

Commit 634c8dd

Browse files
Filmbostock
andauthored
document geo+centroid (#1652)
Co-authored-by: Mike Bostock <[email protected]>
1 parent 7f67431 commit 634c8dd

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

docs/marks/geo.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const walmarts = shallowRef({type: "FeatureCollection", features: []});
1111
const world = shallowRef(null);
1212
const statemesh = computed(() => us.value ? topojson.mesh(us.value, us.value.objects.states, (a, b) => a !== b) : {type: null});
1313
const nation = computed(() => us.value ? topojson.feature(us.value, us.value.objects.nation) : {type: null});
14+
const states = computed(() => us.value ? topojson.feature(us.value, us.value.objects.states).features : []);
1415
const counties = computed(() => us.value ? topojson.feature(us.value, us.value.objects.counties).features : []);
1516
const land = computed(() => world.value ? topojson.feature(world.value, world.value.objects.land) : {type: null});
1617

@@ -128,6 +129,22 @@ Plot.plot({
128129
```
129130
:::
130131

132+
The geo mark doesn’t have **x** and **y** channels; to derive those, for example to add [interactive tips](./tip.md), you can apply a [centroid transform](../transforms/centroid.md) on the geometries.
133+
134+
:::plot defer https://observablehq.com/@observablehq/plot-state-centroids
135+
```js
136+
Plot.plot({
137+
projection: "albers-usa",
138+
marks: [
139+
Plot.geo(statemesh, {strokeOpacity: 0.2}),
140+
Plot.geo(nation),
141+
Plot.dot(states, Plot.centroid({fill: "red", stroke: "var(--vp-c-bg-alt)"})),
142+
Plot.tip(states, Plot.pointer(Plot.centroid({title: (d) => d.properties.name})))
143+
]
144+
})
145+
```
146+
:::
147+
131148
The geo mark supports [faceting](../features/facets.md). Below, a comic strip of sorts shows the locations of Walmart store openings in past decades.
132149

133150
:::plot defer https://observablehq.com/@observablehq/plot-map-large-multiples

docs/transforms/centroid.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import * as topojson from "topojson-client";
66
import {computed, shallowRef, onMounted} from "vue";
77

88
const us = shallowRef(null);
9-
const countymesh = computed(() => us.value ? topojson.mesh(us.value, us.value.objects.counties) : {type: null});
109
const statemesh = computed(() => us.value ? topojson.mesh(us.value, us.value.objects.states) : {type: null});
1110
const states = computed(() => us.value ? topojson.feature(us.value, us.value.objects.states).features : []);
1211
const counties = computed(() => us.value ? topojson.feature(us.value, us.value.objects.counties).features : []);
12+
const nation = computed(() => us.value ? topojson.feature(us.value, us.value.objects.nation) : []);
1313

1414
onMounted(() => {
1515
d3.json("../data/us-counties-10m.json").then((data) => (us.value = data));
@@ -19,7 +19,7 @@ onMounted(() => {
1919

2020
# Centroid transform
2121

22-
Plot offers two transforms that derive centroids from GeoJSON geometries: [centroid](#centroid-options) and [geoCentroid](#geocentroid-options). These transforms can be used by any mark that accepts **x** and **y** channels. For instance, to label U.S. states we can use a [text mark](../marks/text.md).
22+
Plot offers two transforms that derive centroids from GeoJSON geometries: [centroid](#centroid-options) and [geoCentroid](#geocentroid-options). These transforms can be used by any mark that accepts **x** and **y** channels. Below, a [text mark](../marks/text.md) labels the U.S. states.
2323

2424
:::plot defer https://observablehq.com/@observablehq/plot-state-labels
2525
```js
@@ -68,6 +68,22 @@ Plot.dot(counties, Plot.hexbin({r:"count"}, Plot.geoCentroid())).plot({projectio
6868
```
6969
:::
7070

71+
Combined with the [pointer transform](../interactions/pointer.md), the centroid transform can add [interactive tips](../marks/tip.md) on a map:
72+
73+
:::plot defer https://observablehq.com/@observablehq/plot-state-centroids
74+
```js
75+
Plot.plot({
76+
projection: "albers-usa",
77+
marks: [
78+
Plot.geo(statemesh, {strokeOpacity: 0.2}),
79+
Plot.geo(nation),
80+
Plot.dot(states, Plot.centroid({fill: "red", stroke: "var(--vp-c-bg-alt)"})),
81+
Plot.tip(states, Plot.pointer(Plot.centroid({title: (d) => d.properties.name})))
82+
]
83+
})
84+
```
85+
:::
86+
7187
## centroid(*options*)
7288

7389
```js

0 commit comments

Comments
 (0)