@@ -2,16 +2,16 @@ import * as Plot from "@observablehq/plot";
2
2
import * as d3 from "d3" ;
3
3
import { feature } from "topojson-client" ;
4
4
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
+ }
15
15
16
16
export async function geoTip ( ) {
17
17
const london = feature ( await d3 . json ( "data/london.json" ) , "boroughs" ) ;
@@ -33,37 +33,60 @@ export async function geoTip() {
33
33
fill : "access" ,
34
34
stroke : "var(--plot-background)" ,
35
35
strokeWidth : 0.75 ,
36
+ channels : { borough : "borough" } ,
36
37
tip : true
37
38
} )
38
39
]
39
40
} ) ;
40
41
}
41
42
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