-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_income.html
308 lines (255 loc) · 8.14 KB
/
map_income.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
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
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE HTML>
<!--
Alpha by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>Pet & Income - Alpha by HTML5 UP</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assets/css/main.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<body>
<div id="page-wrapper"> <!-- Header -->
<header id="header">
<h1><a href="index.html">Cat & Dog</a></h1>
<nav id="nav">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="#" class="icon fa-angle-down">Menu</a>
<ul>
<li><a href="cat.html">Cat by States</a></li>
<li><a href="dog.html">Dog by States</a></li>
<li><a href="map.html">Pet on Map</a></li>
<li>
<a href="#">More Maps</a>
<ul>
<li><a href="map_election.html">Pet & Election</a></li>
<li><a href="map_income.html">Pet & Income</a></li>
<li><a href="map_ob.html">Pet & Obesity</a></li>
</ul>
</li>
<li><a href="about.html">About</a></li>
</ul>
</li>
<li></li>
</ul>
</nav>
</header>
<!-- Main -->
<section id="main" class="container">
<header>
<h2>Pet Preference and Household Income</h2>
</header>
<div class="box">
<h3>Pet Preference</h3>
<center><svg width="960" height="600" id="statesvg"></svg> </center>
<h3>Median Household Income</h3>
<center><svg width="960" height="600" id="statesvg2"></svg> </center>
<p>Median household income of each state is mapped on US state map. Red represents low income, blue represents high income, and grey represents the national average household income. </p>
<p> </p>
<center>
<h3>More maps</h3>
<p> </p>
</center>
<ul class="actions"><center>
<li><a href="map_election.html" class="button special">Pet and Election</a></li>
<li><a href="map_ob.html" class="button special">Pet and Obesity</a></li>
</center></ul>
<ul class="actions"><center>
<li><a href="index.html" class="button">Home</a></li>
</center></ul>
</div>
</section>
<!-- Footer -->
<footer id="footer">
<ul class="copyright">
<li>© Jiehuan Huang. All rights reserved.</li>
<li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.dropotron.min.js"></script>
<script src="assets/js/jquery.scrollgress.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
<meta charset="utf-8">
<style>
.state{
fill: none;
stroke: #a9a9a9;
stroke-width: 1;
}
.state:hover{
fill-opacity:0.5;
}
#tooltip h4{
margin:0;
font-size:14px;
}
#tooltip{
position: absolute;
text-align: center;
margin: 10px;
font: 12px sans-serif;
border: 1px;
border-radius: 2px;
pointer-events: none;
background:rgba(0,0,0,0.9);
border:1px solid grey;
border-radius:5px;
font-size:12px;
width:auto;
padding:4px;
color:white;
opacity:0;
}
#tooltip table{
table-layout:auto;
}
#tooltip tr td{
padding:0;
margin:0;
}
#tooltip tr td:nth-child(1){
width:auto;
}
#tooltip tr td:nth-child(2){
text-align:center;
}
</style>
<body>
<div id="tooltip"></div>
<script src="uStates.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function tooltip3Html(n, d){
return "<tr><td>"+n+"</td></tr>" + "<br/>"+
"<tr><td>Cat Owner: </td><td>"+(d.cat)+"%</td></tr>"+ "<br/>" +
"<tr><td>Dog Owner: </td><td>"+(d.dog)+"%</td></tr>"+ "<br/>" +
"<tr><td>Preference of Dog: </td><td>"+(d.ratio)+"</td></tr>";
}
function tooltip2Html(n, d){
return "<tr><td>"+n+"</td></tr>" + "<br/>"+
"<tr><td>Household: </td><td>"+(d.household)+"K</td></tr>"+"<br/>" +
"<tr><td>Cat/Dog Owner: </td><td>"+(d.pet)+"%</td></tr>"+"<br/>" +
"<tr><td>Cat Population: </td><td>"+(d.cat_pop)+"K</td></tr>"+"<br/>" +
"<tr><td>Dog Population: </td><td>"+(d.dog_pop)+"K</td></tr>"+"<br/>" +
"<tr><td>Cat Owner: </td><td>"+(d.cat)+"%</td></tr>"+"<br/>" +
"<tr><td>Dog Owner: </td><td>"+(d.dog)+"%</td></tr>"+"<br/>" +
"<tr><td>Avg. cats for every cat owner: </td><td>"+(d.cat_mean)+"</td></tr>"+"<br/>" +
"<tr><td>Avg. dogs for every dog owner: </td><td>"+(d.dog_mean)+"</td></tr>"+"<br/>" +
"<tr><td>Preference of Dog: </td><td>"+(d.ratio)+"</td></tr>";
}
d3.csv("data.csv", function(d) {
return d;
},
function(error, dataset) {
if (error) throw error;
var data ={};
["FL", "SC", "GA", "AL", "NC", "TN", "RI", "CT", "MA",
"ME", "NH", "VT", "NY", "NJ", "PA", "DE", "MD", "WV", "KY", "OH",
"MI", "WY", "MT", "ID", "WA", "DC", "TX", "CA", "AZ", "NV", "UT",
"CO", "NM", "OR", "ND", "SD", "NE", "IA", "MS", "IN", "IL", "MN",
"WI", "MO", "AR", "OK", "KS", "LA", "VA"]
.forEach(function(d){
var color = d3.scaleLinear()
.domain([0.692, 1.187, 1.609])
.range(["blue", "lightgrey", "red"]);
for (var i = 0; i < 49; i++)
{
if (d == dataset[i].id)
data[d]={cat: dataset[i].cat, dog: dataset[i].dog, household: dataset[i].household, pet: dataset[i].pet, cat_mean: dataset[i].cat_mean, dog_mean: dataset[i].dog_mean, cat_pop: dataset[i].cat_pop, dog_pop: dataset[i].dog_pop,
ratio: Math.round(dataset[i].ratio *100)/100, color: color(dataset[i].ratio)};
}
});
uStates.draw("#statesvg", data, tooltip3Html, tooltip2Html);
d3.select(self.frameElement).style("height", "600px");
});
</script>
<script>
var svg = d3.select("#statesvg");
legend = svg.append("rect")
.attr("x", 850)
.attr("y", 400)
.attr("height", 20)
.attr("width", 20)
.style("fill", "red");
legend = svg.append("text")
.attr("x", 880)
.attr("y", 415)
.text("Dog");
legend = svg.append("rect")
.attr("x", 850)
.attr("y", 430)
.attr("height", 20)
.attr("width", 20)
.style("fill", "blue");
legend = svg.append("text")
.attr("x", 880)
.attr("y", 445)
.text("Cat");
</script>
<script>
function tooltipHtml(n, d){ /* function to create html content string in tooltip div. */
return "<tr><td>"+n+"</td></tr>" + "<br/>"+
"<tr><td>Median Household Income: $</td><td>"+(d.income)+"</td></tr>"+ "<br/>";
}
d3.csv("income.csv", function(d) {
return d;
},
function(error, dataset) {
if (error) throw error;
var data ={};
["FL", "SC", "GA", "AL", "NC", "TN", "RI", "CT", "MA",
"ME", "NH", "VT", "NY", "NJ", "PA", "DE", "MD", "WV", "KY", "OH",
"MI", "WY", "MT", "ID", "WA", "DC", "TX", "CA", "AZ", "NV", "UT",
"CO", "NM", "OR", "ND", "SD", "NE", "IA", "MS", "IN", "IL", "MN",
"WI", "MO", "AR", "OK", "KS", "LA", "VA"]
.forEach(function(d){
var color = d3.scaleLinear()
.domain([75675, 56516, 40037])
.range(["blue", "lightgrey", "red"]);
for (var i = 0; i < 49; i++)
{
if (d == dataset[i].id)
data[d]={income: dataset[i].income, color: color(dataset[i].income)};
}
});
uStates.draw("#statesvg2", data, tooltipHtml);
d3.select(self.frameElement).style("height", "600px");
});
</script>
<script>
var svg = d3.select("#statesvg2");
legend = svg.append("rect")
.attr("x", 800)
.attr("y", 400)
.attr("height", 20)
.attr("width", 20)
.style("fill", "red");
legend = svg.append("text")
.attr("x", 830)
.attr("y", 415)
.text("Low Income");
legend = svg.append("rect")
.attr("x", 800)
.attr("y", 430)
.attr("height", 20)
.attr("width", 20)
.style("fill", "blue");
legend = svg.append("text")
.attr("x", 830)
.attr("y", 445)
.text("High Income");
</script>
</html>