-
Notifications
You must be signed in to change notification settings - Fork 35
/
33_map_points.html
148 lines (109 loc) · 3.28 KB
/
33_map_points.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Data points on the map</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: gray;
}
svg {
background-color: black;
}
</style>
</head>
<body>
<script type="text/javascript">
//Year of CO2 data to map
var year = "2010";
//Width and height
var w = 500;
var h = 300;
//Define map projection
var projection = d3.geo.mercator()
.center([ 0, 40 ])
.translate([ w/2, h/2 ])
.scale([ w/7 ]);
//Define path generator
var path = d3.geo.path()
.projection(projection);
//Define quantize scale to sort data values into buckets of color
//Colors by Cynthia Brewer (colorbrewer2.org), YlOrRd
var color = d3.scale.quantize()
.range([ "#ffffb2", "#fecc5c", "#fd8d3c", "#f03b20", "#bd0026" ]);
//Create SVG
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in CO2 data
d3.csv("data/co2/co2_emissions.csv", function(data) {
//Set input domain for color scale
color.domain([
d3.min(data, function(d) { return +d[year]; }),
d3.max(data, function(d) { return +d[year]; })
]);
//Load in GeoJSON data
d3.json("data/geo/mapshaper_output.json", function(json) {
//Merge the CO2 data and GeoJSON into a single array
//
//Loop through once for each CO2 data value
for (var i = 0; i < data.length; i++) {
//Grab country name
var dataCountryCode = data[i].countryCode;
//Grab data value, and convert from string to float
var dataValue = +data[i][year];
//Find the corresponding country inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
//We'll check the official ISO country code
var jsonCountryCode = json.features[j].properties.iso_a3;
if (dataCountryCode == jsonCountryCode) {
//Copy the data value into the GeoJSON
json.features[j].properties.co2 = dataValue;
//Stop looking through the JSON
break;
}
}
}
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d) {
//Get data value
var value = d.properties.co2;
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
//Load in cities data
d3.csv("data/cities/cities.csv", function(data) {
//Create a circle for each city
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
//[0] returns the first coordinate (x) of the projected value
return projection([d.longitude, d.latitude])[0];
})
.attr("cy", function(d) {
//[1] returns the second coordinate (y) of the projected value
return projection([d.longitude, d.latitude])[1];
})
.attr("r", 5)
.style("fill", "blue")
.style("opacity", 0.75);
});
}); //End d3.json()
}); //End d3.csv()
</script>
</body>
</html>