-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
217 lines (186 loc) · 5.17 KB
/
app.js
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
212
213
214
215
216
217
import React, {Component} from 'react';
import {render} from 'react-dom';
import {StaticMap} from 'react-map-gl';
import DeckGL, {GeoJsonLayer} from 'deck.gl';
import {scaleLinear, scaleThreshold} from 'd3-scale';
// Set your mapbox token here
const MAPBOX_TOKEN = "pk.eyJ1IjoiamdpcHBlciIsImEiOiJjanBhbnR0aWwwNXUyM2tudTdkdWw4NWd6In0.bP3cWqFy7sjN3vxcN2nxkw"; // eslint-disable-line
// Source data GeoJSON
const DATA_URL = {
ACCIDENTS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/highway/accidents.csv',
ROADS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/highway/roads.json'
};
function getKey({state, type, id}) {
return `${state}-${type}-${id}`;
}
export const COLOR_SCALE = scaleThreshold()
.domain([0, 4, 8, 12, 20, 32, 52, 84, 136, 220])
.range([
[26, 152, 80],
[102, 189, 99],
[166, 217, 106],
[217, 239, 139],
[255, 255, 191],
[254, 224, 139],
[253, 174, 97],
[244, 109, 67],
[215, 48, 39],
[168, 0, 0]
]);
const WIDTH_SCALE = scaleLinear()
.clamp(true)
.domain([0, 200])
.range([10, 2000]);
export const INITIAL_VIEW_STATE = {
latitude: 38,
longitude: -100,
zoom: 4,
minZoom: 2,
maxZoom: 8
};
export class App extends Component {
constructor(props) {
super(props);
this.state = {
hoveredObject: null,
...this._aggregateAccidents(props.accidents)
};
this._onHover = this._onHover.bind(this);
this._renderTooltip = this._renderTooltip.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.accidents !== this.props.accidents) {
this.setState({
...this._aggregateAccidents(nextProps.accidents)
});
}
}
_aggregateAccidents(accidents) {
const incidents = {};
const fatalities = {};
if (accidents) {
accidents.forEach(a => {
const r = (incidents[a.year] = incidents[a.year] || {});
const f = (fatalities[a.year] = fatalities[a.year] || {});
const key = getKey(a);
r[key] = a.incidents;
f[key] = a.fatalities;
});
}
return {incidents, fatalities};
}
_getLineColor(f, fatalities) {
if (!fatalities) {
return [200, 200, 200];
}
const key = getKey(f.properties);
const fatalitiesPer1KMile = ((fatalities[key] || 0) / f.properties.length) * 1000;
return COLOR_SCALE(fatalitiesPer1KMile);
}
_getLineWidth(f, incidents) {
if (!incidents) {
return 10;
}
const key = getKey(f.properties);
const incidentsPer1KMile = ((incidents[key] || 0) / f.properties.length) * 1000;
return WIDTH_SCALE(incidentsPer1KMile);
}
_onHover({x, y, object}) {
this.setState({x, y, hoveredObject: object});
}
_renderLayers() {
const {roads = DATA_URL.ROADS, year} = this.props;
const {incidents, fatalities} = this.state;
return [
new GeoJsonLayer({
id: 'geojson',
data: roads,
opacity: 1,
stroked: false,
filled: false,
lineWidthMinPixels: 0.5,
parameters: {
depthTest: false
},
getLineColor: f => this._getLineColor(f, fatalities[year]),
getLineWidth: f => this._getLineWidth(f, incidents[year]),
pickable: true,
onHover: this._onHover,
updateTriggers: {
getLineColor: {year},
getLineWidth: {year}
},
transitions: {
getLineColor: 1000,
getLineWidth: 1000
}
})
];
}
_renderTooltip() {
const {hoveredObject, x, y, fatalities, incidents} = this.state;
const {year} = this.props;
if (!hoveredObject) {
return null;
}
const props = hoveredObject.properties;
const key = getKey(props);
const f = fatalities[year][key];
const r = incidents[year][key];
const content = r ? (
<div>
<b>{f}</b> people died in <b>{r}</b> crashes on{' '}
{props.type === 'SR' ? props.state : props.type}-{props.id} in <b>{year}</b>
</div>
) : (
<div>
no accidents recorded in <b>{year}</b>
</div>
);
return (
<div className="tooltip" style={{left: x, top: y}}>
<big>
{props.name} ({props.state})
</big>
{content}
</div>
);
}
render() {
const {viewState, controller = true, baseMap = true} = this.props;
return (
<DeckGL
layers={this._renderLayers()}
pickingRadius={5}
initialViewState={INITIAL_VIEW_STATE}
viewState={viewState}
controller={controller}
>
{baseMap && (
<StaticMap
reuseMaps
mapStyle="mapbox://styles/mapbox/dark-v9"
preventStyleDiffing={true}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
)}
{this._renderTooltip}
</DeckGL>
);
}
}
export function renderToDOM(container) {
render(<App />, container);
const formatRow = d => ({
...d,
incidents: Number(d.incidents),
fatalities: Number(d.fatalities)
});
require('d3-request').csv(DATA_URL.ACCIDENTS, formatRow, (error, response) => {
if (!error) {
render(<App accidents={response} year={response[0].year} />, container);
}
});
}