-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
179 lines (159 loc) · 4.19 KB
/
script.js
File metadata and controls
179 lines (159 loc) · 4.19 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
import data from './titanic-data.js'
// Color variables for 'Embarked' data
const portColor = {
S: "tomato",
C: "cornflowerblue",
Q: "orange",
undefined: "green",
total: 'gray'
}
// Get a reference to the #titanic
const titanic = document.querySelector('#titanic');
// Get a reference to the #dataDisplay caption
// const displayData = document.querySelector('#data');
// Set some styles on the titanic
// display flex, justifyContent center, alignItems flex-end
titanic.style.display = 'grid';
titanic.style.gridTemplateColumns = 'repeat(30, 30px)';
titanic.style.gridGap = '1px';
// Main DOM Element
// Map over the data and make a new element for each passenger
const passengers = data.map(p => {
const el = document.createElement('div')
titanic.appendChild(el)
el.style.transition = "400ms"
return el
});
drawDiagram()
// Main Diagram
function drawDiagram() {
passengers.forEach((p, i) => {
const { survived, sex, embarked, pclass } = data[i].fields
p.style.width = '30px'
p.style.height = '30px'
p.style.opacity = survived === 'Yes' ? '100%' : '15%'
p.style.borderRadius = sex === 'female' ? '50%' : '0'
p.style.backgroundColor = portColor[embarked]
})
}
// Bar Chart DOM Element
const titanicEmbarked = document.querySelector('#titanic-embarked');
// Reduce undefined
const embarkedCounts = data.reduce((acc, p) => {
if (acc[p.fields.embarked] === undefined) {
acc[p.fields.embarked] = 1
} else {
acc[p.fields.embarked] += 1
}
return acc
}, {});
// Bar Total
embarkedCounts.total = data.length
// Bar Chart
const embarkedKeys = Object.keys(embarkedCounts)
embarkedKeys.forEach((e) => {
const el = document.createElement('div')
titanicEmbarked.appendChild(el)
el.style.width = '30px'
const count = embarkedCounts[e]
const percent = count / data.length * 100
el.style.height = `${percent}%`
el.style.backgroundColor = portColor[e]
el.style.margin = '1px'
});
// Bar Chart Styles
titanicEmbarked.style.display = 'flex';
// titanicEmbarked.style.flexDirection = 'column';
titanicEmbarked.style.alignItems = 'flex-end';
titanicEmbarked.style.position = 'fixed'
titanicEmbarked.style.left = '1%';
titanicEmbarked.style.top = '5.5%';
titanicEmbarked.style.border = '1px dotted white';
titanicEmbarked.style.width = '200px';
titanicEmbarked.style.height = '300px';
// Reset Diagram
document.body.addEventListener('click', (e) => {
if (e.target.matches('#reset')) {
document.location.reload(true)
}
})
// Sort Survived
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortSurvived')) {
data.sort((a, b) => {
if (a.fields.survived === 'Yes') {
return -1
}
return 1
});
drawDiagram()
}
});
// Sort by Gender
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortGender')) {
data.sort((a, b) => {
if (a.fields.sex === 'female') {
return -1
}
return 1
});
drawDiagram()
}
});
// Sort by Embarkation
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortEmbarked')) {
data.sort((a, b) => {
if (a.fields.embarked < b.fields.embarked) {
return -1
} else if (a.fields.embarked > b.fields.embarked) {
return 1
}
return 0
});
drawDiagram()
}
});
// Sort by Age
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortAge')) {
data.sort((a, b) => {
if (a.fields.age < b.fields.age) {
return -1
} else if (a.fields.age > b.fields.age) {
return 1
}
return 0
});
drawDiagram()
}
});
// Sort by Class
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortClass')) {
data.sort((a, b) => {
if (a.fields.pclass < b.fields.pclass) {
return -1
} else if (a.fields.pclass > b.fields.pclass) {
return 1
}
return 0
});
drawDiagram()
}
});
// Sort by Fare
document.body.addEventListener('click', (e) => {
if (e.target.matches('#sortFare')) {
data.sort((a, b) => {
if (a.fields.fare < b.fields.fare) {
return -1
} else if (a.fields.fare > b.fields.fare) {
return 1
}
return 0
});
drawDiagram()
}
});