-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex3.html
319 lines (305 loc) · 12.2 KB
/
index3.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
/*body,html{
margin: 0;
padding: 0;
font-family: "Arial", sans-serif;
font-size: 11px;
text-align: center;
}*/
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.key path {
display: none;
}
.key line {
stroke: #000;
shape-rendering: crispEdges;
}
.key text {
font-size: 10px;
}
</style>
</head>
<body>
<div >
<h3>Description</h3>
<p >
<p>The choropleth on the left depicts the number of votes cast in the 2016 presidential election. On hovering over the states, a bar chart is generated on the right for the county-wise breakdown and disappears on mouseout.</p>
<p>On checking the checkbox, the choropleth and the generated bar charts use the population normalised (per capita) values ie., #votes=20 and population=100, value used = 20/100=0.2</p>
<p>The grey state(Minnesota) on the choropleth depicts missing values and hovering over some of the states generates an empty bar chart when there is no county-wise data available for those states.</p>
<p>On checking the bonus 1 check box, a heat map is generated in place of the choropleth and has the same functionalities as mentioned above for choropleth i.e., the heat map is generated again with per_capita values instead of raw values. Since the colours of the heat map are determined according to the percentile, the colours do not differ like they do for the choropleth.</p>
</p>
</div>
<div id="map">
<h3><center>Votes Cast In US Presidential Election 2016: State and County-Wise Breakdown</center></h3>
</div>
<div id="chart">
</div>
<div id="toggleContainer">
<table>
<tr>
<td>
<input id="toggle1" type="checkbox">
</td>
<td>Check this to switch between choropleth and heat map (Bonus 1)</td>
</tr>
<tr>
<td>
<input id="toggle3" type="checkbox" onclick="drawMaps()">
</td>
<td>Check this to switch to population normalised graph (Bonus 2)</td>
</tr>
</table>
<script type="text/javascript">
window.onload=function(e){
drawChoropleth(false);
var checkboxes = document.getElementsByTagName('input');
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = false;
}
}
}
</script>
<script type="text/javascript">
function drawChart(currentState,flagValue){
//console.log(currentState);
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 850 - margin.left - margin.right,
height = 320 - margin.top - margin.bottom;
var x = d3.scaleBand()
.range([0, width])
.padding(0.5);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom(x);
var yAxis = d3.axisLeft(y);
var svg = d3.select("#chart").append("svg")
.attr("id","bar")
.attr("transform","translate(650,-370)")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", //"translate(400,0)");
"translate(" + margin.left + "," + margin.top + ")");
/*svg..select("#bar").append("text")
.attr("x", 650)
.attr("y", -370)
.attr("dy", ".35em")
.text("County-Wise Votes Breakdown");*/
d3.csv("hw3-input.csv", function(error, data) {
data= data.filter(function(d){
return d.state==currentState;});
//console.log(data);
data.forEach(function(d) {
//console.log(d);
if (flagValue==true){
d.votes=parseFloat(d.votes_popl);
}
else{
dataVotes = parseInt(d.votes);
}
//d.votes = parseInt(d.votes);
});
x.domain(data.map(function(d) { return d.county; }));
y.domain([0, d3.max(data, function(d) {
console.log(d.votes);
return d.votes; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end");
//.text("Value ($)");
svg.selectAll("#bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) {
return x(d.county); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.votes); })
.attr("height", function(d) { return height - y(d.votes); });
});
}
</script>
<script type="text/javascript">
function drawChoropleth(flagValue){
//Width and height
var w = 700;
var h = 400;
var activeCounty;
// define map projection
var projection = d3.geoAlbersUsa()
.translate([w/3 + 25, h/2])
.scale(700);
//Define default path generator
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("#map")
.append("svg")
.attr("id","map-chart")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("transform","translate(25,0)");
//.attr("visibility","hidden");
//.attr("tranform", "translate(0" + margin.left + "," + margin.top + ")");
var color = d3.scaleThreshold().domain([0,1000,15000,45000,75000,101000,251000,501000,751000,1000000,1250000])
.range(["#f7fcfd","#e5f5f9","#ccece6","#99d8c9","#66c2a4","#41ae76","#238b45","#01a744","#01933c","#006d2c","#00441b"]);
d3.csv("hw3-state.csv", function(data) {
//console.log(data.length)
// Load TopoJSON
//var json={"my":"json"};
d3.json("us-states.json", function(error, json) {
//alert(error)
for (var i = 0; i < data.length; i++) {
var dataState = data[i].state;
var dataVotes;
if (flagValue==true){
dataVotes=parseFloat(data[i].votes_popl);
}
else{
dataVotes = parseInt(data[i].votes);
}
//console.log(dataVotes);
for (var j = 0; j < json.features.length; j++) {
var jsonCounty=json.features[j].properties.NAME;
if(dataState.toUpperCase() == jsonCounty.toUpperCase()){
//Copy the data value into the JSON
// basically creating a new value column in JSON data
json.features[j].properties.value = dataVotes;
//console.log(json.features[j].properties.value)
break;
}
}
}
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function(d){
//get the data value
var value = d.properties.value;
if(value){
//If value exists
//console.log(color(value))
return color(value);
} else {
return "#ccc"
}
}) // style fill
.on("mouseover", function(d) {
var xPosition = d3.mouse(this)[0];
var yPosition = d3.mouse(this)[1] - 30;
activeCounty=d.properties.NAME;
// console.log(d.properties);
svg.append("text")
.attr("id", "tooltip")
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("font-weight", "bold")
.attr("fill", "black")
.text(d.properties.NAME);
d3.select(this)
.style("fill", "steelblue");
var activeState=d.properties.NAME
//console.log(activeState);
drawChart(activeState);
})
.on("mouseout", function(d) {
d3.select("#tooltip").remove();
d3.select(this)
.transition()
.duration(250)
.style("fill", function(d){
//get the data value
var value = d.properties.value;
if(value){
//If value exists
//console.log(color(value))
return color(value);
} else {
return "#ccc"
}
})
d3.select("#bar").remove();
});
}); //json loop
}) //csv loop
var y = d3.scaleSqrt()
.domain([0, 1250000])
.range([0,250]);
var yAxis = d3.axisLeft(y)
//.scale(y)
.tickValues(color.domain())
var g = svg.append("g")
.attr("class", "key")
.attr("transform", "translate(550, 165)")
.call(yAxis);
g.selectAll("rect")
.data(color.range().map(function(d, i) {
return {
y0: i ? y(color.domain()[i - 1]) : y.range()[0],
y1: i < color.domain().length ? y(color.domain()[i]) : y.range()[1],
z: d
};
}))
.enter().append("rect")
.attr("width", 8)
.attr("y", function(d) { return d.y0; })
.attr("height", function(d) { return d.y1 - d.y0; })
.style("fill", function(d) { return d.z; });
}
</script>
<script type="text/javascript">
var flagValue=false;
//drawChart("Arizona",false);
//var hoodsCheckbox = document.querySelector('input[id="hoods_toggle"]');
var dataCheckbox = document.querySelector('input[id="toggle3"]');
/*hoodsCheckbox.onchange = function() {
if(this.checked) {
d3.selectAll(".neighborhoods").attr("visibility", "visible");
} else {
d3.selectAll(".neighborhoods").attr("visibility", "hidden");
}
};*/
dataCheckbox.onchange = function() {
d3.select("#map-chart").remove();
if(this.checked) {
drawChoropleth(true);
} else {
drawChoropleth(false);
}
};
</script>
</body>
</html>