-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathafrica_map3.html
More file actions
135 lines (100 loc) · 3.03 KB
/
africa_map3.html
File metadata and controls
135 lines (100 loc) · 3.03 KB
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
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
body {
font-family: Helvetica, sans-serif;
padding: 40px;
}
.countries {
stroke: #fff;
stroke-width:2px;
}
.legendLinear text {
font-size: 9px;
}
</style>
<body>
<h2>Current Malaria Rates in Africa</h2>
<p>Source: <a href="http://data.unicef.org/child-health/malaria.html">UNICEF</a>. Data shown is most recent data for countries with data available.</p>
<div id="vis"></div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/1.9.0/d3-legend.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 1000,
height = 600;
var svg = d3.select('#vis').append('svg')
.attr('width', width)
.attr('height', height);
var projection = d3.geo.mercator()
.scale(400) // mess with this if you want
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var colorScale = d3.scale.linear().range(["#fee0d2", "#de2d26"]).interpolate(d3.interpolateLab);
var countryById = d3.map();
// we use queue because we have 2 data files to load.
queue()
.defer(d3.json, "data/topojson/africa.topojson")
.defer(d3.csv, "data/Malaria_most_recent_totals.csv", typeAndSet) // process
.await(loaded);
function typeAndSet(d) {
d.Total = +d.Total;
countryById.set(d.ISO, d);
return d;
}
function getColor(d) {
var dataRow = countryById.get(d.properties.iso_a3);
if (dataRow) {
console.log(dataRow);
return colorScale(dataRow.Total);
} else {
console.log("no dataRow", d);
return "#ccc";
}
}
function getText(d) {
var dataRow = countryById.get(d.properties.iso_a3);
if (dataRow) {
console.log(dataRow);
return dataRow.Country + ":" + dataRow.Total;
} else {
console.log("no dataRow", d);
return d.properties.name + ": No data.";
}
}
function loaded(error, africa, malaria) {
console.log(africa);
console.log(malaria);
colorScale.domain(d3.extent(malaria, function(d) {return d.Total;}));
var countries = topojson.feature(africa, africa.objects.collection).features;
svg.selectAll('path.countries')
.data(countries)
.enter()
.append('path')
.attr('class', 'countries')
.attr('d', path)
.attr('fill', function(d,i) {
console.log(d.properties.name);
return getColor(d);
})
.append("title")
.text(function(d) {
return getText(d);
});
// The d3-legend component is called here:
var linear = colorScale;
svg.append("g")
.attr("class", "legendLinear")
.attr("transform", "translate(20,20)");
var legendLinear = d3.legend.color()
.shapeWidth(30)
.orient("horizontal")
.scale(linear);
svg.select(".legendLinear")
.call(legendLinear);
}
</script>
</body>
</html>