-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
244 lines (200 loc) · 6.19 KB
/
script.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// This example requires the Visualization library. Include the libraries=visualization
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=visualization">
let map, heatmap;
let planesByFlag = {};
let countryCodes;
let chart;
Chart.defaults.font.size = 14;
const flagsSelect = document.getElementById('select-planes');
let gradient = [
"rgba(142, 114, 194, 0.1)",
"rgba(58, 28, 113, 1)",
"rgba(58, 28, 113, 1)",
"rgba(58, 28, 113, 1)",
"rgba(58, 28, 113, 1)",
"rgba(58, 28, 113, 1)",
"rgba(77, 28, 169, 1)",
"rgba(194, 26, 220, 1)",
"rgba(194, 26, 220, 1)",
"rgba(194, 26, 220, 1)",
"rgba(216, 74, 88, 1)",
"rgba(216, 74, 88, 1)",
"rgba(216, 74, 88, 1)",
"rgba(216, 74, 88, 1)",
"rgba(223, 25, 25, 1)",
"rgba(223, 25, 25, 1)",
];
fetch('./data/country-codes.json')
.then((response) => response.json())
.then((json) => countryCodes = json);
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
zoom: 3,
center: { lat: 33.749374, lng: 10.451329 },
// center: { lat: 30, lng: 0 },
mapId: "abdf9465a5f0e4c7",
});
map.addListener("maptypeid_changed", () => {
if (map.getMapTypeId() !== "roadmap") {
heatmap.set("gradient", null);
} else {
heatmap.set("gradient", gradient);
}
})
getPoints();
}
function getPoints(specialFlag = null) {
let url;
if (specialFlag && specialFlag != 'all') {
url = `https://airlabs.co/api/v9/flights?api_key=8acc5d02-45ec-4547-ac4c-fc53fd8724d1&flag=${specialFlag}`;
} else {
url = "https://airlabs.co/api/v9/flights?api_key=8acc5d02-45ec-4547-ac4c-fc53fd8724d1";
}
fetch(url.toString())
.then(response => response.json())
.then(data => {
createHeatmap(data);
})
.catch(e => {
console.log(e);
})
}
const createHeatmap = (data) => {
let points = [];
for (const plane of data.response) {
points.push(new google.maps.LatLng(plane.lat, plane.lng));
if (plane.flag) {
if (plane.flag == "UK" || plane.flag == "GB") {
plane.flag = "GB&flag=UK"
}
if (plane.flag in planesByFlag) {
planesByFlag[plane.flag] += 1;
} else {
planesByFlag[plane.flag] = 1;
}
}
}
if (heatmap) {
heatmap.setData(points);
} else {
heatmap = new google.maps.visualization.HeatmapLayer({
data: points,
map: map,
radius: 12,
gradient: gradient
});
updatePlanesSelect();
loadChart();
}
}
const updatePlanesSelect = () => {
let flags = Object.keys(planesByFlag);
flags.sort((flag_a, flag_b) => planesByFlag[flag_b] - planesByFlag[flag_a]);
for (const flagCode of flags) {
let country = countryCodes.filter(country => country.code == flagCode)[0];
if (country) {
flagsSelect.appendChild(new Option(`${country.name} - ${planesByFlag[flagCode]}`, flagCode));
} else {
flagsSelect.appendChild(new Option(`${flagCode} - ${planesByFlag[flagCode]}`, flagCode));
}
}
}
flagsSelect.addEventListener('change', (event) => {
let select = event.target;
getPoints(select.value);
})
// window.initMap = initMap;
const loadChart = () => {
let width = window.innerWidth;
let showOptions = 7;
if (width <= 350) {
showOptions = 3;
}
else if (width <= 500) {
showOptions = 4;
}
else if (width <= 672) {
showOptions = 5;
}
else if (width <= 768) {
showOptions = 6;
}
let allFlags = Object.keys(planesByFlag);
allFlags.sort((flag_a, flag_b) => planesByFlag[flag_b] - planesByFlag[flag_a]);
const flags = allFlags.slice(0, showOptions);
let labels = flags.map(flagCode => countryCodes.filter(country => country.code == flagCode)[0].name);
let planeNumbers = allFlags.slice(0, 7).map(flagCode => planesByFlag[flagCode])
const data = {
labels: labels,
datasets: [{
axis: 'y',
label: 'Planes by the flag',
backgroundColor: 'rgba(66, 25, 148, 1)',
data: planeNumbers,
inflateAmount: -3,
borderRadius: 3,
hoverBackgroundColor: 'rgba(109, 56, 214, 0.8)'
}]
};
const config = {
type: 'bar',
data: data,
plugins: [ChartDeferred],
options: {
indexAxis: 'y',
plugins: {
deferred: {
xOffset: 150, // defer until 150px of the canvas width are inside the viewport
yOffset: '50%', // defer until 50% of the canvas height are inside the viewport
}
},
transitions: {
show: {
animations: {
x: {
from: 0
}
}
},
hide: {
animations: {
x: {
to: 0
},
}
}
},
animations: {
y: { duration: 0 }
},
}
};
chart = new Chart(
document.getElementById('planeByCountriesChart'),
config
);
}
const updateLegend = () => {
let width = window.innerWidth;
let showOptions = 7;
if (width <= 350) {
showOptions = 3;
}
else if (width <= 500) {
showOptions = 4;
}
else if (width <= 672) {
showOptions = 5;
}
else if (width <= 768) {
showOptions = 6;
}
let allFlags = Object.keys(planesByFlag);
allFlags.sort((flag_a, flag_b) => planesByFlag[flag_b] - planesByFlag[flag_a]);
const flags = allFlags.slice(0, showOptions);
let labels = flags.map(flagCode => countryCodes.filter(country => country.code == flagCode)[0].name);
chart.data.labels = labels;
chart.update();
}
window.addEventListener('resize', updateLegend);