-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3_vis.js
462 lines (392 loc) · 14.3 KB
/
d3_vis.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
Promise.all([
d3.csv("https://raw.githubusercontent.com/6859-sp21/final-project-police-brutality/main/data/fatal-police-shootings-data.csv"),
d3.json("https://raw.githubusercontent.com/6859-sp21/final-project-police-brutality/main/data/race-population.json"),
]).then(function(data) {
// files[0] will contain file1.csv
// files[1] will contain file2.csv
fpsData = data[0];
raceData = data[1];
// d3_racism();
d3_militarization();
d3_population();
}).catch(function(err) {
// handle error here
})
// function d3_racism() {
// var width = 300,
// height = 300,
// innerRadius = 50,
// radius = Math.min(width, height) / 2,
// labelHeight = 10;
// var legendRectSize = 18;
// var legendSpacing = 4;
// var svg = d3.select("#d3-racism")
// .append("svg")
// .attr("width", width)
// .attr("height", height)
// .append("g")
// .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// // var color = d3.scaleOrdinal(["#a31526","#32964d", "#683c00", "#ff743c", "#255026", "#dd6e81", "#96a467"])
// // var color = d3.scaleOrdinal(["#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e","#e6ab02","#a6761d"])
// var color = d3.scaleOrdinal(["#66c2a5","#fc8d62","#8da0cb","#e78ac3","#a6d854","#ffd92f","#e5c494"]);
// var pie = d3.pie()
// .value(d => d.count)
// .sort(null);
// var arc = d3.arc()
// .innerRadius(innerRadius)
// .outerRadius(radius)
// function type(d) {
// d.killings = Number(d.killings)
// d.population = Number(d.population)
// return d;
// }
// function arcTween(a) {
// const i = d3.interpolate(this._current, a);
// this._current = i(1);
// return (t) => arc(i(t));
// }
// d3.json("https://raw.githubusercontent.com/6859-sp21/final-project-police-brutality/main/data/race-population.json", type).then(data => {
// var count = 0;
// d3.selectAll("input")
// .on("click", update);
// function update() {
// var val = "population"
// count += 1;
// // will start @ killings
// if (count % 2 === 0){
// val = "killings"
// }
// console.log(data[val])
// const path = svg.selectAll("path")
// .data(pie(data[val]));
// // Update existing arcs
// path.transition().duration(200).attrTween("d", arcTween);
// // Enter new arcs
// path.enter().append("path")
// .attr("fill", (d, i) => color(i))
// .attr("d", arc)
// .attr("stroke", "white")
// .attr("stroke-width", "2px")
// .each(function(d) { this._current = d; });
// }
// update();
// // tooltips
// var tooltip = d3.select("#d3-racism").append("div")
// .attr("class", "tooltip")
// .style("position", "absolute")
// .style("z-index", "10")
// .style("visibility", "hidden")
// .style("background", "white")
// .style("padding", "10px")
// .style("border-radius", "5px")
// .style("width", "200px")
// svg.selectAll("path")
// .on("mouseover", function(event, d) {
// tooltip.transition()
// .duration(200)
// .style("opacity", 0.9)
// .style("visibility", "visible")})
// .on("mousemove", function(event, d) {
// d3.select(this)
// .style("opacity", 0.5)
// tooltip.html(d.data.race + ": " + d.data.percentage + "%")
// .style("top",(event.pageY-10)+"px").style("left",(event.pageX+10)+"px")
// })
// .on("mouseout", function(){
// tooltip.transition()
// .duration(300)
// .style("opacity", 0)
// .style("visiblity", 'hidden')
// d3.select(this)
// .style("opacity", 1.0)})
// });
// }
function d3_population() {
var width = 300,
height = 300,
innerRadius = 50,
radius = Math.min(width, height) / 2
var svg = d3.select("#d3-population")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
var svgRace = d3.select("#d3-racism")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");// // var color = d3.scaleOrdinal(["#a31526","#32964d", "#683c00", "#ff743c", "#255026", "#dd6e81", "#96a467"])
// var color = d3.scaleOrdinal(["#a31526","#32964d", "#683c00", "#ff743c", "#255026", "#dd6e81", "#96a467"])
var color = d3.scaleOrdinal(["#a6cee3","#fb9a99","#b2df8a", "#fdbf6f","#cab2d6","#ffff99"])
// var color = d3.scaleOrdinal(["#66c2a5","#fc8d62","#8da0cb","#e78ac3","#a6d854","#ffd92f","#e5c494"]);
var pie = d3.pie()
.value(d => d.count)
.sort(null);
var arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(radius)
function type(d) {
d.killings = Number(d.killings)
d.population = Number(d.population)
return d;
}
d3.json("https://raw.githubusercontent.com/6859-sp21/final-project-police-brutality/main/data/race-population.json", type).then(data => {
const path = svg.selectAll("path")
.data(pie(data["population"]))
path.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d) {return (color(d.data.race))})
.attr("stroke", "white")
.attr("stroke-width", "1px")
const pathRace = svgRace.selectAll("path")
.data(pie(data["killings"]))
pathRace.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d) {return (color(d.data.race))})
.attr("stroke", "white")
.attr("stroke-width", "1px")
// tooltips
var tooltip = d3.select("#d3-population").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "white")
.style("padding", "10px")
.style("border-radius", "5px")
.style("width", "200px")
svg.selectAll("path")
.on("mouseover", function(event, d) {
tooltip.transition()
.duration(200)
.style("opacity", 0.9)
.style("visibility", "visible")})
.on("mousemove", function(event, d) {
d3.select(this)
.style("opacity", 0.5)
tooltip.html(d.data.race + ": " + d.data.percentage + "%")
.style("top",(event.pageY-10)+"px").style("left",(event.pageX+10)+"px")
})
.on("mouseout", function(){
tooltip.transition()
.duration(300)
.style("opacity", 0)
.style("visiblity", 'hidden')
d3.select(this)
.style("opacity", 1.0)})
svg.append("text")
.attr("text-anchor", "middle")
.attr('y', -5)
.attr('font-size', '1em')
.text(`Population`)
.attr('font-family', 'Crimson Text')
svg.append("text")
.attr("text-anchor", "middle")
.attr('y', 15)
.attr('font-size', '1em')
.text(`Distribution`)
.attr('font-family', 'Crimson Text')
var tooltipKillings = d3.select("#d3-racism").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "white")
.style("padding", "10px")
.style("border-radius", "5px")
.style("width", "200px")
svgRace.selectAll("path")
.on("mouseover", function(event, d) {
tooltip.transition()
.duration(200)
.style("opacity", 0.9)
.style("visibility", "visible")})
.on("mousemove", function(event, d) {
d3.select(this)
.style("opacity", 0.5)
tooltip.html(d.data.race + ": " + d.data.percentage + "%")
.style("top",(event.pageY-10)+"px").style("left",(event.pageX+10)+"px")
})
.on("mouseout", function(){
tooltip.transition()
.duration(300)
.style("opacity", 0)
.style("visiblity", 'hidden')
d3.select(this)
.style("opacity", 1.0)})
svgRace.append("text")
.attr("text-anchor", "middle")
.attr('y', -5)
.attr('font-size', '1em')
.text(`Death from`)
.attr('font-family', 'Crimson Text')
svgRace.append("text")
.attr("text-anchor", "middle")
.attr('y', 15)
.attr('font-size', '1em')
.text(`Police Brutality`)
.attr('font-family', 'Crimson Text')
})};
function d3_militarization() {
// set the dimensions and margins of the graph
var margin = {top: 60, right: 230, bottom: 50, left: 150},
width = 800 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#d3-militarization")
.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 + ")");
d3.csv("https://raw.githubusercontent.com/6859-sp21/final-project-police-brutality/main/data/all1033Categories.csv").then((data) => {
//////////
// GENERAL //
//////////
// List of groups = header of the csv files
var keys = data.columns.slice(1)
// color palette
var color = d3.scaleOrdinal()
.domain(keys)
// .range(["#32964d", "#a31526", "#96a467", "#683c00", "#ff743c", "#255026", "#dd6e81"]);
.range(["#4e79a7","#f28e2c","#e15759","#76b7b2","#59a14f","#edc949","#af7aa1","#ff9da7","#9c755f","#bab0ab"])
//stack the data?
var stackedData = d3.stack()
.keys(keys)
(data)
//////////
// AXIS //
//////////
// Add X axis
var x = d3.scaleLinear()
.domain(d3.extent(data, function(d) { return d.year; }))
.range([ 0, width ]);
var xAxis = svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(5).tickFormat(d3.format("d")))
// Add X axis label:
svg.append("text")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", height+40 )
.text("Year");
// Add Y axis label:
// svg.append("text")
// .attr("text-anchor", "end")
// .attr("x", -20)
// .attr("y", -20 )
// .text("Value of Loaned Equipment")
// .attr("text-anchor", "start")
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 2000000000])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y).ticks(5).tickFormat(d3.formatPrefix("$.1", 1e6)))
//////////
// BRUSHING AND CHART //
//////////
// Add a clipPath: everything out of this area won't be drawn.
var clip = svg.append("defs").append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", width )
.attr("height", height )
.attr("x", 0)
.attr("y", 0);
// Add brushing
var brush = d3.brushX() // Add the brush feature using the d3.brush function
.extent( [ [0,0], [width,height] ] ) // initialise the brush area: start at 0,0 and finishes at width,height: it means I select the whole graph area
.on("end", updateChart) // Each time the brush selection changes, trigger the 'updateChart' function
// Create the scatter variable: where both the circles and the brush take place
var areaChart = svg.append('g')
.attr("clip-path", "url(#clip)")
// Area generator
var area = d3.area()
.x(function(d) { return x(d.data.year); })
.y0(function(d) { return y(d[0]); })
.y1(function(d) { return y(d[1]); })
// Show the areas
areaChart
.selectAll("mylayers")
.data(stackedData)
.enter()
.append("path")
.attr("class", function(d) { return "myArea " + d.key })
.style("fill", function(d) { return color(d.key); })
.attr("d", area)
// Add the brushing
areaChart
.append("g")
.attr("class", "brush")
.call(brush);
var idleTimeout
function idled() { idleTimeout = null; }
// A function that update the chart for given boundaries
function updateChart(event) {
extent = event.selection
// If no selection, back to initial coordinate. Otherwise, update X axis domain
if(!extent){
if (!idleTimeout) return idleTimeout = setTimeout(idled, 350); // This allows to wait a little bit
x.domain(d3.extent(data, function(d) { return d.year; }))
}else{
x.domain([ x.invert(extent[0]), x.invert(extent[1]) ])
areaChart.select(".brush").call(brush.move, null) // This remove the grey brush area as soon as the selection has been done
}
// Update axis and area position
xAxis.transition().duration(1000).call(d3.axisBottom(x).ticks(5).tickFormat(d3.format("d")))
areaChart
.selectAll("path")
.transition().duration(1000)
.attr("d", area)
}
//////////
// HIGHLIGHT GROUP //
//////////
// What to do when one group is hovered
var highlight = function(event, d){
// reduce opacity of all groups
d3.selectAll(".myArea").style("opacity", .1)
// expect the one that is hovered
d3.select("."+d).style("opacity", 1)
}
// And when it is not hovered anymore
var noHighlight = function(d){
d3.selectAll(".myArea").style("opacity", 1)
}
//////////
// LEGEND //
//////////
// Add one dot in the legend for each name.
var size = 20
svg.selectAll("myrect")
.data(keys)
.enter()
.append("rect")
.attr("x", 450)
.attr("y", function(d,i){ return 10 + i*(size+5)}) // 100 is where the first dot appears. 25 is the distance between dots
.attr("width", size)
.attr("height", size)
.style("fill", function(d){ return color(d)})
.on("mouseover", highlight)
.on("mouseleave", noHighlight)
// Add one dot in the legend for each name.
svg.selectAll("mylabels")
.data(keys)
.enter()
.append("text")
.attr("x", 450 + size*1.2)
.attr("y", function(d,i){ return 10 + i*(size+5) + (size/2)}) // 100 is where the first dot appears. 25 is the distance between dots
.style("fill", function(d){ return color(d)})
.text(function(d){ return d})
.attr("text-anchor", "left")
.style("alignment-baseline", "middle")
.on("mouseover", highlight)
.on("mouseleave", noHighlight)
});
}