Skip to content

Commit 242e66f

Browse files
committed
girls' names race
1 parent 86da183 commit 242e66f

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

index.html

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,20 @@
99
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
1010
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/css/select2.min.css" rel="stylesheet" />
1111
<style>
12+
body{
13+
font-size: 16px;
14+
}
1215
.c3-line{
1316
stroke-width: 2px;
1417
}
18+
#raceW span{
19+
position: absolute;
20+
bottom: 1.5em;
21+
right: 2em;
22+
font-size: 3em;
23+
font-weight: bolder;
24+
color: lightslategray;
25+
}
1526
</style>
1627
</head>
1728
<body class="container-fluid">
@@ -70,8 +81,7 @@ <h3 id="instruction">Jak używać</h3>
7081
<!-- <div id="namesNew"></div> -->
7182
<br/>
7283
<h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
73-
<div id="raceW">
74-
<span id="year"></span>
84+
<div id="raceW" class="position-relative">
7585
</div>
7686

7787
</div>
@@ -204,7 +214,7 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
204214
})
205215

206216
d3.csv('https://raw.githubusercontent.com/codestance/babyNames/dev/raceW.csv').then(function(dat){
207-
// console.log(dat)
217+
208218
function filterData(year){
209219
let filtered = dat.filter(obj => +obj[year]!==0)
210220
filtered.sort((a, b) => b[year] - a[year]);
@@ -216,7 +226,6 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
216226
names.unshift('x')
217227
return names
218228
}
219-
// console.log('filtered',filterData(2000))
220229

221230
function valuesInYear(year){
222231
let datValues = filterData(year).map(d => +d[year])
@@ -225,47 +234,64 @@ <h3>Ranking najpopularniejszych imion wg kolejnych lat</h3>
225234
}
226235

227236
let raceChart = c3.generate({
228-
bindto: '#raceW',
229-
data: {
230-
x: 'x',
231-
xFormat: '%Y',
232-
columns: [
233-
namesInYear(2000),
234-
valuesInYear(2000)
235-
],
236-
type: 'bar'
237-
},
238-
order: 'desc',
239-
axis: {
240-
rotated: true,
241-
x: {
242-
type: 'category',
237+
bindto: '#raceW',
238+
data: {
239+
x: 'x',
240+
xFormat: '%Y',
241+
columns: [
242+
namesInYear(2000),
243+
valuesInYear(2000)
244+
],
245+
names: {
246+
value: 'liczba'
247+
},
248+
type: 'bar'
249+
},
250+
order: 'desc',
251+
axis: {
252+
rotated: true,
253+
x: {
254+
type: 'category',
255+
}
256+
},
257+
legend: {
258+
show: false
259+
}
260+
});
261+
const raceChartDiv = document.getElementById('raceW')
262+
const span = document.createElement('span')
263+
raceChartDiv.appendChild(span)
264+
index = 0
265+
span.innerText=years[index]
266+
const io = new IntersectionObserver((entry, observer) => {
267+
entry = entry[0]
268+
let animation
269+
if (entry.intersectionRatio > 0) {
270+
animation =
271+
setInterval(() => {
272+
index++
273+
if( index < years.length){
274+
raceChart.load({
275+
columns: [
276+
namesInYear(years[index]),
277+
valuesInYear(years[index])
278+
]
279+
})
280+
span.innerText=years[index]
281+
}
282+
else{
283+
index = 0
284+
}
285+
},4000)
286+
287+
} else {
288+
clearInterval(animation)
243289
}
244-
}
245-
});
246-
247-
setTimeout(function(){
248-
raceChart.load({
249-
columns: [
250-
namesInYear(2001),
251-
valuesInYear(2001)
252-
],
253290
})
254-
},5000)
291+
io.observe(raceChartDiv)
255292

256293
})
257294

258-
259-
260-
261-
// setTimeout(function () {
262-
// race.load({
263-
// columns: [
264-
// ['value', 11000, 250, 200]
265-
// ]
266-
// });
267-
// }, 10000);
268-
269295
</script>
270296
</body>
271297
<footer>

0 commit comments

Comments
 (0)