-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
227 lines (202 loc) · 6.86 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bubble Chart - Gender vs Human Development Index</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
}
.tooltip {
position: absolute;
text-align: center;
width: 120px;
padding: 8px;
background: lightgray;
border: 1px solid #ccc;
border-radius: 4px;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s ease-in-out;
}
.play-button {
display: inline-block;
width: 60px; /* Set the button size */
height: 60px; /* Set the button size */
background: #333;
color: white;
cursor: pointer;
border: none;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
transition: background 0.3s ease-in-out;
position: relative;
margin-left: 20px; /* Adjust the margin to align with the timeline */
vertical-align: middle;
}
.play-button.playing {
background: green;
}
.play-button:before {
content: "";
display: inline-block;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 20px solid white;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(270deg); /* Rotate the triangle 90 degrees */
transition: transform 0.3s ease-in-out;
}
.timeline {
list-style: none;
padding: 0;
margin: 0;
display: inline-block;
vertical-align: middle;
}
.timeline li {
display: inline-block;
padding: 10px;
background: #ddd;
border-radius: 4px;
cursor: pointer;
height: 40px; /* Match the height of the play button */
line-height: 40px; /* Adjust to center text vertically */
}
.timeline li:hover {
background: #bbb;
}
.timeline li.active {
background: #333;
color: white;
}
</style>
</head>
<body>
<h2>Gender Development Index vs Human Development Index (2012-2022)</h2>
<ul class="timeline">
<li>2012</li>
<li>2013</li>
<li>2014</li>
<li>2015</li>
<li>2016</li>
<li>2017</li>
<li>2018</li>
<li>2019</li>
<li>2020</li>
<li>2021</li>
<li>2022</li>
</ul>
<button class="play-button"></button>
<svg width="960" height="600"></svg>
<div class="tooltip"></div>
<script>
d3.csv("filtered_data").then(function(data) {
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
const margin = {top: 100, right: 100, bottom: 100, left: 100};
const chartWidth = width - margin.left - margin.right,
chartHeight = height - margin.top - margin.bottom;
const chartArea = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const xScale = d3.scaleLinear().range([0, chartWidth]);
const yScale = d3.scaleLinear().range([chartHeight, 0]);
const sizeScale = d3.scaleSqrt().range([4, 40]);
const xAxis = d3.axisBottom(xScale);
const yAxis = d3.axisLeft(yScale);
const tooltip = d3.select(".tooltip");
let currentYear = 2012;
let playing = false;
let animation;
function updateChart(year) {
const filteredData = data.filter(d => d.Year == year);
const continents = [...new Set(filteredData.map(d => d.Continent))];
const colorScale = d3.scaleOrdinal()
.domain(continents)
.range(d3.schemeCategory10);
xScale.domain(d3.extent(filteredData, d => +d["Human Development Index"])).nice();
yScale.domain(d3.extent(filteredData, d => +d["Gender Development Index"])).nice();
sizeScale.domain(d3.extent(filteredData, d => +d["Population"]));
const bubbles = chartArea.selectAll(".bubble")
.data(filteredData, d => d.Entity);
bubbles.exit().remove();
const bubblesEnter = bubbles.enter().append("circle")
.attr("class", "bubble")
.attr("cx", d => xScale(d["Human Development Index"]))
.attr("cy", d => yScale(d["Gender Development Index"]))
.attr("r", d => sizeScale(d["Population"]))
.style("fill", d => colorScale(d.Continent))
.style("opacity", 0.7)
.on("mouseover", (event, d) => {
tooltip.transition().duration(200).style("opacity", .9);
tooltip.html(`${d.Entity}<br>HDI: ${d["Human Development Index"]}<br>GDI: ${d["Gender Development Index"]}<br>Population: ${d.Population}`)
.style("left", (event.pageX + 5) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", d => {
tooltip.transition().duration(500).style("opacity", 0);
});
bubblesEnter.merge(bubbles)
.transition()
.duration(1000)
.attr("cx", d => xScale(d["Human Development Index"]))
.attr("cy", d => yScale(d["Gender Development Index"]))
.attr("r", d => sizeScale(d["Population"]))
.style("fill", d => colorScale(d.Continent));
chartArea.selectAll(".axis").remove();
chartArea.append("g")
.attr("class", "x axis")
.attr("transform", `translate(0,${chartHeight})`)
.call(xAxis);
chartArea.append("g")
.attr("class", "y axis")
.call(yAxis);
chartArea.append("text")
.attr("class", "x axis-label")
.attr("x", chartWidth / 2)
.attr("y", chartHeight + 40)
.style("text-anchor", "middle")
.text("Human Development Index");
chartArea.append("text")
.attr("class", "y axis-label")
.attr("x", -chartHeight / 2)
.attr("y", -40)
.style("text-anchor", "middle")
.text("Gender Development Index")
.attr("transform", "rotate(-90)");
d3.select(".timeline li.active").classed("active", false);
d3.select(`.timeline li:nth-child(${year - 2011})`).classed("active", true);
}
updateChart(currentYear);
d3.select(".play-button").on("click", function() {
playing = !playing;
d3.select(this).classed("playing", playing);
if (playing) {
animation = d3.interval(() => {
currentYear++;
if (currentYear > 2022) {
currentYear = 2012;
}
updateChart(currentYear);
}, 1000);
} else {
animation.stop();
}
});
d3.select(".timeline").selectAll("li").on("click", function() {
const selectedYear = parseInt(this.innerHTML);
currentYear = selectedYear;
updateChart(currentYear);
});
}).catch(function(error) {
console.error("Error loading the CSV file:", error);
});
</script>
</body>
</html>