-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathgeo-tip.ts
154 lines (146 loc) · 4.54 KB
/
geo-tip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as Plot from "@observablehq/plot";
import * as d3 from "d3";
import {feature} from "topojson-client";
export async function geoText() {
const london = feature(await d3.json("data/london.json"), "boroughs");
return Plot.plot({
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
marks: [
Plot.geo(london),
Plot.text(london.features, Plot.centroid({text: "id", stroke: "var(--plot-background)", fill: "currentColor"}))
]
});
}
/** The geo mark with the tip option. */
export async function geoTip() {
const [london, boroughs] = await getLondonBoroughs();
const access = await getLondonAccess();
return Plot.plot({
width: 900,
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
color: {scheme: "RdYlBu", pivot: 0.5},
marks: [
Plot.geo(access, {
fx: "year",
geometry: (d) => boroughs.get(d.borough),
fill: "access",
stroke: "var(--plot-background)",
strokeWidth: 0.75,
channels: {borough: "borough"},
tip: true
})
]
});
}
/** The geo mark with the tip option and the centroid transform. */
export async function geoTipCentroid() {
const [london, boroughs] = await getLondonBoroughs();
const access = await getLondonAccess();
return Plot.plot({
width: 900,
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
color: {scheme: "RdYlBu", pivot: 0.5},
marks: [
Plot.geo(
access,
Plot.centroid({
fx: "year",
geometry: (d) => boroughs.get(d.borough),
fill: "access",
stroke: "var(--plot-background)",
strokeWidth: 0.75,
channels: {borough: "borough"},
tip: true
})
)
]
});
}
/** The geo mark with the tip option and the poi transform. */
export async function geoTipPoi() {
const [london, boroughs] = await getLondonBoroughs();
const access = await getLondonAccess();
return Plot.plot({
width: 900,
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
color: {scheme: "RdYlBu", pivot: 0.5},
marks: [
Plot.geo(
access,
Plot.poi({
fx: "year",
geometry: (d) => boroughs.get(d.borough),
fill: "access",
stroke: "var(--plot-background)",
strokeWidth: 0.75,
channels: {borough: "borough"},
tip: true
})
)
]
});
}
/** The geo mark with the tip option and the geoCentroid transform. */
export async function geoTipGeoCentroid() {
const [london, boroughs] = await getLondonBoroughs();
const access = await getLondonAccess();
return Plot.plot({
width: 900,
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
color: {scheme: "RdYlBu", pivot: 0.5},
marks: [
Plot.geo(
access,
Plot.geoCentroid({
fx: "year",
geometry: (d) => boroughs.get(d.borough),
fill: "access",
stroke: "var(--plot-background)",
strokeWidth: 0.75,
channels: {borough: "borough"},
tip: true
})
)
]
});
}
function getFirstPoint(feature) {
return feature.geometry.type === "Polygon"
? feature.geometry.coordinates[0][0]
: feature.geometry.coordinates[0][0][0];
}
/** The geo mark with the tip option and x and y channels. */
export async function geoTipXY() {
const [london, boroughs] = await getLondonBoroughs();
const access = await getLondonAccess();
return Plot.plot({
width: 900,
projection: {type: "transverse-mercator", rotate: [2, 0, 0], domain: london},
color: {scheme: "RdYlBu", pivot: 0.5},
marks: [
Plot.geo(access, {
x: (d) => getFirstPoint(boroughs.get(d.borough))[0],
y: (d) => getFirstPoint(boroughs.get(d.borough))[1],
fx: "year",
geometry: (d) => boroughs.get(d.borough),
fill: "access",
stroke: "var(--plot-background)",
strokeWidth: 0.75,
channels: {borough: "borough"},
tip: true
})
]
});
}
async function getLondonBoroughs() {
const london = feature(await d3.json("data/london.json"), "boroughs");
const boroughs = new Map(london.features.map((d) => [d.id, d]));
return [london, boroughs];
}
async function getLondonAccess() {
return (await d3.csv<any>("data/london-car-access.csv", d3.autoType)).flatMap(({borough, y2001, y2011, y2021}) => [
{borough, year: "2001", access: y2001},
{borough, year: "2011", access: y2011},
{borough, year: "2021", access: y2021}
]);
}