-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
518 lines (493 loc) · 15.5 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//* Map
const mymap = L.map(
'mapid',
{
minZoom: 2,
maxZoom: 5,
maxBounds: [
[90, -180],
[-90, 180],
],
},
{ doubleClickZoom: false }
).locate({
setView: true,
});
const mapData = [statesData, countriesjson, canadaprv];
let geojson = L.geoJson(mapData, {
style: style,
onEachFeature,
}).addTo(mymap);
const myIcon = L.icon({
iconUrl:
'data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAABsAAAAnCAYAAAD+bDODAAAJhElEQVRYhZ2Xe5BU1RHGf+ece+/Mzu7O7rJPWB7ydCGCURTkoQJGTYIi0SoDQQUiQikIZYgiIjECKopaUCqgQECCEkCJAkmAKnmIr4CKrIblJSACC8u+nzP3zr0nde8uShAQ0/9Mzczp/rpPd3/dR3Qs6MX5RApwPXJKqxlUWSv6KcVl4RDNlSTsesRicYpdl/+kp+jNWVHWKEmJp89rjnOC+SAacr4pEVNsR9zbo0An9+/mckU7j0uyNUkhTUNccPiUYOdByZZCxfY9ot409aI2OfopASfPBfoDMENBRS3DTpTIV/r9XKc9MsThVzclIAM4AVVHBbX1kBKBtJYa8oAKWL/R4LkVJpu/ENV52d7YjBSWJdwLgJkKjpYxIxaTU2aOSvDQQzbYsGSxwaKNBgdOSBwXlALXBUNChzyPe29OMHJkAiyYPdvi0YUGoZD3dMsspjiJc4D5ER0rY5qbUFPXzbTpNzzOP+eZDHkyRI0jGdjD5c5rE1zTycOyoLJG8OFuyYZdirUfK1JMzfKpMW4Z6/D+0hADJ1lIw30qP5PHT0eoMrNaBTmqqmNwea2c+94sh+uG2YwfmcSE50LcNdBjw1Nx7htlk+NpvjwER04IUkKa3wx1GNo/wbibXE5UCB5+MUTpfsn9j8e5vrnm1X8Y1ympC5Ms9vgpNHxEDZHiErly3tgEfe+JM3pohAVvm6ybHWfgrfEgV5QJ1v5b8fpmhQB6F3j07O1R860gKz3BwpkJbr82xMAJI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rxgvvjV1D5f/27lFsm76w1C+TpgkkBcgu9r1xv8bYukXb4eenb1/SQwXwyD48kp3p8fXWZSV6xIbdY4z1IzNPUnFJOWmUQi3jTT4NiP2fpRMD8vrbJ4cs8BfeyZ5SYiw2d3EM1g5nKTogO6uHU2T5ydz/8LLADUkN9cD33+HcXnHxkkdU/wxccGs95RtMjTQy/02vzJYH7SoxG2xR29evpKA8pMpq00iNn63bQIWy9UFGfKBd/U55DUmEP51a2F8ekR7YZMMv1Jd7HKFxXZd4cFNabi4Y27gz1wkhQXD+RLMM8uVoLHjGB7ahLpUjCt8TlxkQL8F8GvIqNHEqP+AAAAAElFTkSuQmCC',
iconSize: [27, 39],
iconAnchor: [13.5, 39],
popupAnchor: [0, -39],
});
mymap.addControl(new L.Control.Fullscreen());
L.control.BigImage({ position: 'topright' }).addTo(mymap);
L.control.textbox = L.Control.extend({
onAdd: function (mymap) {
const text = L.DomUtil.create('div');
text.id = 'order-text';
text.innerHTML = '<strong></strong>';
},
});
const items = [];
const contents = [];
const data = [];
const form = document.getElementById('fileForm');
const csvFile = document.getElementById('csvFile');
const submit = document.getElementById('submit');
const instructions = document.getElementById('instructions');
const insModal = document.getElementById('insModal');
const modal = document.getElementById('modal');
const list = document.getElementById('list');
const total = document.getElementById('total');
const topBar = document.getElementById('top');
const places = [];
const info = [];
const shipCountries = [];
let flattened = [];
let markers = 0;
let allTotal;
const unique = [];
let coordsObj;
const control = L.control();
const legend = [];
const listData = [];
const locations = [];
let startDate, endDate, year, month, start, end;
const regionsShipped = [];
const monthFull = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
const smallCountries = [
'Vatican City',
'Monaco',
'Gibraltar',
'Tokelau',
'Nauru',
'Saint Barthelemy',
'Tuvalu',
'Macau',
'Sint Maarten',
'Saint Martin',
'Bermuda',
'San Marino',
'Guernsey',
'Anguilla',
'Montserrat',
'Jersey',
'Wallis and Futuna',
'British Virgin Islands',
'Liechtenstein',
'Aruba',
'Marshall Islands',
'American Samoa',
'Cook Islands',
'Saint Pierre and Miquelon',
'Niue',
'Saint Kitts and Nevis',
'Cayman Islands',
'Maldives',
'Malta',
'Grenada',
'United States Virgin Islands',
'Mayotte',
'Saint Vincent and the Grenadines',
'Barbados',
'Antigua and Barbuda',
'Curacao',
'Seychelles',
'Palau',
'Northern Mariana Islands',
'Andorra',
'Guam',
'Isle of Man',
'Saint Lucia',
'Micronesia',
'Singapore',
'Tonga',
'Dominica',
'Bahrain',
'Kiribati',
'Turks and Caicos Islands',
'Sao Tome and Principe',
'Hong Kong',
'Martinique',
'Faroe Islands',
'Guadeloupe',
'Comoros',
'Mauritius',
'Reunion',
'Luxembourg',
'Samoa',
'Cape Verde',
'French Polynesia',
'Trinidad and Tobago',
'Brunei',
'Palestine',
'Puerto Rico',
'Cyprus',
'Lebanon',
'Gambia',
'Jamaica',
'Qatar',
'Falkland Islands',
'Vanuatu',
'Montenegro',
'Bahamas',
'Timor Leste',
'Eswatini',
'Kuwait',
'Fiji',
'New Caledonia',
'Hawaii',
];
csvFile.addEventListener('input', e => {
e.preventDefault();
const files = csvFile.files;
return new Promise(resolve => {
resolve(parseFiles(files, data));
return data;
})
.then(data => flatten(data))
.then(flattened => getLocations(flattened))
.then(locations => getPlaces(locations))
.then(places => mapOrders(places))
.then(regions => getShipped(regions))
.then(regionsShipped => addMarker(regionsShipped))
.then(regionsShipped => getListData(regionsShipped))
.then(listData => sumOrders(listData))
.then(totals => sumTotal(totals))
.then(sum => addSum(sum))
.then(regions => featureOrders(regions))
.catch(error => console.error(error));
});
instructions.addEventListener('click', function (e) {
e.preventDefault;
if (modal.className === 'hidden') {
modal.className = 'shown';
} else {
modal.className = 'hidden';
}
});
modal.addEventListener('click', function (e) {
e.preventDefault;
if (modal.className === 'shown') {
modal.className = 'hidden';
}
});
submit.addEventListener('click', function (e) {
e.preventDefault();
console.log('Form Submitted');
document.getElementById('top').style.display = 'none';
document.getElementById('mapid').style.height = '98vh';
const merged = new Promise(function (resolve, reject) {
resolve((flattened = data.flat()));
return flattened;
reject(console.error(error));
})
.then(flattened => getLocations(flattened))
.then(locations => getPlaces(locations))
.then(places => mapOrders(places))
.then(regions => getShipped(regions))
.then(regionsShipped => addMarker(regionsShipped))
.then(regionsShipped => getListData(regionsShipped))
.then(listData => sumOrders(listData))
.then(totals => sumTotal(totals))
.then(sum => addSum(sum))
.then(regions => featureOrders(regions))
// .then(L.geoJson(mapData, { style: style }).addTo(mymap))
.catch(error => console.error(error));
});
function readFile(file, arr) {
const reader = new FileReader();
reader.onload = function (event) {
event.preventDefault();
return new Promise(resolve => {
Papa.parse(file, {
header: true,
complete: results => {
resolve(results.data);
let _ = results.data.pop();
return arr.push(results.data);
},
});
});
};
reader.readAsText(file);
}
function parseFiles(arr, arr2) {
return new Promise(resolve => {
for (let i = 0; i < arr.length; i++) {
readFile(arr[i], arr2);
}
resolve(arr2);
return arr2;
});
}
function flatten(arr) {
flattened = arr.flat();
return flattened;
}
function sumTotal(arr) {
const sum = arr.reduce((partial_sum, a) => partial_sum + a, 0);
return sum;
}
function sumOrders(arr) {
const totals = [];
for (let i = 0; i < arr.length; i++) {
totals.push(arr[i][1]);
}
return totals;
}
function addSum(int) {
total.innerHTML += `Total Orders: ${int}`;
allTotal = int;
return regions;
}
function getLocations(orders) {
let data = orders;
let int = data.length - 2;
let item = data[int][''];
startDate = data[int]['Sale Date'];
endDate = data[0]['Sale Date'];
start = getDates(startDate);
end = getDates(endDate);
for (let i = 0; i < data.length; i++) {
const ship = data[i]['Ship Country'];
const country = countryCode(ship);
const state = acronymToFullName(data[i]['Ship State']);
let ste;
shipCountries.push(ship);
if (ship === 'United States') {
ste = 'US' + data[i]['Ship State'];
locations.push([`${country}`, `${ste}`]);
info.push([`${state}, ${country}`]);
} else if (ship === 'Canada') {
ste = 'CA' + data[i]['Ship State'];
locations.push([`${country}`, `${ste}`]);
info.push([`${state}, ${country}`]);
} else {
locations.push([`${country}`, `${country}`]);
info.push([`${country}`]);
}
}
return locations;
}
const getDates = function (str) {
let arr = str.split('/');
year = +arr[2];
month = monthFull[+arr[0] - 1];
return `${year}`;
};
const getPlaces = function (locations) {
for (let i = 0; i < locations.length; i++) {
const el = locations[i];
const loc = el[1];
if (loc !== undefined) {
places.push(loc);
}
}
return places;
};
const mapOrders = function (places) {
for (let i = 0; i < places.length; i++) {
const el = places[i];
const region = regions.find(region => region.code === el);
if (region != undefined) {
region.orders++;
} else {
console.log('error with mapOrders' + el);
}
}
return regions;
};
function getShipped(arr) {
regionsShipped.push(
regions.filter(obj => {
return obj.orders >= 1;
})
);
return regionsShipped;
}
function addMarker(arr) {
const item = arr[0];
for (let i = 0; i < item.length; i++) {
if (smallCountries.includes(item[i].name)) {
const lat = item[i].lat;
const lng = item[i].lng;
markers++;
L.marker([lat, lng], { icon: myIcon })
.addTo(mymap)
.bindPopup(`${item[i].name}, ${item[i].orders}`)
.on('mouseover', function (e) {
this.openPopup();
})
.on('mouseout', function (e) {
this.closePopup();
});
}
}
return arr;
}
function getListData(arr) {
let item = arr[0];
for (let i = 0; i < item.length; i++) {
listData.push([item[i].name, item[i].orders]);
}
return listData;
}
function getColor(d) {
return d > 750
? '#222434'
: d > 500
? '#252839'
: d > 250
? '#464C58'
: d > 100
? '#575E68'
: d > 50
? '#757D83'
: d > 10
? '#8D9498'
: d > 0
? '#A0A7AA'
: '#ffffff';
}
function style(feature) {
return {
fillColor: getColor(feature.properties.orders),
weight: 2,
opacity: 1,
color: '#F2B632',
dashArray: '3',
fillOpacity: 1,
};
}
const updateColor = function () {
mymap.removeLayer(L.geoJson);
var geojson = L.geoJson(mapData, {
style: style,
onEachFeature: onEachFeature,
}).addTo(mymap);
};
const featureOrders = function (arr) {
for (let i = 0; i < arr.length; i++) {
const e = arr[i];
if (e.orders > 0) {
if (e.code[0] === 'U' && e.code[1] === 'S') {
if (
e.code === 'USAE' ||
e.code === 'USAP' ||
e.code === 'USVI' ||
e.code === 'USUM'
) {
continue;
} else {
const features = statesData.features;
const a = features.filter(
feature => feature.properties.name === arr[i].name
)[0];
a.properties.orders = e.orders;
}
} else if (e.code[0] === 'C' && e.code[1] === 'A') {
const features = canadaprv.features;
const b = features.filter(obj => obj.properties.name === e.name)[0];
if (b != undefined) {
b.properties.orders = e.orders;
} else {
continue;
}
} else {
const c = countriesjson.features.filter(
feature => feature.properties.name === arr[i].name
)[0];
if (c != undefined) {
const x = e.orders;
c.properties.orders = x;
} else {
continue;
}
}
} else {
continue;
}
}
updateColor();
return arr;
};
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: zoomToFeature,
});
}
function highlightFeature(e) {
const layer = e.target;
layer.setStyle({
weight: 5,
color: '#252839',
dashArray: '',
fillOpacity: 1,
fillColor: '#F2B632',
});
layer.bringToFront();
control.update(layer.feature.properties);
}
function resetHighlight(e) {
geojson.resetStyle(e.target);
}
control.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'control');
this.update();
return this._div;
};
control.update = function (props) {
const contents = props
? `<b>${props.name}</b><br />${props.orders} orders shipped`
: 'Hover over a state';
if (allTotal !== undefined) {
this._div.innerHTML = `<h3>${allTotal} Orders Shipped</h3>${contents}`;
} else {
this._div.innerHTML = `<h3>Orders Shipped</h3>${contents}`;
}
};
control.addTo(mymap);
function zoomToFeature(e) {
mymap.fitBounds(e.target.getBounds());
}
const guide = L.control({ position: 'bottomright' });
guide.onAdd = function (map) {
const div = L.DomUtil.create('div', 'control guide');
const grades = [1, 10, 50, 100, 250, 500, 750];
const labels = [];
let from, to;
for (let i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
`<i style="background:${getColor(from + 1)}; color: ${getColor(
from + 1
)}">**</i> ${from}${to ? `–${to}` : '+'}`
);
}
div.innerHTML = labels.join('<br>');
return div;
};
guide.addTo(mymap);