-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
317 lines (246 loc) · 10.1 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Life Expectancy</title>
<script src="https://d3js.org/d3.v6.js"></script>
<script src="d3-geo-projection.v2.min.js"></script>
<script src="d3-legend.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" media="screen"/>
</head>
<body>
<div id="main_header">
<h2>Worldwide trends in life expectancy 1960-2018</h2>
</div>
<div class="header">
Worldwide life expectancy from birth in 1960.
</div>
<div id="g1_header">
<svg id="g1">
</svg>
</div>
<script>
let margin = {top:10,right:20,bottom:10,left:60},
width = 1200,
height = 1000,
chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
let svg = d3.select("#g1")
.attr("width", width)
.attr("height", height)
Promise.all( [
d3.csv('simplified.csv', d3.autoType),
d3.json('world-110m.geojson')
])
.then(([pop, geomap]) => {
const y = "1960"
// Map and projection
let path = d3.geoPath();
let projection = d3.geoNaturalEarth2()
.scale(chartWidth / 1.4 / Math.PI)
.translate([chartWidth / 2, chartHeight / 2]);
let colorScale = d3.scaleThreshold()
.domain([45,50,55,60,65,70,75,80])
.range(d3.schemeBlues[9]);
svg.append("g")
.attr("id", "map")
.selectAll("path")
.data(geomap.features)
.enter().append("path")
.attr("stroke", "#fff")
.attr("d", path .projection(projection))
.attr("fill", d => {
d.final = getLifeExp(pop, d.id, y) || 0;
return colorScale(d.final);
})
.attr("exp", d => d.final)
.on("mouseover", function () {
let r = d3.select(this);
console.log(r.attr("exp"));});
let legend = d3.legendColor()
.labels(d3.legendHelpers.thresholdLabels)
.labelFormat(d3.format("2"))
.title("")
//.titleWidth(300)
//.orient("vertical")
.orient("horizontal")
.scale(colorScale)
.shapePadding(10)
.shapeWidth(80)
.shapeHeight(30)
svg.append("g")
.attr("transform", `translate(${100}, ${margin.top})`)
.attr("class", "le_legend")
.call(legend);
});
function getLifeExp(data, id, year) {
let val = data.filter( d => d.Code === id);
if(val.length == 0) return null;
return val[0][year];
}
</script>
<div class="transition">
In 1960, the global north is well ahead of the global south in terms of
life expectancy. But as we move to 2016, we can see many of those light
blue countries begin to darken as their life expectancy greatly increases.
</div>
<div class="header">
Worldwide life expectancy from birth in 2016.
</div>
<div id="g2_header">
<svg id="g2">
</svg>
</div>
<script>
Promise.all( [
d3.csv('countrydata.csv', d3.autoType),
d3.json('world-110m.geojson')
])
.then(([pop, geomap]) => {
let width = 1200,
height = 1000;
let svg = d3.select("#g2")
.attr("width", width)
.attr("height", height)
const group_key = 'Region2';
pop = pop.filter( d => {
return d[group_key ] != null && d[group_key] != "NA" })
pop.sort( (a,b) =>
d3.descending(a[group_key ],b[group_key ]) ||
d3.ascending(a['2016'], b['2016']));
// Map and projection
let path = d3.geoPath();
let projection = d3.geoNaturalEarth2()
.scale(chartWidth / 1.4 / Math.PI)
.translate([chartWidth / 2, chartHeight / 2]);
// Data and color scale
let colorScale = d3.scaleThreshold()
.domain([45,50,55,60,65,70,75,80])
.range(d3.schemeBlues[9]);
svg.append("g")
.attr("id", "map")
//.attr("transform",`translate(${margin.left},${0})`)
.selectAll("path")
.data(geomap.features)
.enter().append("path")
.attr("stroke", "#fff")
.attr("d", path .projection(projection))
.attr("fill", d => {
d.final = getLifeExp(pop, d.id, '2016') || 0;
return colorScale(d.final);
})
.attr("exp", d => d.final)
.on("mouseover", function () {
let r = d3.select(this);
console.log(r.attr("exp"));});
let legend = d3.legendColor()
.labels(d3.legendHelpers.thresholdLabels)
.labelFormat(d3.format("2"))
//.orient("vertical")
.orient("horizontal")
.scale(colorScale)
.shapePadding(10)
.shapeWidth(100)
.shapeHeight(30)
svg.append("g")
.attr("transform", `translate(${100}, ${margin.top})`)
.attr("class", "le_legend")
.call(legend);
});
function getLifeExp(data, id, year) {
let val = data.filter( d => d.Code === id);
if(val.length == 0) return null;
return val[0][year];
}
</script>
<div class="transition">
Great leaps were made in life expectancy since 1960. But, many regions have still not
caught up to the leaders. Is there a reason for this? Let's look at the mid 1980's to mid 2000's:
the height of the AIDS epidemic
around the world. Was the disease impactful enough to lower the growth rate of life expectancy?
</div>
<div class="header" id="hiv_header">
Percent change in life expectancy after outbreak of HIV epidemic.
</div>
<div id="g3_header">
<svg id="g3">
</svg>
</div>
<script>
Promise.all( [
d3.csv('simplified.csv', d3.autoType),
d3.json('world-110m.geojson')
])
.then(([pop, geomap]) => {
let margin = {top:20,right:20,bottom:10,left:60},
width = 1200,
height = 1000,
chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
let svg = d3.select("#g3")
.attr("width", width)
.attr("height", height)
// Map and projection
let path = d3.geoPath();
let projection = d3.geoNaturalEarth2()
.scale(chartWidth / 1.4 / Math.PI)
.translate([chartWidth / 2, chartHeight / 2]);
const y = 'aidsEffect';
let lifeExtent = d3.extent(pop, d=>d[y]);
let divScale = d3.scaleDiverging(
[lifeExtent[0], 0, lifeExtent[1]], d3.interpolateRdBu);
let colorScale = d3.scaleThreshold()
.domain([-75,-50, -25, 0, 25,50,75])
.range(d3.schemeBlues[9]);
svg.append("g")
.attr("id", "map")
.selectAll("path")
.data(geomap.features)
.enter().append("path")
.attr("stroke", "#fff")
.attr("d", path .projection(projection))
.attr("fill", d => {
d.final = getLifeExp(pop, d.id, y) || 0;
return divScale(d.final);
})
.attr("exp", d => d.final)
.on("mouseover", function () {
let r = d3.select(this);
console.log(r.attr("exp"));});
let legend = d3.legendColor()
//.labels(d3.legendHelpers.thresholdLabels)
.labelFormat(d3.format(".2f"))
//.orient("vertical")
.orient("horizontal")
.scale(divScale)
.cells(7)
.labels(["-100%", "-75%", "-25%", "0", "25%", "75%", "100%"])
.shapePadding(10)
.shapeWidth(100)
.shapeHeight(30)
svg.append("g")
.attr("transform", `translate(${100}, ${margin.top})`)
.attr("class", "div_legend")
.call(legend);
//svg.selectAll(".cell .label").text("100");
svg.selectAll(".cell").each( (d, i) => {
})
});
d3.select("body").selectAll("input")
function getLifeExp(data, id, year) {
let val = data.filter( d => d.Code === id);
if(val.length == 0) return null;
return val[0][year];
}
</script>
<div class="transition">
The onset of the AIDS epidemic was disastrous for many countries, particularly
those in Sub-Saharan Africa. Luckily this trend has started reversing since 2005,
so maybe the worst impact of the epidemic is over and these countries will look
forward to large increases in the coming years.
</div>
<div class="footer">
Data courtesy of <a href="https://data.worldbank.org/indicator/SP.DYN.LE00.IN?view=chart">
World Bank</a>.
</div>
</body>
</html>