-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
516 lines (460 loc) · 12.2 KB
/
index.js
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// set the dimensions and margins of the graph
var margin_2 = { top: 20, right: 20, bottom: 30, left: 50 },
width = 1000 - margin_2.left - margin_2.right,
height = 500 - margin_2.top - margin_2.bottom;
var svg_2 = d3
.select("#graph-d")
.append("svg")
.attr("width", width + margin_2.left + margin_2.right)
.attr("height", height + margin_2.top + margin_2.bottom)
.append("g")
.attr("transform", "translate(" + margin_2.left + "," + margin_2.top + ")");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var margin = { top: 40, right: 100, bottom: 50, left: 100 },
width = 1020 - margin.left - margin.right,
height = 531.25 - margin.top - margin.bottom;
function drawGraph(xText, yText) {
// Get the data
d3.csv("gun_violence_data.csv", function(error, data) {
if (error) throw error;
// change string (from CSV) into number format
data.forEach(function(d) {
d[yText] = +d[yText];
if (d[xText] == "Y") {
d[xText] = 0;
} else if (d[xText] == "N") {
d[xText] = 1;
} else {
d[xText] = 2;
}
});
// Get all Data
var xName = d3
.nest()
.key(function(d) {
return d[xText];
})
.rollup(function(v) {
return v.length;
})
.entries(data);
var yName = d3
.nest()
.key(function(d) {
return d[yText];
})
.rollup(function(v) {
return v.length;
})
.entries(data);
let test = Object.values(xName[0]);
// Get all of the unique bin buckets
let keys = [];
for (i = 0; i < yName.length; i++) {
keys.push(parseInt(yName[i].key));
}
// Get array of objects of all objects
let allData = [];
for (j = 0; j < xName.length; j++) {
for (i = 0; i < keys.length; i++) {
let temp = Object.values(xName[j]);
let temp2 = temp.filter(obj => obj[yText] === keys[i]).length;
var object = {
outcome: Object.values(xName[j])[0],
bin: keys[i],
count: temp2
};
allData.push(object);
}
}
console.log(allData);
d3.select("#graph-d-title").text(xText + " vs " + yText)
//setup x
var xValue = function(d) {
return d[xText];
}, // data -> value
xScale = d3.scaleLinear().range([0, width]), // value -> display
xMap = function(d) {
return xScale(xValue(d));
}, // data -> display
xAxis = d3.axisBottom().scale(xScale);
// setup y
var yValue = function(d) {
return d[yText];
}, // data -> value
yScale = d3.scaleLinear().range([height, 0]), // value -> display
yMap = function(d) {
return yScale(yValue(d));
}, // data -> display
yAxis = d3.axisLeft().scale(yScale);
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(data, xValue) - 1, d3.max(data, xValue) + 1]);
yScale.domain([d3.min(data, yValue) - 1, d3.max(data, yValue) + 1]);
// x-axis
svg_2
.append("g")
.attr("transform", "translate(0," + height + ")")
.attr("class", "x-axis")
.call(xAxis);
svg_2
.append("text")
.attr("class", "xaxisText")
.attr(
"transform",
"translate(" + width / 2 + " ," + (height + margin_2.top + 10) + ")"
)
.style("text-anchor", "middle")
.text(xText);
// y-axis
svg_2
.append("g")
.attr("class", "y-axis")
.call(yAxis);
svg_2
.append("text")
.attr("class", "yaxisText")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin_2.left + 20)
.attr("x", 0 - height / 2)
.style("text-anchor", "middle")
.text(yText);
// draw dots
svg_2
.selectAll(".dot")
.data(data)
.enter()
.append("circle")
.transition()
.duration(750)
.attr("class", "dot")
.attr("r", 5)
.attr("cx", xMap)
.attr("cy", yMap)
.style("stroke", "red") // set the line colour
.style("fill", "none"); // set the fill colour
var select = d3.select("#x-value").on("change", function() {
var newX = d3.select("#x-value").property("value");
var newY = d3.select("#y-value").property("value");
redraw(newX, newY);
});
var select = d3.select("#y-value").on("change", function() {
var newX = d3.select("#x-value").property("value");
var newY = d3.select("#y-value").property("value");
redraw(newX, newY);
});
function redraw(xText, yText) {
d3.csv("gun_violence_data.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d[yText] = +d[yText];
if (d[xText] == "Y") {
d[xText] = 0;
} else if (d[xText] == "N") {
d[xText] = 1;
} else {
d[xText] = 2;
}
});
var xName = d3
.nest()
.key(function(d) {
return d[xText];
})
.rollup(function(v) {
return v.length;
})
.entries(data);
console.log(xName);
var yName = d3
.nest()
.key(function(d) {
return d[yText];
})
.rollup(function(v) {
return v.length;
})
.entries(data);
console.log(yName);
// setup x
var xValue = function(d) {
return d[xText];
}, // data -> value
xScale = d3.scaleLinear().range([0, width]), // value -> display
xMap = function(d) {
return xScale(xValue(d));
}, // data -> display
xAxis = d3.axisBottom().scale(xScale);
// setup y
var yValue = function(d) {
return d[yText];
}, // data -> value
yScale = d3.scaleLinear().range([height, 0]), // value -> display
yMap = function(d) {
return yScale(yValue(d));
}, // data -> display
yAxis = d3.axisLeft().scale(yScale);
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(data, xValue) - 1, d3.max(data, xValue) + 1]);
yScale.domain([d3.min(data, yValue) - 1, d3.max(data, yValue) + 1]);
d3.select("#graph-d-title").text(xText + " vs " + yText)
// re-render axis based on the new data
svg_2.selectAll(".xaxisText").text(xText);
svg_2.selectAll(".yaxisText").text(yText);
svg_2
.selectAll(".x-axis")
.transition()
.duration(10)
.call(xAxis);
svg_2
.selectAll(".y-axis")
.transition()
.duration(10)
.call(yAxis);
svg_2.selectAll("circle").remove();
svg_2
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.transition()
.duration(750)
.attr("class", "dot")
.attr("r", 5)
.attr("cx", xMap)
.attr("cy", yMap)
.style("stroke", "red") // set the line colour
.style("fill", "none"); // set the fill colour
});
}
});
}
drawGraph("Random Victims", "Wounded");
// define the line
var valueline = d3
.line()
.x(function(d) {
return x(d.Year);
})
.y(function(d) {
return y(d.Total_Shootings);
});
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3
.select("#graph")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Create Tooltip
var div = d3
.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.csv("gun_violence_data.csv", function(error, data) {
var ag_data = agregateData(data);
// Scale the range of the data
// draw the initial visualization
drawVis_1(ag_data);
// Adding the dropdown
var select = d3.select("#weapon-type");
select.on("change", function(d) {
var value = d3.select(this).property("value");
filterType(value, data);
});
select
.append("option")
.attr("value", "All")
.text("All");
select
.selectAll("option")
.data(
d3
.map(data, function(d) {
return d["Firearm Type"];
})
.keys()
)
.enter()
.append("option")
.attr("value", function(d) {
return d;
})
.text(function(d) {
return d;
});
});
agregateData = data => {
data = data.filter(function(d) {
return +d.Date.split("/")[2] >= 2008;
});
data = data.filter(function(d) {
return +d.Date.split("/")[2] <= 2018;
});
data = d3
.nest()
.key(function(d) {
// Summing by firearm type
return d.Date.split("/")[2];
})
.rollup(function(leaves) {
return leaves.length;
})
.entries(data)
.map(function(d) {
return { Year: d.key, Total_Shootings: d.value };
});
return data;
};
filterType = (value, data) => {
if (value == "All") {
data = data.filter(function(d) {
return d["Firearm Type"] != "";
});
} else {
data = data.filter(function(d) {
return d["Firearm Type"] == value;
});
}
var ag_data = agregateData(data);
// UPDATE THE VISUALIZATION
y.domain([
0,
d3.max(ag_data, function(d) {
return d.Total_Shootings;
})
]);
// Make the changes
svg
.select(".line")
.transition() // change the line
.duration(750)
.attr("d", valueline(ag_data));
svg
.select(".y.axis")
.transition() // change the y axis
.duration(750)
.call(d3.axisLeft(y).ticks(4));
svg
.selectAll("circle")
.data(ag_data)
.transition()
.duration(750)
.attr("cx", function(d) {
return x(d.Year);
})
.attr("cy", function(d) {
return y(d.Total_Shootings);
});
//Enter new circles
d3.selectAll("circle")
.data(ag_data)
.enter()
.append("circle")
.attr("cx", function(d) {
return x(d.Year);
})
.attr("cy", function(d) {
return y(d.Total_Shootings);
})
.attr("r", 3);
// Remove old
d3.selectAll("circle")
.data(ag_data)
.exit()
.remove();
};
drawVis_1 = data => {
x.domain(
d3.extent(data, function(d) {
return d.Year;
})
);
y.domain([
0,
d3.max(data, function(d) {
return d.Total_Shootings;
})
]);
// Add the valueline path.
svg
.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline);
// Add the scatterplot
svg
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("r", 3)
.attr("cx", function(d) {
return x(d.Year);
})
.attr("cy", function(d) {
return y(d.Total_Shootings);
})
.on("mouseover", function(d) {
div
.transition()
.duration(200)
.style("opacity", 5);
div
.html(
"<div><b>Year</b>: " +
d.Year +
"</div><br/>" +
"<div><b>Total Shootings</b>: " +
d.Total_Shootings +
"</div>"
)
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY - 65 + "px");
})
.on("mouseout", function(d) {
div
.transition()
.duration(500)
.style("opacity", 0);
});
// Add the X Axis
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.attr("class", "x axis")
.call(
d3.axisBottom(x).tickFormat(function(n) {
return n * 1;
})
);
// X Axis Label
svg
.append("text")
.attr(
"transform",
"translate(" + width / 2 + " ," + (height + margin.top + 0) + ")"
)
.style("text-anchor", "middle")
.style("font-size", "12px")
.text("Year");
// Add the Y Axis
svg
.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y).ticks(4));
// Y Axis Label
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -margin.left / 2)
.attr("x", 0 - height / 2)
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-size", "12px")
.text("Number of Shootings");
};