-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
app.js
131 lines (115 loc) · 3.08 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
import React, {Component} from 'react';
import {render} from 'react-dom';
import {StaticMap} from 'react-map-gl';
import DeckGL, {LineLayer, ScatterplotLayer} from 'deck.gl';
import GL from '@luma.gl/constants';
// Set your mapbox token here
const MAPBOX_TOKEN = process.env.MapboxAccessToken; // eslint-disable-line
// Source data CSV
const DATA_URL = {
AIRPORTS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/line/airports.json', // eslint-disable-line
FLIGHT_PATHS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/line/heathrow-flights.json' // eslint-disable-line
};
const INITIAL_VIEW_STATE = {
latitude: 47.65,
longitude: 7,
zoom: 4.5,
maxZoom: 16,
pitch: 50,
bearing: 0
};
function getColor(d) {
const z = d.start[2];
const r = z / 10000;
return [255 * (1 - r * 2), 128 * r, 255 * r, 255 * (1 - r)];
}
function getSize(type) {
if (type.search('major') >= 0) {
return 100;
}
if (type.search('small') >= 0) {
return 30;
}
return 60;
}
export class App extends Component {
constructor(props) {
super(props);
this.state = {
hoveredObject: null
};
this._onHover = this._onHover.bind(this);
this._renderTooltip = this._renderTooltip.bind(this);
}
_onHover({x, y, object}) {
this.setState({x, y, hoveredObject: object});
}
_renderTooltip() {
const {x, y, hoveredObject} = this.state;
return (
hoveredObject && (
<div className="tooltip" style={{left: x, top: y}}>
<div>{hoveredObject.country || hoveredObject.abbrev}</div>
<div>{hoveredObject.name.indexOf('0x') >= 0 ? '' : hoveredObject.name}</div>
</div>
)
);
}
_renderLayers() {
const {
airports = DATA_URL.AIRPORTS,
flightPaths = DATA_URL.FLIGHT_PATHS,
getWidth = 3
} = this.props;
return [
new ScatterplotLayer({
id: 'airports',
data: airports,
radiusScale: 20,
getPosition: d => d.coordinates,
getFillColor: [255, 140, 0],
getRadius: d => getSize(d.type),
pickable: true,
onHover: this._onHover
}),
new LineLayer({
id: 'flight-paths',
data: flightPaths,
getSourcePosition: d => d.start,
getTargetPosition: d => d.end,
getColor,
getWidth,
pickable: true,
onHover: this._onHover
})
];
}
render() {
const {mapStyle = 'mapbox://styles/mapbox/dark-v9'} = this.props;
return (
<DeckGL
layers={this._renderLayers()}
initialViewState={INITIAL_VIEW_STATE}
controller={true}
pickingRadius={5}
parameters={{
blendFunc: [GL.SRC_ALPHA, GL.ONE, GL.ONE_MINUS_DST_ALPHA, GL.ONE],
blendEquation: GL.FUNC_ADD
}}
>
<StaticMap
reuseMaps
mapStyle={mapStyle}
preventStyleDiffing={true}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
{this._renderTooltip}
</DeckGL>
);
}
}
export function renderToDOM(container) {
render(<App />, container);
}