/
ch9-4.html
134 lines (116 loc) · 3.16 KB
/
ch9-4.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
<html>
<head>
<title>Nesting</title>
<meta name="description" content="Practical D3">
<meta name="author" content="Tarek Amr, @gr33ndata">
</head>
<body>
</body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
<script type="text/javascript">
var w = 900, h = 450;
var odi_url = "http://index.okfn.org/api/places.json";
var geo_url = "https://gist.githubusercontent.com/sindresorhus/1341699/raw/84704529d9ee4965df2cddc55e5f2bc3dc686950/countrycode-latlong-array.json";
var svg = d3.select('body').append('svg')
.attr('width', w)
.attr('height', h);
var colours = d3.scale.category10();
// Longitude goes from -180 to 180
var xScale = d3.scale.linear()
.domain([-180, 180])
.range([0, w]);
// Latitude goes from -90 to 90
var yScale = d3.scale.linear()
.domain([90, -90])
.range([0, h]);
// Circles radii are function of score
var rScale = d3.scale.linear()
.domain([0, 100])
.range([0, 40]);
queue()
.defer(d3.json, odi_url)
.defer(d3.json, geo_url)
.await(function(error, odi, geo) {
if (error) return console.warn(error);
console.log(odi, geo);
draw(odi, geo);
});
var draw = function(odi, geo){
// Add coordinates to Index data
odi.forEach(function(d, i){
try {
d.lat = geo[d.id][0];
d.lng = geo[d.id][1];
}
catch(err) {
d.lat = d.lng = null;
}
});
// Ignore countries we could not get coordinates for
// Also declutter the drawing by ignoring countries with low score
odi = odi.filter(function(d, i){
if (d.lng == null){
return false;
}
if (d.score < 20){
return false;
} else {
return true;
}
});
// Draw circle for each country
svg.selectAll("circle")
.data(odi)
.enter()
.append("circle")
.on('click', function(d, i){
console.log("" + d.id + " " + d.score)
})
.attr("cx", function(d,i){
return xScale(d.lng);
})
.attr("cy", function(d,i){
return yScale(d.lat);
})
.style("r", function(d,i){
return rScale(d.score);
})
.style("fill", function(d,i){
return colours(d.continent);
})
.style("opacity",function(d){
return 0.45;
});
svg.selectAll("text")
.data(odi)
.enter()
.append("text")
.text(function(d){
return d.id.toUpperCase();;
})
.attr("x", function(d,i){
return xScale(d.lng);
})
.attr("y", function(d,i){
return yScale(d.lat);
})
.style("text-anchor", "middle")
.attr('font-family', function(d){
return 'Verdana';
})
.attr('font-weight', function(d){
return 'bold';
})
.style("font-size", function(d,i){
return "8px";
})
.attr("fill", function(d,i){
return "#000";
})
.style("opacity",function(d){
return 0.8;
});
}
</script>
<html>