-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths.html
103 lines (91 loc) · 3.01 KB
/
paths.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Paths using GeoData</title>
<script type="text/javascript" src="../d3/d3.js"></script>
<style type="text/css">
</style>
</head>
</head>
<body>
<script type="text/javascript">
var w = 500;
var h = 300;
//Defining the map projection
// A projection is an algorithm of compromise; it is the method by which 3D space is “projected” onto a 2D plane.
var projection = d3.geo.albersUsa()
.translate([w/2, h/2])
.scale([500]);
//This takes translates GeoJSON coordinates into SVG path codes
var path = d3.geo.path()
.projection(projection);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("widt", w)
.attr("height", h);
//Define quantize scale to sort data values into buckets of color
// a scale that can take data values as input, and will return colors.
var color = d3.scale.quantize()
.range(["rgb(237,248,233)", "rgb(186,228,179)",
"rgb(116,196,118)", "rgb(49,163,84)","rgb(0,109,44)"]);
d3.csv("us-agricultural-productivity.csv", function(data) {
color.domain([
d3.min(data, function(d) { return d.value; }),
d3.max(data, function(d) { return d.value; })
]);
d3.json("us-states.json", function(json) {
//Merg the agricultural data with the GeoJSON data
//Loop through one for each data value
for (var i=0; i < data.length; i++) {
//grab the state names
var dataState = data[i].state;
//Grab the data value, and convert from string to float
var dataValue = parseFloat(data[i].value);
//Find the corresponding state inside the GeoJSON
for (var j=0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.name;
if (dataState == jsonState) {
//Copy the data value into the JSON
json.features[j].properties.value = dataValue;
//Stops looking for the JSON once its set
break;
}
}
}
//Binds data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("fill", function(d) {
//Get the data value
var value = d.properties.value;
if (value) {
return color(value);
} else {
return "#ccc";
}
});
});
d3.csv("us-cities.csv", function(data) {
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.attr("r", 5)
.style("fill", "yellow")
.style("opacity", 0.75);
});
});
</script>
</body>
</html>