-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
209 lines (172 loc) · 4.28 KB
/
index.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
'use strict';
var leaflet = require('leaflet');
var uxhr = require('uxhr');
var _ = require('lodash');
var topojson = require('topojson');
var Promise = require('es6-promise').Promise;
var legendTemplate = require('./templates/legend.handlebars');
var infoTemplate = require('./templates/info.handlebars');
var colors = ["#a50026", "#d73027", "#f46d43", "#fdae61", "#fee090", "#e0f3f8", "#abd9e9", "#74add1", "#4575b4", "#313695"];
var worldwideAvg = 0;
var map, geojson, legend, info;
function fetch(url) {
return new Promise(function(resolve, reject) {
uxhr(url, {}, {
error: function(respText) {
reject(new Error(respText));
},
success: function(respText) {
resolve(respText);
}
});
});
}
function parseJSON(data) {
return new Promise(function(resolve) {
resolve(JSON.parse(data));
});
}
function highlightFeatures(evt) {
var layer = evt.target;
layer.setStyle({
weight: 3,
color: '#666',
fillOpacity: 0.7
});
if (!leaflet.Browser.ie && !leaflet.Browser.opera) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
function resetHighlight(evt) {
geojson.resetStyle(evt.target);
info.update();
}
function getColor(percentage) {
return colors[~~((percentage-45)/(100-45) * colors.length)];
}
function style(feature) {
return {
fillColor: getColor(feature.properties.percentage),
weight: 1,
opacity: 1,
color: 'white',
fillOpacity: 0.7
};
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeatures,
mouseout: resetHighlight
});
}
function parseCountries(results) {
var data = results[0];
var iso_countries = results[1];
return new Promise(function(resolve) {
var countries = _(data).map(function(country) {
var iso = _.find(iso_countries, {cca2: country[0]});
if (!iso) {
return null;
}
return {
shortName: iso.cca3,
name: iso.name.common,
percentage: country[1]
};
}).filter(Boolean);
resolve(countries.value());
});
}
var sni = fetch('./sni.json').then(parseJSON);
var world = fetch('./countries.geo.json').then(parseJSON);
var countries = fetch('./countries.json').then(parseJSON);
var dataset = Promise.all([sni, countries]).then(parseCountries);
Promise.all([world, dataset]).then(function(results) {
var world = topojson.feature(results[0], results[0].objects.countries);
var countries = results[1];
world.features = _(world.features).map(function(features) {
var iso = _.find(countries, {shortName: features.properties.iso_a3 || features.properties.ISO_A3});
if (!iso) {
return null;
}
return _.extend({}, features, {
id: iso.name,
properties: {
percentage: iso.percentage,
name: iso.name
}
});
}).filter(Boolean).value();
geojson = leaflet.geoJson(world, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
});
var map = leaflet.map('map', {
minZoom: 2,
maxZoom: 16,
maxBounds: [
[-90, -180],
[90, 180]
]
}).setView([0, 0], 2);
window.map = map;
leaflet.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/{variant}/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles © Esri — Esri, DeLorme, NAVTEQ',
variant: 'Canvas/World_Light_Gray_Base'
}).addTo(map);
legend = leaflet.control({
position: 'bottomleft'
});
legend.onAdd = function() {
var div = leaflet.DomUtil.create('div', 'info legend');
var grade = 45;
var rows = _.map(colors, function(color) {
var row = {
color: color,
start: grade,
end: grade + 5
};
grade+=5;
return row;
});
div.innerHTML = legendTemplate({rows: rows});
return div;
};
legend.addTo(map);
info = leaflet.control({
position: 'bottomright'
});
info.onAdd = function() {
this._div = leaflet.DomUtil.create('div', 'info');
this.update();
return this._div;
};
info.update = function(props) {
this._div.innerHTML = infoTemplate({ props: props, worldwide: worldwideAvg });
};
info.addTo(map);
_.mixin({
mean: function(obj, key) {
return _.sum(obj, key) / _.size(obj);
},
sum: function(obj, key) {
var arr;
if (_.isArray(obj) && typeof obj[0] === 'number') {
arr = obj;
} else {
key = key || 'value';
arr = _.pluck(obj, key);
}
var val = 0, i;
for (i = 0; i < arr.length; i++) {
val += (arr[i]-0);
}
return val;
}
});
dataset.then(function(countries) {
worldwideAvg = _.mean(countries, 'percentage').toFixed(2);
info.update();
});