-
Notifications
You must be signed in to change notification settings - Fork 0
/
statecomparision.html
375 lines (339 loc) · 13.2 KB
/
statecomparision.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body { font: 12px Arial;}
path {
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 2;
shape-rendering: crispEdges;
}
.area {
fill: steelblue;
clip-path: url(#clip);
}
#Unemp{
align : float-right;
}
.y.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
}
.Fields {
display: inline-block;
}
.Fields > * {
display: inline-block;
}
.panel {
padd
ing: 40px;
}
.Width {
width: 100%;
}
</style>
<body>
<h2><center>Unemployment Rates of a State for two different years</center></h2>
<div class="Width panel">
<div class="Fields Width">
<select id="State">
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregan">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
</select>
<select id="Years">
<option value=1978>1978</option>
<option value=1979>1979</option>
<option value=1980>1980</option>
<option value=1981>1981</option>
<option value=1982>1982</option>
<option value=1983>1983</option>
<option value=1984>1984</option>
<option value=1985>1985</option>
<option value=1986>1986</option>
<option value=1987>1987</option>
<option value=1988>1988</option>
<option value=1989>1989</option>
<option value=1990>1990</option>
<option value=1991>1991</option>
<option value=1992>1992</option>
<option value=1993>1993</option>
<option value=1994>1994</option>
<option value=1995>1995</option>
<option value=1996>1996</option>
<option value=1997>1997</option>
<option value=1998>1998</option>
<option value=1999>1999</option>
<option value=2000>2000</option>
<option value=2001>2001</option>
<option value=2002>2002</option>
<option value=2003>2003</option>
<option value=2004>2004</option>
<option value=2005>2005</option>
<option value=2006>2006</option>
<option value=2007>2007</option>
<option value=2008>2008</option>
<option value=2009>2009</option>
<option value=2010>2010</option>
<option value=2011>2011</option>
<option value=2012>2012</option>
<option value=2013>2013</option>
<option value=2014>2014</option>
<option value=2015>2015</option>
<option value=2016>2016</option>
</select>
<button id="display" onclick="onDisplay()">Show</button>
</div>
<div id="Unemp">
</div>
</div>
<script src="d3.v3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/topojson/1.1.0/topojson.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function brush() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
slide.selectAll("path.line").attr("d", function(d) {return valueline(d)});
slide.select(".x.axis").call(xAxis);
slide.select(".y.axis").call(yAxis);
}
var selectedStates = [];
function displayState(state) {
selectedStates.push(state);
//console.log(selectedStates);
svg.selectAll("path.line").remove();
var dataArray = [];
for(var statename in dataAll){
selectedStates.forEach(function(s) {
if (s == statename)
dataArray.push(dataAll[statename]);
});
}
slide.selectAll(".line")
.data(dataArray)
.enter()
.append("path")
.attr("class", "line")
.attr("stroke-width",5)
.attr("id","displayline")
.attr("stroke", function (d) {
return grpcolor(d[0].State);
})
.attr("d", function(d){
return valueline(d);
})
.attr("clip-path", "url(#clip)")
.on('mouseover', function (e) {
d3.select("#statePath-" + e[0].State).style('fill-opacity', 0.1);
})
.on('mouseout', function(e) {
d3.select("#statePath-" + e[0].State).style('fill-opacity', 1);
slide.selectAll(".line").append("title")
.text(function (d) {
return d[0].State;
});
});
compare.selectAll(".line")
.data(dataArray)
.enter()
.append("path")
.attr("class", "line")
.attr("stroke-width",5)
.attr("stroke", function (d) {
return grpcolor(d[0].State);
})
.attr("d", function(d){
return valueline2(d);
})
.attr("clip-path", "url(#clip)")
.on("mouseover", function (d) {
svg.selectAll(".line")
.attr("stroke-width", 15);
});
}
// Set the dimensions of the graph
var margin = {top: 20, right: 15, bottom: 100, left: 50},
margin2 = {top: 430, right: 15, bottom: 20, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
height2 = 500 - margin2.top - margin2.bottom;
// Parse the date / time
var parseDate2 = d3.time.format("%B-%Y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brush);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.unemplyment); });
var valueline2 = d3.svg.line()
.x(function(d) { return x2(d.date); })
.y(function(d) { return y2(d.unemplyment); });
// Adds the svg
var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var slide = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var compare = svg.append("g")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
var grpcolor = d3.scale.category20();
var dataAll = {};
d3.csv("unemplyment.csv", function(error, data) {
data.forEach(function(d) {
if (dataAll[d.State]== undefined)
dataAll[d.State] = [];
dataAll[d.State].push(d);
});
for(var statename in dataAll){
dataAll[statename].forEach(function(d) {
d.date = parseDate2(d.Month+"-"+d.Year);
d.unemplyment = +d["unemplyment"];
});
dataAll[statename].sort(function(a,b){
if (a.date<b.date)
return -1;
else if (a.date>b.date)
return 1;
else
return 0;
})
}
});
var margin1 = {top1: 20, right1: 10, bottom1: 100, left1: 40};
var width1 = 1000 - margin1.left1 - margin1.right1;
var height1 = 500 - margin1.top1 - margin1.bottom1;
var x1 = d3.scale.ordinal().rangeRoundBands([0, width1]);
var y1 = d3.scale.linear().range([height1, 0]);
var xAxis1 = d3.svg.axis().scale(x1).orient("bottom").ticks(12);
var yAxis1 = d3.svg.axis().scale(y1).orient("left").ticks(12);
// Define the line
var valueline1 = d3.svg.line()
.x(function(d) { return x1(d.Month); })
.y(function(d) { return y1(d.unemplyment); });
var slide1 = d3.select("#Unemp")
.append("svg")
.attr("width", width1 + margin1.left1+ margin1.right1)
.attr("height", height1 + margin1.top1 + margin1.bottom1)
.append("g")
.attr("transform", "translate(" + margin1.left1 + "," + margin1.top1 + ")");
var grpcolor1 = d3.scale.category20c();
x1.domain(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"])
.rangePoints([0, width1]);
y1.domain([0, 12]);
// Add the X Axis
slide1.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height1 + ")")
.call(xAxis1)
.append("text")
.attr("y", 40)
.attr("x", 450)
.attr("fill", "#000")
.text("Months");
// Add the Y Axis
slide1.append("g")
.attr("class", "y axis")
.call(yAxis1)
.append("text")
.attr("text-anchor", "margin-right")
.attr("transform", "rotate(-90)")
.attr("y", 10)
.attr("x", -140)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Unemployment Rate(%)");
function onDisplay() {
var state = State.value;
var date = Years.value;
var values = dataAll[state];
var selectedItems = [];
selectedItems.push(values.filter(o => o.Year == date));
slide1
.append("path")
.data(selectedItems)
.attr("class", "line")
.attr("stroke-width",2)
.attr("stroke",function(d){
return grpcolor(state);
})
.attr("d", function(d){
return valueline1(d);
});
slide1.selectAll(".line").append("title")
.text(function (d) {
console.log(d[0].Month);
});
}
</script>