-
Notifications
You must be signed in to change notification settings - Fork 1
/
step3.html
75 lines (63 loc) · 2.2 KB
/
step3.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
<html>
<head>
<script src="js/d3.v3.min.js"></script>
<script src="js/topojson.js"></script>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<h1>Step 3</h1>
<div id="container">
</div>
<script>
var width = 960, height = 500;
var projection = d3.geo.albersUsa().scale(1000).translate([width / 2, height / 2]);
var path = d3.geo.path() .projection(projection), svg;
svg = d3.select("#container").append("svg");
svg.attr("width", width) .attr("height", height);
fields = [ "Code", "1990-advanced", "1990-bachelors", "1990-hs",
"1990-not-hs", "2000-advanced", "2000-bachelors", "2000-hs",
"2006-advanced", "2006-bachelors", "2006-hs", "2007-advanced",
"2007-bachelors", "2007-hs", "2008-advanced", "2008-bachelors",
"2008-hs", "2009-advanced", "2009-bachelors", "2009-hs"];
function colorStates(field, scale) {
d3.selectAll(".states path")
.attr("fill", function(d) {
return d.data ? scale(d.data[field]) : "#333";
});
}
d3.json("data/us.json", function(us) {
d3.csv("data/education.csv", function(edu) {
var dataById = {}, features, field, scale;
field = "1990-hs";
edu.forEach(function(d) {
fields.forEach(function(field) {
d[field] = parseFloat(d[field]);
});
dataById[d.Code] = d;
});
features = topojson.feature(us, us.objects.states).features;
features.forEach(function(d) {
d.data = dataById[d.id];
});
scale = d3.scale.linear()
.domain(d3.extent(edu, function(d,i) { return d[field];}))
.range(["#f03b20", "#ffeda0"])
.interpolate(d3.interpolateHcl);
svg.append("g")
.classed("states", true)
.selectAll("path")
.data(features)
.enter()
.append("path")
.attr("class", function(d,i) { return d.id;})
.attr("d", path);
svg.insert("path")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "land")
.attr("d", path);
colorStates(field, scale);
});
});
</script>
</body>
</html>