Skip to content

Commit 430388f

Browse files
committed
update docs
1 parent 5a77d7a commit 430388f

File tree

7 files changed

+572
-47
lines changed

7 files changed

+572
-47
lines changed

docs/marks/geo.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Plot.plot({
4949
marks: [
5050
Plot.geo(counties, {
5151
fill: (d) => d.properties.unemployment,
52-
title: (d) => `${d.properties.name}\n${d.properties.unemployment}%`
52+
title: (d) => `${d.properties.name} ${d.properties.unemployment}%`,
53+
tip: true
5354
})
5455
]
5556
})
@@ -129,17 +130,16 @@ Plot.plot({
129130
```
130131
:::
131132

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+
The geo mark doesn’t have **x** and **y** channels; when you use the [**tip** option](./tip.md), the [centroid transform](../transforms/centroid.md) is implicitly applied on the geometries to compute the tip position. <VersionBadge pr="2088" /> The centroids are shown below in red.
133134

134135
:::plot defer https://observablehq.com/@observablehq/plot-state-centroids
135136
```js
136137
Plot.plot({
137138
projection: "albers-usa",
138139
marks: [
139-
Plot.geo(statemesh, {strokeOpacity: 0.2}),
140+
Plot.geo(states, {strokeOpacity: 0.1, tip: true, title: (d) => d.properties.name}),
140141
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})))
142+
Plot.dot(states, Plot.centroid({fill: "red", stroke: "var(--vp-c-bg-alt)"}))
143143
]
144144
})
145145
```
@@ -157,7 +157,7 @@ Plot.plot({
157157
marks: [
158158
Plot.geo(statemesh, {strokeOpacity: 0.2}),
159159
Plot.geo(nation),
160-
Plot.geo(walmarts, {fy: (d) => d.properties.date, r: 1.5, fill: "blue"}),
160+
Plot.geo(walmarts, {fy: (d) => d.properties.date, r: 1.5, fill: "blue", tip: true, title: (d) => d.properties.date}),
161161
Plot.axisFy({frameAnchor: "top", dy: 30, tickFormat: (d) => `${d.getUTCFullYear()}’s`})
162162
]
163163
})

src/plot.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,10 @@ function derive(mark, options = {}) {
513513
return initializer({...options, x: null, y: null}, (_data, _facets, _channels, scales, dimensions, context) => {
514514
const state = context.getMarkState(mark);
515515
const {facets, channels} = state;
516-
// Promote the geometry channel to the x and y channel using the centroid initializer, if needed.
517-
if (!channels.x && !channels.y && channels.geometry) {
518-
const update = centroid().initializer(channels.geometry.value, facets, channels, scales, dimensions, context);
516+
const {x, y, geometry} = channels;
517+
if (!x && !y && geometry) {
518+
// Promote the geometry channel to x and y channels using the centroid initializer.
519+
const update = centroid().initializer(geometry.value, facets, channels, scales, dimensions, context);
519520
return {...state, channels: {...channels, x: update.channels.x, y: update.channels.y}};
520521
}
521522
return state;

test/output/geoText.svg

Lines changed: 86 additions & 0 deletions
Loading

test/output/geoTip.svg

Lines changed: 142 additions & 0 deletions
Loading

test/output/geoTip2.svg

Lines changed: 131 additions & 0 deletions
Loading

test/output/geoTipCentroid.svg

Lines changed: 142 additions & 0 deletions
Loading

test/plots/geo-tip.ts

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import * as Plot from "@observablehq/plot";
22
import * as d3 from "d3";
33
import {feature} from "topojson-client";
44

5-
// export async function londonMap() {
6-
// const london = feature(await d3.json("data/london.json"), "boroughs");
7-
// return Plot.plot({
8-
// projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
9-
// marks: [
10-
// Plot.geo(london),
11-
// Plot.text(london.features, Plot.centroid({text: "id", stroke: "var(--plot-background)", fill: "currentColor"}))
12-
// ]
13-
// });
14-
// }
5+
export async function geoText() {
6+
const london = feature(await d3.json("data/london.json"), "boroughs");
7+
return Plot.plot({
8+
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
9+
marks: [
10+
Plot.geo(london),
11+
Plot.text(london.features, Plot.centroid({text: "id", stroke: "var(--plot-background)", fill: "currentColor"}))
12+
]
13+
});
14+
}
1515

1616
export async function geoTip() {
1717
const london = feature(await d3.json("data/london.json"), "boroughs");
@@ -33,37 +33,60 @@ export async function geoTip() {
3333
fill: "access",
3434
stroke: "var(--plot-background)",
3535
strokeWidth: 0.75,
36+
channels: {borough: "borough"},
3637
tip: true
3738
})
3839
]
3940
});
4041
}
4142

42-
// export async function londonCarAccessGeoCentroid() {
43-
// const london = feature(await d3.json("data/london.json"), "boroughs");
44-
// const wide = await d3.csv<any>("data/london-car-access.csv", d3.autoType);
45-
// const access = wide.flatMap(({borough, y2001, y2011, y2021}) => [
46-
// {borough, year: "2001", access: y2001},
47-
// {borough, year: "2011", access: y2011},
48-
// {borough, year: "2021", access: y2021}
49-
// ]);
50-
// const boroughs = new Map(london.features.map((d) => [d.id, d]));
51-
// return Plot.plot({
52-
// width: 900,
53-
// projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
54-
// color: {scheme: "RdYlBu", pivot: 0.5},
55-
// marks: [
56-
// Plot.geo(
57-
// access,
58-
// Plot.geoCentroid({
59-
// fx: "year",
60-
// geometry: (d) => boroughs.get(d.borough),
61-
// fill: "access",
62-
// stroke: "var(--plot-background)",
63-
// strokeWidth: 0.75,
64-
// tip: true
65-
// })
66-
// )
67-
// ]
68-
// });
69-
// }
43+
export async function geoTip2() {
44+
const us = await d3.json<any>("data/us-counties-10m.json");
45+
const nation = feature(us, us.objects.nation);
46+
const states = feature(us, us.objects.states).features;
47+
return Plot.plot({
48+
projection: "albers-usa",
49+
marks: [
50+
Plot.geo(states, {strokeOpacity: 0.1, tip: true, title: (d) => d.properties.name}),
51+
Plot.geo(nation),
52+
Plot.dot(states, Plot.centroid({fill: "red", stroke: "black"}))
53+
]
54+
});
55+
}
56+
57+
export async function geoTipCentroid() {
58+
const london = feature(await d3.json("data/london.json"), "boroughs");
59+
const wide = await d3.csv<any>("data/london-car-access.csv", d3.autoType);
60+
const access = wide.flatMap(({borough, y2001, y2011, y2021}) => [
61+
{borough, year: "2001", access: y2001},
62+
{borough, year: "2011", access: y2011},
63+
{borough, year: "2021", access: y2021}
64+
]);
65+
const boroughs = new Map(london.features.map((d) => [d.id, d]));
66+
return Plot.plot({
67+
width: 900,
68+
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
69+
color: {scheme: "RdYlBu", pivot: 0.5},
70+
marks: [
71+
Plot.geo(access, {
72+
fx: "year",
73+
geometry: (d) => boroughs.get(d.borough),
74+
fill: "access",
75+
stroke: "var(--plot-background)",
76+
strokeWidth: 0.75
77+
}),
78+
Plot.tip(
79+
access,
80+
Plot.pointer(
81+
Plot.geoCentroid({
82+
fx: "year",
83+
geometry: (d) => boroughs.get(d.borough),
84+
fill: "access",
85+
format: {x: null, y: null},
86+
channels: {borough: "borough"}
87+
})
88+
)
89+
)
90+
]
91+
});
92+
}

0 commit comments

Comments
 (0)