-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
app.tsx
211 lines (190 loc) · 5.81 KB
/
app.tsx
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* global fetch */
import React, {useMemo} from 'react';
import {createRoot} from 'react-dom/client';
import {Map} from 'react-map-gl/maplibre';
import DeckGL from '@deck.gl/react';
import {ScatterplotLayer, ArcLayer} from '@deck.gl/layers';
import {BrushingExtension} from '@deck.gl/extensions';
import {scaleSqrt} from 'd3-scale';
import type {Color, PickingInfo, MapViewState} from '@deck.gl/core';
import type {BrushingExtensionProps} from '@deck.gl/extensions';
import type {Feature, Polygon, MultiPolygon} from 'geojson';
// Source data GeoJSON
const DATA_URL =
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/arc/counties.json'; // eslint-disable-line
export const inFlowColor: Color = [35, 181, 184];
export const outFlowColor: Color = [166, 3, 3];
const INITIAL_VIEW_STATE: MapViewState = {
longitude: -100,
latitude: 40.7,
zoom: 3,
maxZoom: 15,
pitch: 0,
bearing: 0
};
const MAP_STYLE = 'https://basemaps.cartocdn.com/gl/positron-nolabels-gl-style/style.json';
type CountyProperties = {
/** county name */
name: string;
/** to another county index -> net flow */
flows: {[id: number]: number};
/** geographical centroid */
centroid: [lon: number, lat: number];
};
type County = Feature<Polygon | MultiPolygon, CountyProperties>;
type MigrationFlow = {
source: County;
target: County;
/** Number of migrants */
value: number;
};
type MigrationDestination = {
county: County;
/** Population net gain */
netGain: number;
};
const brushingExtension = new BrushingExtension();
/* eslint-disable max-nested-callbacks */
function getLayerData(data?: County[]): {
arcs: MigrationFlow[];
points: MigrationDestination[];
} | null {
if (!data || !data.length) {
return null;
}
const arcs: MigrationFlow[] = [];
const points: MigrationDestination[] = data
.map((county, fromId) => {
const {flows} = county.properties;
let netGain = 0;
Object.keys(flows).forEach(key => {
const toId = Number(key);
const value = flows[toId];
netGain -= value;
// if number too small, ignore it
if (value >= 50) {
arcs.push({
source: county,
target: data[toId],
value
});
}
});
return {county, netGain};
})
// sort points by radius large -> small
.sort((a, b) => Math.abs(b.netGain) - Math.abs(a.netGain));
return {arcs, points};
}
function getTooltip({object}: PickingInfo<MigrationDestination>) {
return (
object &&
`\
${object.county.properties.name}
Net gain: ${object.netGain}`
);
}
/* eslint-disable react/no-deprecated */
export default function App({
data,
enableBrushing = true,
brushRadius = 100000,
strokeWidth = 1,
opacity = 0.7,
mapStyle = MAP_STYLE
}: {
data?: County[];
enableBrushing?: boolean;
brushRadius?: number;
strokeWidth?: number;
opacity?: number;
mapStyle?: string;
}) {
const layerData = useMemo(() => getLayerData(data), [data]);
const radiusScale = useMemo(() => {
return (
layerData &&
scaleSqrt()
.domain([0, Math.abs(layerData.points[0].netGain)])
.range([1, 20])
);
}, [layerData]);
const layers = layerData && [
new ScatterplotLayer<MigrationFlow, BrushingExtensionProps<MigrationFlow>>({
id: 'sources-with-gain',
data: layerData.arcs,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
brushingTarget: 'custom',
getPosition: d => d.target.properties.centroid,
getRadius: d => radiusScale(d.value),
getBrushingTarget: d => d.source.properties.centroid,
getFillColor: inFlowColor,
// only show source points when brushing
visible: enableBrushing,
radiusScale: 3000,
extensions: [brushingExtension]
}),
new ScatterplotLayer<MigrationFlow, BrushingExtensionProps<MigrationFlow>>({
id: 'sources-with-loss',
data: layerData.arcs,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
brushingTarget: 'custom',
getPosition: d => d.source.properties.centroid,
getRadius: d => radiusScale(d.value),
getBrushingTarget: d => d.target.properties.centroid,
getFillColor: outFlowColor,
// only show source points when brushing
visible: enableBrushing,
radiusScale: 3000,
extensions: [brushingExtension]
}),
new ScatterplotLayer<MigrationDestination, BrushingExtensionProps>({
id: 'destinations',
data: layerData.points,
brushingRadius: brushRadius,
lineWidthMinPixels: 2,
stroked: true,
pickable: true,
brushingEnabled: enableBrushing,
radiusScale: 3000,
getPosition: d => d.county.properties.centroid,
getLineColor: d => (d.netGain > 0 ? inFlowColor : outFlowColor),
getFillColor: [0, 0, 0, 0],
getRadius: d => radiusScale(d.netGain) + 4,
extensions: [brushingExtension]
}),
new ArcLayer<MigrationFlow, BrushingExtensionProps>({
id: 'arc',
data: layerData.arcs,
getWidth: strokeWidth,
opacity,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
brushingTarget: 'source_target',
getSourcePosition: d => d.source.properties.centroid,
getTargetPosition: d => d.target.properties.centroid,
getSourceColor: outFlowColor,
getTargetColor: inFlowColor,
extensions: [brushingExtension]
})
];
return (
<DeckGL
layers={layers}
initialViewState={INITIAL_VIEW_STATE}
controller={true}
getTooltip={getTooltip}
>
<Map reuseMaps mapStyle={mapStyle} />
</DeckGL>
);
}
export async function renderToDOM(container: HTMLDivElement) {
const root = createRoot(container);
root.render(<App />);
const resp = await fetch(DATA_URL);
const {features} = await resp.json();
root.render(<App data={features} />);
}