-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.html
executable file
·430 lines (383 loc) · 13.8 KB
/
index.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="d3.v2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="jquery.tipsy.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<link rel="stylesheet" href="tipsy.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<div><h1>The Costs of Living, 2010</h1></div>
</head>
<body>
<div class="graph">
<script type="text/javascript">
// 1a) states are defined here
var width = 670,
height = 400,
centered;
var projection = d3.geo.albersUsa()
.scale(width)
.translate([0, 0]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", click);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("g")
.attr("id", "states");
//1b) states are now plotted
var map = d3.json("readme.json", function(json) {
g.selectAll("path")
.data(json.features)
.enter().append("path")
.attr("d", path)
.on("click", click)
.classed("active", false)
.on("mouseover", function() { d3.select(d3.event.target).classed("highlight", true); })
.on("mouseout", function() { d3.select(d3.event.target).classed("highlight", false); });
});
// 2a) define cities here
var circles = svg.append("svg:g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("svg:g")
.attr("id", "circles")
.on("click",click);
// 2b) define empty arrays to house csv columns
var City = [];
var Lat = [];
var Long = [];
var CompositeIndex = [];
var GroceryItems = [];
var Housing = [];
var Utilities = [];
var Transportation = [];
var HealthCare = []
var longLat = {"type": "FeatureCollection", "features":[]};
// 2c) takes csv file, stores each column into arrays above, and we'll eventually concactenate them into objects in dataSet
d3.csv("living.csv", function(cities)
{
var dataEnter = d3.select("body").data(cities).enter();
dataEnter.append("span").html(function(d)
{
City.push(d["City"]);
Lat.push(parseFloat(d["Lat"]));
Long.push(parseFloat(d["Long"]));
CompositeIndex.push(parseFloat(d["CompositeIndex"]));
GroceryItems.push(parseFloat(d["GroceryItems"]));
Housing.push(parseFloat(d["Housing"]));
Utilities.push(parseFloat(d["Utilities"]));
Transportation.push(parseFloat(d["Transportation"]));
HealthCare.push(parseFloat(d["HealthCare"]));
});
// 2d) stores longitude and latitude in recogniznable GEOjson format, and other variables within Features object
for (var i = 0; i < City.length; i++) {
var obj = {
type: "Point",
coordinates: [Long[i],Lat[i]],
City: [City[i]],
CompositeIndex: CompositeIndex[i],
GroceryItems: GroceryItems[i],
Housing: Housing[i],
Utilities: Utilities[i],
Transportation: Transportation[i],
HealthCare: HealthCare[i],
Status: 1
};
longLat["features"].push(obj);
}
// 2f) check to make sure objects stored correctly (comment out later)
/* console.log(longLat["features"]); */
// 2g) Plots cities on map
circles.selectAll("path")
.data(longLat.features)
.enter().insert("a")
.append("path")
.attr("d", path);
// 2h) gives hover states
var hover = function() {
$('g>a').tipsy({
gravity: 'w',
html: true,
title: function() {
var d = this.__data__;
var a = d["City"];
var b = d["CompositeIndex"];
var c = d["Housing"];
var e = d["GroceryItems"];
var f = d["Utilities"];
var g = d["Transportation"];
var h = d["HealthCare"];
if (d.Status>0) {
return "<b>" + a + "</b>" + "<br>" + "Composite Index: " + b + "<br>" + "Housing Costs: " + c + "<br>" + "Grocery Costs: " + e + "<br>" + "Utilities Costs: " + f + "<br>" + "Transportation Costs: " + g + "<br>" + "Healthcare Costs: " + h;
}
else {return "<b>" + a + "</b>" + "<br>" + "Not in current selection";}
}});};
hover();
// 5) define sliders for different categories
// Global filter variables
var comp_min = 80,
comp_max = 190,
groc_min = 70,
groc_max = 170,
house_min = 60,
house_max = 320,
util_min = 70,
util_max = 200,
trans_min = 80,
trans_max = 150,
health_min = 80,
health_max = 150;
// Predicate for filtering a single data point
function isOutOfRange(d) {
return d.CompositeIndex < comp_min || d.CompositeIndex > comp_max
|| d.GroceryItems < groc_min || d.GroceryItems > groc_max
|| d.Housing < house_min || d.Housing > house_max
|| d.Utilities < util_min || d.Utilities > util_max
|| d.Transportation < trans_min || d.Transportation > trans_max
|| d.HealthCare < health_min || d.HealthCare > health_max;
}
$(function() {
$("#slider-comp").slider({
range: true,
min: 80,
max: 190,
values: [80, 190],
slide: function(event, ui) {
// Update category filters
comp_min = ui.values[0];
comp_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-comp").text("Composite Index: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-comp").text("Composite Index: " + $("#slider-comp").slider("values", 0) +
" - " + $("#slider-comp").slider("values", 1));
});
$(function() {
$("#slider-groc").slider({
range: true,
min: 70,
max: 170,
values: [70, 170],
slide: function(event, ui) {
// Update category filters
groc_min = ui.values[0];
groc_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-groc").text("Grocery Items: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-groc").text("Grocery Items: " + $("#slider-groc").slider("values", 0) +
" - " + $("#slider-groc").slider("values", 1));
});
$(function() {
$("#slider-house").slider({
range: true,
min: 60,
max: 320,
values: [60, 320],
slide: function(event, ui) {
// Update category filters
house_min = ui.values[0];
house_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-house").text("Housing: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-house").text("Housing: " + $("#slider-house").slider("values", 0) +
" - " + $("#slider-house").slider("values", 1));
});
$(function() {
$("#slider-util").slider({
range: true,
min: 70,
max: 200,
values: [70, 200],
slide: function(event, ui) {
// Update category filters
util_min = ui.values[0];
util_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-util").text("Utilities: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-util").text("Utilities: " + $("#slider-util").slider("values", 0) +
" - " + $("#slider-util").slider("values", 1));
});
$(function() {
$("#slider-trans").slider({
range: true,
min: 80,
max: 150,
values: [80, 150],
slide: function(event, ui) {
// Update category filters
trans_min = ui.values[0];
trans_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-trans").text("Transportation: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-trans").text("Transportation: " + $("#slider-trans").slider("values", 0) +
" - " + $("#slider-trans").slider("values", 1));
});
$(function() {
$("#slider-health").slider({
range: true,
min: 80,
max: 150,
values: [80, 150],
slide: function(event, ui) {
// Update category filters
health_min = ui.values[0];
health_max = ui.values[1];
// Filter on all global filters
var allPaths = circles.selectAll("path");
allPaths.style("opacity",1).filter(function(d) {
d.Status = 1;
/* console.log(d.Status); */
})
.on("mouseover",hover());
allPaths.filter(isOutOfRange).style("opacity", 0).filter(function(d){
d.Status = 0;
})
.on("mouseover",hover());
// Dynamically update indicator text
$("#label-health").text("Health Care: " + ui.values[0] + " - " + ui.values[1]);
}
});
// Initialize indicator text
$("#label-health").text("Health Care: " + $("#slider-health").slider("values", 0) +
" - " + $("#slider-health").slider("values", 1));
});
}); /* close main body of code */
// 4) here we have our zoom function
function click(d) {
var x = 0,
y = 0,
k = 1;
if (d && centered !== d) {
var centroid = path.centroid(d);
x = -centroid[0];
y = -centroid[1];
k = 3;
centered = d;
} else {
centered = null;
}
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(1000)
.attr("transform", "scale(" + k + ")translate(" + x + "," + y + ")")
.style("stroke-width", 1.5 / k + "px");
circles.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
circles.transition()
.duration(1000)
.attr("transform", "scale(" + k + ")translate(" + x + "," + y + ")")
.attr("id","circles")
.style("stroke-width", 1 / k + "px")
}
</script>
</div>
<div class="all-sliders" style="text-align: right"></div>
<div class="slider-container" style="text:align: right">
In each of these categories, the national average is 100.
</div>
<div class="slider-container">
<span class="label" id="label-comp">Composite Index: </span>
<div class="slider" id="slider-comp"></div>
</div>
<div class="slider-container">
<span class="label" id="label-groc">Grocery Costs: </span>
<div class="slider" id="slider-groc"></div>
</div>
<div class="slider-container">
<span class="label" id="label-house">Housing Costs: </span>
<div class="slider" id="slider-house"></div>
</div>
<div class="slider-container">
<span class="label" id="label-util">Utilities Costs: </span>
<div class="slider" id="slider-util"></div>
</div>
<div class="slider-container">
<span class="label" id="label-trans">Transportation Costs : </span>
<div class="slider" id="slider-trans"></div>
</div>
<div class="slider-container">
<span class="label" id="label-health">Healthcare Costs: </span>
<div class="slider" id="slider-health"></div>
</div>
</body>
</html>