-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
232 lines (217 loc) · 9.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
<!DOCTYPE html>
<html>
<head>
<title>Happy Planet Index</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="../d3.min.js"></script>
<script type="text/javascript" src="../d3.geo.min.js"></script>
<script type="text/javascript" src="chernoff.js"></script>
<script type="text/javascript" src="../jquery-1.6.2.min.js"></script>
<style type="text/css">
body {
background-color: #faf5df;
font: 12px Verdana, Palatino, Helvetica, sans;
}
text {
}
.chernoff:hover > * {
stroke-width: 4;
stroke: #2219b2;
}
path {
stroke: #000;
}
path.overlay {
stroke: none;
fill: #000;
fill-opacity: 0;
}
path.overlay:hover {
stroke-width: 4;
stroke: #2219b2;
}
.active > * {
stroke-width: 4;
stroke: #2219b2;
}
path.active {
stroke-width: 4;
stroke: #2219b2;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="text"
style="position: absolute; left: 150px; top: 150px; width: 200px;">
<h1>Happy Planet Index</h1>
The colour of the faces and countries shows the
<a href="http://www.happyplanetindex.org/">Happy Planet Index</a>
value. This is also reflected in the shape of the mouths. The slant of
the brows reflects the number of Happy Life years. The shape of
the face is determined by the GDP per capita. The Human Development
Index is shown in the shape of the hair.
</div>
<div style="position: absolute; bottom: 10px; left: 10px;">
Data from
<a href="http://www.happyplanetindex.org/">Happy Planet Index</a>.
</div>
<div style="position: absolute; bottom: 10px; right: 10px;">
Lars Kotthoff 2012
</div>
<script type="text/javascript">
(function () {
var proj = d3.geo.equirectangular().scale(1).translate([0,0]),
path = d3.geo.path().projection(proj),
transdur = 300,
margin = 25,
width = window.innerWidth - margin,
height = window.innerHeight - 2*margin,
svg = d3.select("#map")
.append("svg:svg")
.attr("height", height)
.attr("width", width),
map = svg.append("svg:g")
.attr("transform", "translate(100,100)")
.append("svg:g"),
rb = svg.append("svg:g");
d3.json("hpi.geojson", function(json) {
var bounds0 = d3.geo.bounds(json),
bounds = bounds0.map(proj),
xscale = (width-200)/Math.abs(bounds[1][0] - bounds[0][0]),
yscale = (height-200)/Math.abs(bounds[1][1] - bounds[0][1]),
scale = Math.min(xscale, yscale);
if(xscale > yscale) {
var d = xscale * Math.abs(bounds[1][0] - bounds[0][0]) -
yscale * Math.abs(bounds[1][0] - bounds[0][0]);
map.attr("transform", "translate(" + d + ", 0)");
} else {
var d = yscale * Math.abs(bounds[1][1] - bounds[0][1]) -
xscale * Math.abs(bounds[1][1] - bounds[0][1]);
map.attr("transform", "translate(0, " + d/2 + ")");
}
proj.scale(scale);
proj.translate(proj([-bounds0[0][0], -bounds0[1][1]]));
var ncountries = json.features.length,
fx = Math.round(ncountries/4 * width/height)-2,
fy = Math.round(ncountries/4 * height/width);
function fpos(idx, off) {
var dx = dy = 0;
if(idx < fx || (idx >= fx + fy && idx < 2*fx+fy)) {
dx = idx%fx * (width/fx);
} else if(idx >= 2*fx+fy) {
dx = 0;
} else {
dx = width - 100;
};
if(idx < fx) {
dy = 0;
} else if(idx >= fx + fy && idx < 2*fx+fy) {
dy = height - 100;
} else if(idx >= fx || (idx >= 2*fx+fy)) {
dy = 100 + idx%fy * ((height-200)/fy);
};
return "translate(" + (dx-off) + "," + (dy-off) + ")";
}
function displayText(f) {
$("#text").empty();
$("#text").append("<h1>" + f.p.name + "</h1>")
.append("<dl>")
.append("<dt><strong>Happy Planet Index</strong>")
.append("<dd>" + f.p.hpi)
.append("<dt><strong>Happy Life years</strong>")
.append("<dd>" + f.p.hly)
.append("<dt><strong>GDP per capita (US-$ PPP)</strong>")
.append("<dd>" + f.p.gdppc)
.append("<dt><strong>Human Development Index</strong>")
.append("<dd>" + f.p.hdi)
.append("</dl>");
}
function resetText() {
$("#text").empty();
$("#text").append("<h1>Happy Planet Index</h1>")
.append("The colour of the faces and countries shows the")
.append(" <a href=\"http://www.happyplanetindex.org/\">Happy Planet Index</a>")
.append(" value. This is also reflected in the shape of the mouths. The slant of")
.append(" the brows reflects the number of Happy Life years. The shape of")
.append(" the face is determined by the GDP per capita. The Human Development")
.append(" Index is shown in the shape of the hair.");
}
resetText();
var hpiex = d3.extent($.map(json.features, function(f) {
return f.p.hpi;
})),
hpicscale = d3.scale.linear().domain(hpiex)
.range(["#fff140", "#a66200"]),
hpiscale = d3.scale.linear().domain(hpiex).range([-1, 1]),
hlyex = d3.extent($.map(json.features, function(f) {
return f.p.hly;
})),
hlyscale = d3.scale.linear().domain(hlyex).range([-1, 1]),
hdiex = d3.extent($.map(json.features, function(f) {
return f.p.hdi;
})),
hdiscale = d3.scale.linear().domain(hdiex).range([-1, 1]),
gdpex = d3.extent($.map(json.features, function(f) {
return f.p.gdppc;
})),
gdpscale = d3.scale.linear().domain(gdpex).range([1, 0]),
fWithData = $.grep(json.features, function(f) {
return f.p.hpi != undefined;
});
c = d3.chernoff()
.face(function(d) { return gdpscale(d.p.gdppc); })
.hair(function(d) { return hdiscale(d.p.hdi); })
.mouth(function(d) { return hpiscale(d.p.hpi); })
.brow(function(d) { return hlyscale(d.p.hly); });
map.selectAll("path")
.data(json.features)
.enter().append("svg:path")
.attr("d", path)
.attr("fill", function(d) { return hpicscale(d.p.hpi); });
map.selectAll("path.overlay")
.data(fWithData)
.enter().append("svg:path")
.attr("class", "overlay")
.attr("d", path)
.on("mouseover", function(f, i) {
rb.selectAll("g.chernoff").data([f], function(e) {
return e.p.name;
}).classed("active", true)
.transition().duration(transdur)
.attr("transform", fpos(i, 5) + "scale(.65)");
displayText(f);
})
.on("mouseout", function(f, i) {
rb.selectAll("g.chernoff").data([f], function(e) {
return e.p.name;
}).classed("active", false)
.transition().duration(transdur)
.attr("transform", fpos(i, 0) + "scale(.6)");
resetText();
});
rb.selectAll("g.chernoff").data(fWithData).enter()
.append("svg:g")
.attr("class", "chernoff")
.attr("transform", function(d, i) {
return fpos(i, 0) + "scale(.6)";
})
.attr("fill", function(d) { return hpicscale(d.p.hpi); })
.on("mouseover", function(f) {
map.selectAll("path.overlay").data([f], function(e) {
return e.p.name;
}).classed("active", true);
displayText(f);
})
.on("mouseout", function(f) {
map.selectAll("path.overlay").data([f], function(e) {
return e.p.name;
}).classed("active", false);
resetText();
})
.call(c);
});
})();
</script>
</body>
</html>