-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
296 lines (255 loc) · 9.38 KB
/
index.html
File metadata and controls
296 lines (255 loc) · 9.38 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<meta charset="utf-8">
<style>
/* css properties */
svg {
font: 10px sans-serif;
}
.caption {
font-weight: bold;
}
.key path {
display: none;
}
.key line {
stroke: #000;
shape-rendering: crispEdges;
}
.county-border {
fill: none;
stroke: #000;
stroke-opacity: .3;
}
.census-border {
fill: none;
stroke: #000;
stroke-opacity: .3;
}
.state-border {
fill: none;
stroke: #000;
stroke-opacity: .7;
}
</style>
<body>
<!-- Add title -->
<h1 style = "text-align:center">Minnesota Population Density, 2014</h1>
<!-- Add input buttons for user -->
<input id = "color" type = "button" value = "color" onclick = "changeColor();" />
<input id = "censustract" type = "button" value = "tracts" onclick = "changeTracts();"/>
<input id = "state" type = "button" value = "state" onclick = "changeStateBorder();" />
<!-- use these libraries for js -->
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
/* Initialization og variables */
//initialize height, width of svg
var width = 960,
height = 800;
//format ticks
var formatNumber = d3.format(",d");
//store how many times each button is pressed
var colorcount = 0; //store number of times user presses color button
var tractcount = 0; //store number of times user presses tract button
var statecount = 0; //store number of times user presses state button
var path = d3.geo.path()
.projection(null);
//define color scale, mapping values in certain ranges to corresponding color
var color = d3.scale.threshold()
.domain([1, 10, 50, 100, 500, 1000, 2000, 5000])
.range(["#fff7ec", "#fee8c8", "#fdd49e", "#fdbb84", "#fc8d59", "#ef6548", "#d7301f", "#b30000", "#7f0000"]);
//.range(["#19ff19", "#00e500", "#00cc00", "#00b200", "#009900", "#007f00", "#006600", "#004c00", "#003300"])
// define scale for legend
var x = d3.scale.linear()
.domain([0, 5100])
.range([0, 480]);
//make the legend
var xAxis = d3.svg.axis()
.scale(x) //scale using above scale
.orient("bottom") //orient ticks on bottom
.tickSize(13)
.tickValues(color.domain()) //get values for ticks
.tickFormat(function(d) { return d >= 100 ? formatNumber(d) : null; }); //remove tick value from legend if <100
//define svg element
var svg = d3.select("body").append("svg")
.attr("width", width) //set width and height using above values
.attr("height", height);
//defing grouping element
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(440,40)"); //position it
//adding in rectangles in legend
g.selectAll("rect")
//bind data of color scale from above
.data(color.range().map(function(d, i) {
return {
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
z: d
};
}))
//add placeholders and append there
.enter().append("rect")
//set height of all rect
.attr("height", 8)
//set up each x position and width
.attr("x", function(d) { return d.x0; })
.attr("width", function(d) { return d.x1 - d.x0; })
//fill with corresponding color
.style("fill", function(d) { return d.z; });
//adding label to legend
g.call(xAxis).append("text")
.attr("class", "caption")
.attr("y", -6)
.text("Population per square mile");
//-----------------------------------------------------------------------------------------------------------
/* Reading in data from JSON file */
d3.json("mn.json", function(error, mn) {
if (error) throw error;
console.log(mn.objects) //debug
//get census tracts from input file
var tracts = topojson.feature(mn, mn.objects["cb_2015_27_tract_500k"]);
// Clip tracts to land.
svg.append("defs").append("clipPath")
.attr("id", "clip-land")
.append("path")
.datum(topojson.feature(mn, mn.objects["MN-COUNTY"]))
.attr("d", path);
// Group tracts by color for faster rendering.
svg.append("g")
.attr("class", "tract")
.attr("clip-path", "url(#clip-land)")
.selectAll("path")
.data(d3.nest()
.key(function(d) { return color(d.properties.population / d.properties.area * 2.58999e6); })
.entries(tracts.features.filter(function(d) { return d.properties.area; })))
.enter().append("path")
.style("fill", function(d) { return d.key; })
.attr("d", function(d) { return path({type: "FeatureCollection", features: d.values}); });
// Draw county borders.
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["MN-COUNTY"], function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
//Draw census tract borders
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["cb_2015_27_tract_500k"], function(a, b) { return a !== b; }))
.attr("class", "census-border")
.attr("d", path);
//draw state border
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["MN-STATE"], function(a, b) { return a == b; }))
.attr("class", "state-border")
.attr("d", path);
//-----------------------------------------------------------------------------------------------------------
/* functions that control changes to map when a button is clicked */
//function that changes the color scheme when called
changeColor = function(){
//use colorcount to check which color to use
//increment counter to check what color scheme to use on next click
colorcount++;
//if odd number of clicks, change to green scheme
if(colorcount%2 == 1){
color.range(["#19ff19", "#00e500", "#00cc00", "#00b200", "#009900", "#007f00", "#006600", "#004c00", "#003300"])
}
//otherwise set to default default scheme
else{
color.range(["#fff7ec", "#fee8c8", "#fdd49e", "#fdbb84", "#fc8d59", "#ef6548", "#d7301f", "#b30000", "#7f0000"]);
}
//update the color of the legend
g.selectAll("rect")
.data(color.range().map(function(d, i) {
return {
x0: i ? x(color.domain()[i - 1]) : x.range()[0],
x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],
z: d
};
}))
.style("fill", function(d) { return d.z; });
//set color of census tracts
svg.selectAll("tract")
.data(d3.nest()
.key(function(d) { return color(d.properties.population / d.properties.area * 2.58999e6); })
.entries(tracts.features.filter(function(d) { return d.properties.area; })))
.enter().append("path")
.style("fill", function(d) { return d.key; })
.attr("d", function(d) { return path({type: "FeatureCollection", features: d.values}); });
// redraw county borders.
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["MN-COUNTY"], function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
//if color is changed from default, redraw the following
if(colorcount%2==1){
//redraw state border
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["MN-STATE"], function(a, b) { return a == b; }))
.attr("class", "state-border")
.attr("d", path);
}
//Draw census tract borders
svg.append("path")
.datum(topojson.mesh(mn, mn.objects["cb_2015_27_tract_500k"], function(a, b) { return a !== b; }))
.attr("class", "census-border")
.attr("d", path);
//if user wanted tracts to be hidden
if(tractcount%2 == 1){
svg.selectAll(".census-border")
.style("stroke-opacity", 0)
}
//show tracts
else{
svg.selectAll(".census-border")
.style("stroke-opacity", 0.3)
}
//if user wanted to hide state border
if(statecount%2 == 1){
svg.selectAll(".state-border")
.style("stroke-opacity", 0)
}
//show state border
else{
svg.selectAll(".state-border")
.style("stroke-opacity", 0.7)
}
}
//function that hides/shows tracts when called
changeTracts = function(){
//increment tractcount for next
tractcount++;
//if odd count, hide tracts
if(tractcount%2 == 1){
svg.selectAll(".census-border")
.style("stroke-opacity", 0)
}
//otherwise, display tracts
else{
svg.selectAll(".census-border")
.style("stroke-opacity", 0.3)
}
}
//function that hides/displays state border when called
changeStateBorder = function(){
//increment number of times button was pressed
statecount++;
//if odd count, hide border
if(statecount%2 == 1){
svg.selectAll(".state-border")
.style("stroke-opacity", 0)
}
//otherwise, display it
else{
svg.selectAll(".state-border")
.style("stroke-opacity", 0.7)
}
}
});
//proper display on github
d3.select(self.frameElement).style("height", height + "px");
</script>
<!-- Additional Information to display at bottom -->
<h4>Christopher Bui</h4>
<h4>Instructor: Suresh Lodha</h4>
<h4>CMPS 165: Data Programming for Visualization</h4>
<h4>Fall 2016</h4>
</body>