-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_map.html
128 lines (95 loc) · 3.37 KB
/
index_map.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Project 1: 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"/>
<!-- <style>
.xAxis .tick text {
font-size: 28px;
text-anchor: middle;
}
</style>-->
</head>
<body>
<div id="main_header">
<h2> Life Expectancy 2016</h2>
</div>
<div class="header" id="g1_header">
<svg id="g1">
</svg>
</div>
<script>
let margin = {top:20,right:20,bottom:120,left:60},
width = 1200,
height = 1200,
chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
let svg = d3.select("#g1")
.attr("width", width)
.attr("height", height)
let annotations = svg.append("g").attr("id","annotations");
let chartArea = svg.append("g").attr("id","points")
.attr("transform",`translate(${margin.left},${margin.top})`);
Promise.all( [
d3.csv('countrydata.csv', d3.autoType),
d3.json('world-110m.geojson')
])
.then(([pop, geomap]) => {
const group_key = 'Region2';
popa = 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"))
.title("Life Expectancy")
.titleWidth(300)
//.orient("vertical")
.orient("horizontal")
.scale(colorScale)
.shapePadding(10)
.shapeWidth(100)
.shapeHeight(30)
svg.append("g")
.attr("transform", `translate(${170}, ${margin.top})`)
.call(legend);
// const color = d3.scaleQuantile([1, 10], d3.schemeBlues[9])
});
function getLifeExp(data, id, year) {
let val = data.filter( d => d.Code === id);
if(val.length == 0) return null;
return val[0][year];
}
</script>
</body>
</html>