forked from humberto-ortiz/salHUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-testing-3.html
289 lines (195 loc) · 6.63 KB
/
pr-testing-3.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
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>PR Visualization</title>
<script type = "text/javascript" src = "http://d3js.org/d3.v3.min.js"></script>
<script type = "text/javascript" src = "http://d3js.org/topojson.v1.min.js"></script>
<style type = "text/css">
path {
fill: #ccc;
stroke: #000;
}
</style>
</head>
<body>
<h1>PR Visualization</h1>
<div align = "left">
<select id = "years">
<option selected value = 0>2000</option>
<option value = 1>2001</option>
<option value = 2>2002</option>
<option value = 3>2003</option>
<option value = 4>2004</option>
<option value = 5>2005</option>
<option value = 6>2006</option>
<option value = 7>2007</option>
<option value = 8>2008</option>
</select>
</div>
<div align = "right">
<select id = "death">
<option value = "accident">Accident</option>
<option value = "alzheimer">Alzheimer</option>
<option value = "cardio">Cardiovascular</option>
<option value = "cerebrovascular">Cerebrovascular</option>
<option value = "diabetes">Diabetes</option>
<option value = "homicide">Homicide</option>
<option selected value = "municipio_total">County Total</option>
<option value = "nefritis">Nefritis</option>
<option value = "pneumonia">Pneumonia</option>
<option value = "respiratory">Respiratory</option>
<option value = "tumor">Tumor</option>
</select>
</div>
<script type = "text/javascript">
var width = 1200;
var height = 700;
var path = d3.geo.path()
.projection(null);
var color = d3.scale.quantize()
.range(["rgb(255, 255, 212)","rgb(254, 227, 145)","rgb(254, 196, 79)",
"rgb(254, 153, 41)","rgb(217, 95, 14)","rgb(153, 52, 4)"]);
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var gdata;
var properties = [];
d3.csv("ypll.csv", function(error, data) {
//saves the csv in a global variable for later use.
gdata = data;
var column = document.getElementById("death")
[document.getElementById("death")
.selectedIndex].value;
color.domain([
0,
d3.max(data, function(d) { return parseFloat(d[column]); })
]);
d3.json("pr.json", function(error, pr) {
var obj = pr.objects.municipios;
for(var c = 0; c < obj.geometries.length; c++) {
obj.geometries[c].properties.year = [];
}
for(var i = 0; i < data.length; i++) {
var property = {};
property.id = parseInt(data[i].id);
property.municipio_total = parseFloat(data[i].municipio_total);
property.cardio = parseFloat(data[i].cardio);
property.tumor = parseFloat(data[i].tumor);
property.diabetes = parseFloat(data[i].diabetes);
property.alzheimer = parseFloat(data[i].alzheimer);
property.cerebrovascular = parseFloat(data[i].cerebrovascular);
property.respiratory = parseFloat(data[i].respiratory);
property.accident = parseFloat(data[i].accident);
property.nefritis = parseFloat(data[i].nefritis);
property.homicide = parseFloat(data[i].homicide);
property.pneumonia = parseFloat(data[i].pneumonia);
property.year = parseInt(data[i].year);
properties.push(property);
for(var j = 0; j < obj.geometries.length; j++) {
var prID = obj.geometries[j].properties.id;
if(properties[i].id == prID) {
obj.geometries[j].properties.year[properties[i].year - 2000] = properties[i];
break;
}
}
}
//Fill the counties with color
svg.selectAll("path")
.data(topojson.feature(pr, obj).features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d) {
var value = d.properties.year
[parseInt(document.getElementById("years")
[document.getElementById("years")
.selectedIndex].value)][column];
if(value) { return color(value); }
else { return "#ccc"; }
})
.on("click", function(d) {
var county = d.properties.name;
var year = document.getElementById("years").selectedIndex;
var death = document.getElementById("death")
[document.getElementById("death")
.selectedIndex].value;
var value = d.properties.year
[parseInt(document.getElementById("years")
[year].value)][death];
alert(county + ": " + parseInt(value));
})
.on("mouseover", function() {
d3.select(this)
.style("fill", "green")
.style("stroke-width", "1.8");
})
.on("mouseout", function(d) {
d3.select(this)
.transition()
.duration(250)
.style("fill", function(d) {
var year = document.getElementById("years").selectedIndex;
var death = document.getElementById("death")
[document.getElementById("death")
.selectedIndex].value;
var value = d.properties.year
[parseInt(document.getElementById("years")
[year].value)][death];
color.domain([
0,
d3.max(gdata, function(d) { return parseFloat(d[death]); })
]);
if(value) { return color(value); }
else { return "#ccc"; }
})
.style("stroke-width", "1");
});
});
});
//Update
d3.selectAll("select").on("change", change);
function change() {
if(this.id === "years") { transitionYears(); }
else { transitionDeaths(); }
};
function transitionYears() {
var newYear = document.getElementById("years").selectedIndex;
var death = document.getElementById("death")
[document.getElementById("death")
.selectedIndex].value;
svg.selectAll("path")
.transition()
.duration(750)
.style("fill", function(d) {
var value = d.properties.year
[parseInt(document.getElementById("years")
[newYear].value)][death];
if(value) { return color(value); }
else { return "#ccc"; }
});
};
function transitionDeaths() {
var year = document.getElementById("years").selectedIndex;
var newDeath = document.getElementById("death")
[document.getElementById("death")
.selectedIndex].value;
color.domain([
0,
d3.max(gdata, function(d) { return parseFloat(d[newDeath]); })
]);
svg.selectAll("path")
.transition()
.duration(750)
.style("fill", function(d) {
var value = d.properties.year
[parseInt(document.getElementById("years")
[year].value)][newDeath];
if(value) { return color(value); }
else { return "#ccc"; }
});
};
</script>
</body>
</html>