-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathafrica_map_clicker.html
More file actions
212 lines (169 loc) · 4.84 KB
/
africa_map_clicker.html
File metadata and controls
212 lines (169 loc) · 4.84 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
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
<!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;
}
#info {
position: absolute;
top: 300px;
left: 50px;
width: 150px;
border: 1px gray dotted;
margin: 15px;
text-align: center;
}
p.country {
font-size: 1.2em;
font-weight: bolder;
color: "steelblue";
}
p.rate {
font-size: 1em;
font-weight: bolder;
fcolor: "steelblue";
}
</style>
<body>
<h2>Fertility Rates in Africa</h2>
<p>Source: World Bank (excerpted)</p>
<div id="vis"></div>
<div id="info">
<p class="country">Click a country!</p>
<p class="rate"></p>
</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 dateFormat = d3.time.format("%Y");
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();
var data = [];
var filterValue = "2013";
// we use queue because we have 2 data files to load.
queue()
.defer(d3.json, "data/topojson/africa.topojson")
.defer(d3.csv, "data/World_Development_Indicators_Metadata_Countries.csv", makeLookup)
.defer(d3.csv, "data/fertility_long_africa.csv", typeFertility) // process
.await(loaded);
function typeFertility(d) {
d.fertility = +d.Fertility;
d.year = dateFormat.parse(d.Year);
d.country = d.Country;
return d;
}
function makeLookup(d) {
countryById.set(d.ISO3, d);
return d;
}
function getData(d) {
var dataRow = countryById.get(d.properties.adm0_a3_is); // best match
var dataVal = null;
// must be a more elegant way to do this with all the checks, but i"m tired
if (dataRow) {
dataVal = data.get(dataRow.ShortName);
}
if (dataVal) {
console.log("dataVal shortname", dataVal[0].Country);
dataVal = dataVal.filter(function (d) {
return d.Year == filterValue;
});
}
if (dataVal) {
dataVal = dataVal[0];
}
return dataVal;
}
function getColor(d) {
var dataVal = getData(d);
if (dataVal) {
return colorScale(dataVal.fertility);
}
// if we fall through, i.e., no match
//console.log("no dataRow", d);
return "#ccc";
}
function loaded(error, africa, codes, fertility) {
//console.log(africa);
//console.log(codes);
//console.log(fertility);
// this construction creates a hash lookup for the country to values
data = d3.nest()
.key(function(d) {
return d.Country;
})
.map(fertility, d3.map);
//console.log("map result", data.get("Afghanistan"));
colorScale.domain(d3.extent(fertility, function(d) {
if (d.Year == filterValue) return d.fertility;}));
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) {
return getColor(d);
})
.on("click", showInfo)
.on("mouseover", function(d) {
d3.select(this).moveToFront();
d3.select(this).style("stroke", "black");
})
.on("mouseout", function(d) {
d3.select(this).style("stroke", "#fff");
})
.append("title")
.text("Click me!");
// 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);
}
function showInfo(d) {
//console.log(getText(d));
var dataVal = getData(d);
if (dataVal) {
d3.select("#info p.country").html(dataVal.country);
d3.select("#info p.rate").html(dataVal.fertility);
} else {
d3.select("#info p.country").html(d.properties.name);
d3.select("#info p.rate").html("no data available.");
}
}
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
</script>
</body>
</html>