-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
299 lines (263 loc) · 8.7 KB
/
main.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
// Global function called when the button is clicked
function onCategoryChanged() {
// create an array of the modes of transportation that are selected
var filter = [];
if (d3.select("#totals").property("checked")) {
filter.push("Total_Per_100K");
}
if (d3.select("#cars").property("checked")) {
filter.push("Car_Per_100K");
}
if (d3.select("#pedestrians").property("checked")) {
filter.push("Ped_Per_100K");
}
if (d3.select("#motorcycles").property("checked")) {
filter.push("Motorcycle_Per_100K");
}
if (d3.select("#bicycles").property("checked")) {
filter.push("Bicycle_Per_100K");
}
if (d3.select("#trucks").property("checked")) {
filter.push("Trucks_Per_100K");
}
if(filter.length == 0) {
confirm("Please select at least one mode of transportation.")
} else {
updateChart(filter);
}
}
var width = 1000;
var height = 750;
// scales
var xScale;
var yScale;
var data;
var svg = d3.select('svg');
var chart1 = svg.append('g');
d3.csv("trafficDataSet.csv", function (csv) {
data = csv;
var initialFilter = ["Total_Per_100K", "Car_Per_100K", "Ped_Per_100K", "Motorcycle_Per_100K", "Bicycle_Per_100K", "Trucks_Per_100K"];
chart1 = d3
.select("#chart1")
.append("svg:svg")
.attr("id", "svg1")
.attr("width", width)
.attr("height", height);
// Add filter button
d3.select('.options')
.append('p')
.append('button')
.attr('id', 'filterButton')
.style("border", "1px solid black")
.text('Filter Data')
.on('click', function() {
onCategoryChanged();
});
updateChart(initialFilter);
});
function addAxes(modes) {
// Axis setup
xScale = d3.scaleTime().domain(d3.extent(data, function(d) {
return d3.timeParse("%Y")(d.Year);
})).range([50, width-30]);
yScale = d3.scaleLinear().domain([
(d3.min(modes, function(d) {
return d3.min(Object.keys(d), function(key) {
return +d[key];
})
})),
(d3.max(modes, function(d) {
return d3.max(Object.keys(d), function(key) {
return +d[key];
})
}))
]).range([height-30, 30]);
var xAxis = d3.axisBottom().scale(xScale);
var yAxis = d3.axisLeft().scale(yScale);
// Create labels for the chart
svg = d3.select("body").select("svg")
// Adds title to the chart
svg.append("text")
.attr("class", "title-label")
.attr("text-anchor", "end")
.attr("x", width - 300)
.attr("y", 50)
.attr("font-weight", "bold")
.attr("text-decoration", "underline")
.text("Fatality Rates (per 100K people) for Different Modes of Transportation");
// Add x axis label
svg.append("text")
.attr("class", "x-label")
.attr("text-anchor", "end")
.attr("x", width - 475)
.attr("y", height)
.attr("font-weight", "bold")
.text("Year");
// Add y axis label
svg.append("text")
.attr("class", "y-label")
.attr("text-anchor", "end")
.attr("x", -300)
.attr("y", 6)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.attr("font-weight", "bold")
.text("Fatalitities per 100K people");
// append x-axis
chart1 // or something else that selects the SVG element in your visualizations
.append("g") // create a group node
.attr("class", "axis-label x-axis")
.attr("transform", "translate(0," + (height - 30) + ")")
.call(xAxis) // call the axis generator
.append("text")
.attr("x", width - 16)
.attr("y", -6)
.style("text-anchor", "end")
var chart1Enter = chart1.selectAll('.y-axis').data(data)
chart1Enter.enter()
.append("g") // create a group node
.attr("class", "axis-label y-axis")
.attr("transform", "translate(50, 0)")
.merge(chart1.selectAll(".y-axis"))
.data(data)
.call(yAxis)
.append("text")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
chart1Enter.exit().remove();
}
function updateChart(filter) {
// filter out modes so that it only contains the modes that are checked
var modes = data.map(item =>
filter.reduce((acc, key) => {
acc[key] = item[key];
return acc;
}, {})
)
addAxes(modes);
// add the year to modes after the axes are updated
modes = modes.map(function(d, i) {
d.Year = data[i].Year;
return d;
})
// append the total data points to the chart as a line graph only if the checkbox is checked, otherwise remove it
chart1.selectAll('.line').remove();
for(var i = 0; i < filter.length; i++) {
var line = d3.line()
.x(function(d) {
return xScale(d3.timeParse("%Y")(d.Year));
})
.y(function(d) {
return yScale(+d[filter[i]]);
});
chart1.append("path")
.datum(modes)
.attr("d", line)
.attr("class", function() {
if(filter[i] == "Total_Per_100K") {
return "totals line"
} else if(filter[i] == "Car_Per_100K") {
return "cars line"
} else if(filter[i] == "Ped_Per_100K") {
return "pedestrians line"
} else if(filter[i] == "Motorcycle_Per_100K") {
return "motorcycles line"
} else if(filter[i] == "Bicycle_Per_100K") {
return "bicycles line"
} else if(filter[i] == "Trucks_Per_100K") {
return "trucks line"
}
})
}
// add a mouseover event to the chart that will show the data points for each line
var mouseG = chart1.append("g").attr("class", "mouse-hover");
mouseG.append("path")
.attr("class", "mouse-line")
.style("stroke", "black")
.style("stroke-width", "1px")
.style("opacity", "0");
var mousePerLine = mouseG.selectAll('.mouse-per-line')
.data(modes)
.enter()
.append("g")
.attr("class", "mouse-per-line");
mousePerLine.append("circle")
.attr("r", 7)
.style("stroke", "black")
.style("fill", "none")
.style("stroke-width", "1px")
.style("opacity", "0");
mousePerLine.append("text")
.attr("transform", "translate(10,3)");
mouseG.append('svg:rect')
.attr('width', (width - 30))
.attr('height', (height))
.attr('fill', 'none')
.attr('pointer-events', 'all')
.on('mouseout', function() {
d3.select(".mouse-line")
.style("opacity", "0");
d3.selectAll(".mouse-per-line circle")
.style("opacity", "0");
d3.selectAll(".mouse-per-line text")
.style("opacity", "0");
})
.on('mouseover', function() {
d3.select(".mouse-line")
.style("opacity", "1")
d3.selectAll(".mouse-per-line circle")
.style("opacity", "1");
d3.selectAll(".mouse-per-line text")
.style("opacity", "1");
})
// it seems like the mousemove event is not working properly
.on('mousemove', function() {
var lines = document.getElementsByClassName('line');
var mouse = d3.mouse(this);
d3.select(".mouse-line")
.attr("d", function() {
var d = "M" + mouse[0] + "," + height;
d += " " + mouse[0] + "," + 0;
return d;
});
d3.selectAll(".mouse-per-line")
.attr("transform", function(d, i) {
var xDate = xScale.invert(mouse[0]),
bisect = d3.bisector(function(d) { return d.Year; }).right;
idx = bisect(d, xDate);
var beginning = 0;
var end;
// end should not be undefined
try {
end = lines[i].getTotalLength();
} catch (TypeError) {
end = 0;
}
var target = null;
while (true) {
target = Math.floor((beginning + end) / 2);
var pos;
try {
pos = lines[i].getPointAtLength(target)
} catch (TypeError) {
pos = 0;
}
if ((target === end || target === beginning) && pos.x !== mouse[0]) {
break;
}
if (pos.x > mouse[0]) end = target;
else if (pos.x < mouse[0]) beginning = target;
else break;
}
// check to see if the text is NaN or undefined
if (isNaN(pos.y) || isNaN(pos.x) || pos.x == undefined || pos.y == undefined) {
return;
} else {
d3.select(this).select('text')
.text(yScale.invert(pos.y).toFixed(2));
return "translate(" + mouse[0] + "," + pos.y +")";
}
});
});
}