Repository navigation
Expand file tree
/
Copy pathpopulation.js
More file actions
148 lines (123 loc) · 3.51 KB
/
Copy pathpopulation.js
File metadata and controls
148 lines (123 loc) · 3.51 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
d3.csv("population_filtered_by_investment.csv").then((data) => {
data.forEach((d) => {
d.A50A = +d.A50A;
d.A50B = +d.A50B;
d.A41 = +d.A41;
d.A8 = +d.A8_2021;
});
const width = 800;
const height = 600;
const padding = 20;
const svg = d3
.select("#population_chart")
.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "2px solid #ccc");
let circles;
function getSelectedFilters() {
const filters = {
gender: [],
age: [],
income: [],
education: [],
};
const genderRadio = document.querySelector('input[name="gender"]:checked');
if (genderRadio) filters.gender.push(+genderRadio.value);
for (let i = 1; i <= 6; i++) {
const ageRadio = document.querySelector(`input[name="age${i}"]:checked`);
if (ageRadio) {
filters.age.push(+ageRadio.value);
}
}
for (let i = 1; i <= 10; i++) {
const incomeRadio = document.querySelector(
`input[name="income${i}"]:checked`
);
if (incomeRadio) {
filters.income.push(+incomeRadio.value);
}
}
for (let i = 1; i <= 6; i++) {
const educationRadio = document.querySelector(
`input[name="education${i}"]:checked`
);
if (educationRadio) {
filters.education.push(+educationRadio.value);
}
}
return filters;
}
function matchesFilters(d, filters) {
const hasAnyFilter =
filters.gender.length > 0 ||
filters.age.length > 0 ||
filters.income.length > 0 ||
filters.education.length > 0;
if (!hasAnyFilter) {
return true;
}
if (filters.gender.length > 0 && !filters.gender.includes(d.A50A)) {
return false;
}
if (filters.age.length > 0 && !filters.age.includes(d.A50B)) {
return false;
}
if (filters.education.length > 0 && !filters.education.includes(d.A41)) {
return false;
}
if (filters.income.length > 0 && !filters.income.includes(d.A8)) {
return false;
}
return true;
}
function updateColors() {
const filters = getSelectedFilters();
// Count how many items match the filters
const matchCount = data.filter((d) => matchesFilters(d, filters)).length;
circles.attr("fill", (d, i) => {
return i < matchCount ? "#8b0000" : "#808080";
});
}
circles = svg
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", (d, i) => {
const cols = Math.ceil(Math.sqrt(data.length));
const col = i % cols;
return (col / cols) * (width - 2 * padding) + padding;
})
.attr("cy", (d, i) => {
const cols = Math.ceil(Math.sqrt(data.length));
const row = Math.floor(i / cols);
const rows = Math.ceil(data.length / cols);
return (row / rows) * (height - 2 * padding) + padding;
})
.attr("r", 2)
.attr("fill", "#8b0000")
.attr("opacity", 0.6);
setTimeout(() => {
const allRadioButtons = document.querySelectorAll('input[type="radio"]');
allRadioButtons.forEach((radio) => {
let wasCheckedBefore = false;
radio.addEventListener("mousedown", (e) => {
wasCheckedBefore = radio.checked;
updateColors();
});
radio.addEventListener("click", (e) => {
if (wasCheckedBefore) {
setTimeout(() => {
radio.checked = false;
updateColors();
}, 10);
}
});
radio.addEventListener("change", () => {
updateColors();
});
});
updateColors();
}, 100);
});