-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathus_states_data_leaflet.html
More file actions
212 lines (171 loc) · 5.76 KB
/
us_states_data_leaflet.html
File metadata and controls
212 lines (171 loc) · 5.76 KB
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
<!DOCTYPE html>
<! A mod of Michelle Chandra's block: http://bl.ocks.org/michellechandra/0b2ce4923dc9b5809922 -->
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
@import url(//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.css);
/* On mouse hover, lighten state color */
path {
stroke: gray;
stroke-width: 1;
fill-opacity: .7;
}
circle {
fill: orange;
fill-opacity: .8;
}
p.intro {
width: 500px;
}
/* Style for Custom Tooltip */
div.tooltip {
position: absolute;
text-align: center;
min-width: 100px;
height: 30px;
padding: 2px;
font: 12px sans-serif;
background: white;
border: 1px orange solid;
border-radius: 5px;
pointer-events: none;
}
.tooltip p {
margin: 5px;
}
/* Legend Font Style */
body {
font: 11px sans-serif;
}
#map {
width: 960px;
height: 500px;
/* Legend Position Style */
.legend {
position:absolute;
left:800px;
top:350px;
}
</style>
</head>
<body>
<h2>A Leaflet Map Showing States Michelle Visited and Cities She Lived In</h2>
<p class="intro">A modified version of the <a href="http://bl.ocks.org/michellechandra/0b2ce4923dc9b5809922">block by Michelle Chandra</a> (data mod'd too).
This example shows joining data to a geojson file from external files, using queue and d3.legend. Also plots on top of Leaflet, based on <a href="https://bost.ocks.org/mike/leaflet/">example from Mike B.</a></p>
<div id="map"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/queue.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.js"></script>
<script>
var map = new L.Map("map", {center: [37.8, -96.9], zoom: 4})
.addLayer(new L.TileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"));
var svg = d3.select(map.getPanes().overlayPane).append("svg"),
g = svg.append("g").attr("class", "leaflet-zoom-hide");
// Define linear scale for output
var stateColor = d3.scale.linear()
.range(["white", "pink"]);
// Append Div for tooltip to SVG
var div = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("display", "none");
queue()
.defer(d3.json, "data/geojson/us-states.json")
.defer(d3.csv, "data/states-visited.csv")
.defer(d3.csv, "data/cities-lived.csv")
.await(ready);
function ready(error, json, states, cities) {
var transform = d3.geo.transform({point: projectPoint});
var path = d3.geo.path().projection(transform);
stateColor.domain(d3.extent(states,function(s) { return s.visits;})); // setting the range of the input data
// Loop through each state data value in the .csv file
states.forEach(function(state) {
// Grab State Name
var dataState = state.statename; // name
// Grab data value
var dataValue = +state.visits; // number
// Find the corresponding state inside the GeoJSON
json.features.forEach(function(j) {
var jsonState = j.properties.name;
if (dataState == jsonState) { // assumes the names will match...
// Copy the data value into the JSON
j.properties.visited = dataValue;
// Stop looking through the JSON
}
});
}); // ends data merge
// Bind the data to the SVG and create one path per GeoJSON feature
var feature = g.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d) {
// Get data value for visited
var value = d.properties.visited;
return stateColor(value);
});
var circles = g.selectAll("circle")
.data(cities)
.enter()
.append("circle")
.attr("cx", function(d) {
return getPoint(d.lon, d.lat).x;
})
.attr("cy", function(d) {
return getPoint(d.lon, +d.lat).y;
})
.attr("r", function(d) {
return Math.sqrt(d.years) * 4;
})
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("display", null);
div.html("<p>Lived in " + d.name + " for " + d.years + " year(s).</p>")
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// fade out tooltip on mouse out
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("display", "none");
});
map.on("viewreset", reset);
reset();
// Reposition the SVG to cover the features.
function reset() {
var bounds = path.bounds(json),
topLeft = bounds[0],
bottomRight = bounds[1];
svg.attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");
feature.attr("d", path);
circles.transition()
.attr("cx", function(d) {
return getPoint(d.lon, d.lat).x;
})
.attr("cy", function(d) {
return getPoint(d.lon, +d.lat).y;
});
}
function getPoint(x, y) {
// d3 uses lon, lat, leafleft uses lat,lon.
//console.log(map.latLngToLayerPoint(new L.LatLng(y,x)));
// this returns an object with .x and .y attributes
return map.latLngToLayerPoint(new L.LatLng(y,x));
}
// Use Leaflet to implement a D3 geometric transformation.
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
} // end ready function
</script>
</body>
</html>