Skip to content

Commit a9424b8

Browse files
mbostockFil
andauthored
simpler geo label example (#1660)
Co-authored-by: Philippe Rivière <[email protected]>
1 parent 5744399 commit a9424b8

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

docs/transforms/centroid.md

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {computed, shallowRef, onMounted} from "vue";
88
const us = shallowRef(null);
99
const countymesh = computed(() => us.value ? topojson.mesh(us.value, us.value.objects.counties) : {type: null});
1010
const statemesh = computed(() => us.value ? topojson.mesh(us.value, us.value.objects.states) : {type: null});
11+
const states = computed(() => us.value ? topojson.feature(us.value, us.value.objects.states).features : []);
1112
const counties = computed(() => us.value ? topojson.feature(us.value, us.value.objects.counties).features : []);
1213

1314
onMounted(() => {
@@ -18,40 +19,21 @@ onMounted(() => {
1819

1920
# Centroid transform
2021

21-
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 the U.S. counties with names starting with V—this is more interesting than it seems—we can use a [dot mark](../marks/dot.md) and 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. For instance, to label U.S. states we can use a [text mark](../marks/text.md).
2223

23-
:::plot defer https://observablehq.com/@observablehq/plot-v-counties
24+
:::plot defer https://observablehq.com/@observablehq/plot-state-labels
2425
```js
2526
Plot.plot({
2627
projection: "albers-usa",
2728
marks: [
28-
Plot.geo(countymesh, {strokeWidth: 0.1}),
29-
Plot.geo(statemesh, {strokeWidth: 0.5}),
30-
Plot.dot(
31-
counties,
32-
Plot.centroid({
33-
filter: (d) => d.properties.name.match(/^V/),
34-
fill: "currentColor",
35-
stroke: "var(--vp-c-bg)"
36-
})
37-
),
38-
Plot.text(
39-
counties,
40-
Plot.centroid({
41-
filter: (d) => d.properties.name.match(/^V/),
42-
text: (d) => d.properties.name,
43-
fill: "currentColor",
44-
stroke: "var(--vp-c-bg)",
45-
frameAnchor: "left",
46-
dx: 6
47-
})
48-
)
29+
Plot.geo(statemesh),
30+
Plot.text(states, Plot.centroid({text: (d) => d.properties.name, fill: "currentColor", stroke: "var(--vp-c-bg)"}))
4931
]
5032
})
5133
```
5234
:::
5335

54-
Also fun, we can pass the centroid to the [voronoi mark](../marks/delaunay.md).
36+
For fun, we can pass county centroids to the [voronoi mark](../marks/delaunay.md).
5537

5638
:::plot defer https://observablehq.com/@observablehq/plot-centroid-voronoi
5739
```js

0 commit comments

Comments
 (0)