-
Notifications
You must be signed in to change notification settings - Fork 2k
/
app.jsx
197 lines (173 loc) · 5.12 KB
/
app.jsx
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
/* global fetch */
import React, {useMemo} from 'react';
import {createRoot} from 'react-dom/client';
import {Map} from 'react-map-gl';
import maplibregl from 'maplibre-gl';
import DeckGL from '@deck.gl/react';
import {ScatterplotLayer, ArcLayer} from '@deck.gl/layers';
import {BrushingExtension} from '@deck.gl/extensions';
import {scaleLinear} from 'd3-scale';
// Source data GeoJSON
const DATA_URL =
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/arc/counties.json'; // eslint-disable-line
export const inFlowColors = [[35, 181, 184]];
export const outFlowColors = [[166, 3, 3]];
// migrate out
const SOURCE_COLOR = [166, 3, 3];
// migrate in
const TARGET_COLOR = [35, 181, 184];
const INITIAL_VIEW_STATE = {
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';
const brushingExtension = new BrushingExtension();
/* eslint-disable max-nested-callbacks */
function getLayerData(data) {
if (!data || !data.length) {
return {};
}
const arcs = [];
const targets = [];
const sources = [];
const pairs = {};
data.forEach((county, i) => {
const {flows, centroid: targetCentroid} = county.properties;
const value = {gain: 0, loss: 0};
Object.keys(flows).forEach(toId => {
value[flows[toId] > 0 ? 'gain' : 'loss'] += flows[toId];
// if number too small, ignore it
if (Math.abs(flows[toId]) < 50) {
return;
}
const pairKey = [i, Number(toId)].sort((a, b) => a - b).join('-');
const sourceCentroid = data[toId].properties.centroid;
const gain = Math.sign(flows[toId]);
// add point at arc source
sources.push({
position: sourceCentroid,
target: targetCentroid,
name: data[toId].properties.name,
radius: 3,
gain: -gain
});
// eliminate duplicates arcs
if (pairs[pairKey]) {
return;
}
pairs[pairKey] = true;
arcs.push({
target: gain > 0 ? targetCentroid : sourceCentroid,
source: gain > 0 ? sourceCentroid : targetCentroid,
value: flows[toId]
});
});
// add point at arc target
targets.push({
...value,
position: [targetCentroid[0], targetCentroid[1], 10],
net: value.gain + value.loss,
name: county.properties.name
});
});
// sort targets by radius large -> small
targets.sort((a, b) => Math.abs(b.net) - Math.abs(a.net));
const sizeScale = scaleLinear()
.domain([0, Math.abs(targets[0].net)])
.range([36, 400]);
targets.forEach(pt => {
pt.radius = Math.sqrt(sizeScale(Math.abs(pt.net)));
});
return {arcs, targets, sources};
}
function getTooltip({object}) {
return (
object &&
`\
${object.name}
Net gain: ${object.net}`
);
}
/* eslint-disable react/no-deprecated */
export default function App({
data,
enableBrushing = true,
brushRadius = 100000,
strokeWidth = 1,
opacity = 0.7,
mapStyle = MAP_STYLE
}) {
const {arcs, targets, sources} = useMemo(() => getLayerData(data), [data]);
const layers = arcs &&
targets && [
new ScatterplotLayer({
id: 'sources',
data: sources,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
// only show source points when brushing
radiusScale: enableBrushing ? 3000 : 0,
getFillColor: d => (d.gain > 0 ? TARGET_COLOR : SOURCE_COLOR),
extensions: [brushingExtension]
}),
new ScatterplotLayer({
id: 'targets-ring',
data: targets,
brushingRadius: brushRadius,
lineWidthMinPixels: 2,
stroked: true,
filled: false,
brushingEnabled: enableBrushing,
// only show rings when brushing
radiusScale: enableBrushing ? 4000 : 0,
getLineColor: d => (d.net > 0 ? TARGET_COLOR : SOURCE_COLOR),
extensions: [brushingExtension]
}),
new ScatterplotLayer({
id: 'targets',
data: targets,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
pickable: true,
radiusScale: 3000,
getFillColor: d => (d.net > 0 ? TARGET_COLOR : SOURCE_COLOR),
extensions: [brushingExtension]
}),
new ArcLayer({
id: 'arc',
data: arcs,
getWidth: strokeWidth,
opacity,
brushingRadius: brushRadius,
brushingEnabled: enableBrushing,
getSourcePosition: d => d.source,
getTargetPosition: d => d.target,
getSourceColor: SOURCE_COLOR,
getTargetColor: TARGET_COLOR,
extensions: [brushingExtension]
})
];
return (
<DeckGL
layers={layers}
initialViewState={INITIAL_VIEW_STATE}
controller={true}
getTooltip={getTooltip}
>
<Map reuseMaps mapLib={maplibregl} mapStyle={mapStyle} preventStyleDiffing={true} />
</DeckGL>
);
}
export function renderToDOM(container) {
const root = createRoot(container);
root.render(<App />);
fetch(DATA_URL)
.then(response => response.json())
.then(({features}) => {
root.render(<App data={features} />);
});
}