-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilitarization.js
192 lines (156 loc) · 5.61 KB
/
militarization.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
// set the dimensions and margins of the graph
var margin = {top: 60, right: 230, bottom: 50, left: 100},
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(["#fd2c3b", "#e6bcbe", "#613d38", "#f9793b", "#8e1023", "#fb899b", "#aa7959"]);
//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", "end")
.attr("x", width)
.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))
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", 500)
.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", 500 + 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)
});