-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataFilter.js
More file actions
276 lines (235 loc) · 7.09 KB
/
dataFilter.js
File metadata and controls
276 lines (235 loc) · 7.09 KB
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
/*
This file consists of the javascript code needed to build the data
sorting visualization. This is the first graph on the live version,
where you can sort and filter using buttons.
*/
async function getData() {
const res = await fetch(
"https://anisha7.github.io/titanicdata/titanic-passengers.json"
);
const json = await res.json();
return json;
}
// data setup
const data = getData().then(d => {
handleData(d);
});
// graph data storage
const elements = [];
const passengerData = [];
// graph div elements
const container = document.getElementById("content7");
container.style.display = "flex";
container.style.flexDirection = "row";
container.style.flexWrap = "wrap";
const overlay = document.getElementById('content7-overlay')
// ********** FILTERING **********
// graph display style controllers
let showGender = false;
let showEmbarked = false;
let showSurvived = false;
// filtering button elements
const buttonGender = document.getElementById("button-gender");
const buttonEmbarked = document.getElementById("button-embarked");
const buttonSurvived = document.getElementById("button-survived");
// event listeners for filtering buttons
buttonGender.addEventListener("click", e => {
showGender = !showGender;
selectButton(e, showGender);
displayByGender();
});
buttonEmbarked.addEventListener("click", e => {
showEmbarked = !showEmbarked;
selectButton(e, showEmbarked);
displayByEmbarked();
});
buttonSurvived.addEventListener("click", e => {
showSurvived = !showSurvived;
selectButton(e, showSurvived);
displayBySurvived();
});
// Append basic/default divs to graph
// and initialize elements and passengerData
// to use for formatting graph later
function handleData(data) {
const fields = data.map(({ fields }) => fields);
fields.forEach((passenger, i) => {
const el = document.createElement("div");
el.style.width = "14px";
el.style.height = "14px";
el.style.backgroundColor = "#F28041";
el.style.margin = "1px";
el.style.transition = "200ms"; // use trasnsition to asnimate changes
el.style.boxSizing = "border-box";
el.style.cursor = "pointer";
el.dataset.index = i; // <div data-index = "i">
container.appendChild(el);
elements.push(el); // store the element
passengerData.push(passenger); // Store the passenger
});
}
// Helper functions for drawing the graph
function selectButton(e, state) {
if (state) {
e.target.style.backgroundColor = "black";
e.target.style.color = "white";
} else {
e.target.style.backgroundColor = "white";
e.target.style.color = "black";
}
}
function displayByGender() {
passengerData.forEach((obj, i) => {
const el = elements[i];
const color = obj.sex === "male" ? "#1589F0" : "pink";
el.style.backgroundColor = showGender ? color : "F28041";
});
}
function displayByEmbarked() {
passengerData.forEach((obj, i) => {
const el = elements[i];
let borderColor = "darkgray";
if (obj.embarked === "Q") {
borderColor = "red";
} else if (obj.embarked === "C") {
borderColor = "purple";
} else if (obj.embarked === "S") {
borderColor = "green";
}
el.style.border = showEmbarked ? `2px solid ${borderColor}` : "none";
});
}
function displayBySurvived() {
passengerData.forEach((obj, i) => {
const el = elements[i];
const borderRadius = obj.survived === "No" ? "0px" : "25px";
el.style.borderRadius = showSurvived ? borderRadius : "0px";
});
}
// redraw all data with current selected filters
function redraw() {
displayByGender();
displayByEmbarked();
displayBySurvived();
}
// ********** SORTING **********
// graph display style controllers
let sortGender = false;
let sortEmbarked = false;
let sortSurvived = false;
let sortFare = false;
let sortId = false;
// sorting button elements
const buttonSortGender = document.getElementById("button-sort-gender");
const buttonSortEmbarked = document.getElementById("button-sort-embarked");
const buttonSortSurvived = document.getElementById("button-sort-survived");
const buttonSortFare = document.getElementById("button-sort-fare");
const buttonSortId = document.getElementById("button-sort-id");
// event listeners for sorting buttons
buttonSortGender.addEventListener("click", e => {
sortGender = !sortGender;
selectButton(e, sortGender);
sortByGender();
});
buttonSortEmbarked.addEventListener("click", e => {
sortEmbarked = !sortEmbarked;
selectButton(e, sortEmbarked);
sortByEmbarked();
});
buttonSortSurvived.addEventListener("click", e => {
sortSurvived = !sortSurvived;
selectButton(e, sortSurvived);
sortbySurvived();
});
buttonSortFare.addEventListener("click", e => {
sortFare = !sortFare;
selectButton(e, sortFare);
sortbyFare();
});
buttonSortId.addEventListener("click", e => {
sortId = !sortId;
selectButton(e, sortId);
sortbyId();
});
// Helper functions for sorting
function sortByGender() {
passengerData.sort((a, b) => (a.sex === "male" ? -1 : 1));
redraw();
}
function sortByEmbarked() {
passengerData.sort((a, b) => {
const x = a.embarked;
const y = b.embarked;
if (x === undefined || y === undefined) {
return 0;
}
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
// names must be equal
return 0;
});
redraw();
}
function sortbySurvived() {
passengerData.sort((a, b) => (a.survived === "Yes" ? -1 : 1));
redraw();
}
function sortbyFare() {
passengerData.sort((a, b) => b.fare - a.fare);
redraw();
}
function sortbyId() {
passengerData.sort((a, b) => b.passengerid - a.passengerid);
redraw();
}
// ********** Display passenger data when passenger block is clicked **********
// Helper to display passenger data
function displayOverlay(data) {
overlay.style.display = 'flex'
overlay.style.justifyContent = 'space-evenly'
overlay.style.alignItems = 'baseline'
overlay.innerHTML = `
<h4> Selected passenger: </h4>
<h4> ${data.name} </h4>
<p> sex: ${data.sex} </p>
<p> embarked: ${data.embarked} </p>
<p> fare: ${data.fare} </p>
<p> survived: ${data.survived} </p>
<p> pclass: ${data.pclass} </p>
`
}
// Helper to hide overlay, needed only with mouseover & mouseout method
// function hideOverlay() {
// overlay.style.display = 'none'
// }
// Event listener on body for selecting
// and displaying selected passenger data
const body = document.querySelector("body");
body.addEventListener("click", e => {
console.log(e.target.dataset.index);
const index = e.target.dataset.index;
if (index !== undefined) {
console.log(passengerData[index])
displayOverlay(passengerData[index])
redraw()
elements[index].style.border = "3px solid black"
}
});
// Tried using mouseover & mouseout.
// This works, but can be glitchy if you move the mouse too quickly or scroll.
// Thus, I've decided to just go with the clicking approach above.
// container.addEventListener('mouseover', (e) => {
// const index = e.target.dataset.index;
// if (index !== undefined) {
// displayOverlay(passengerData[index])
// redraw()
// elements[index].style.border = "3px solid black"
// }
// })
// container.addEventListener('mouseout', (e) => {
// hideOverlay()
// })