-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace.js
67 lines (67 loc) · 2.06 KB
/
race.js
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
// export default
function race(dataName, lastYear){
d3.json(dataName+'.json').then(function(dat){
let raceChart = c3.generate({
bindto: '#'+dataName,
data: {
json: dat[2000],
keys: {
x: 'name',
value: ["value"]
},
type: 'bar',
names: {
value: 'liczba'
}
},
axis: {
rotated: true,
x: {
type: 'category'
}
},
legend: {
show: false
}
})
const raceChartDiv = document.getElementById(dataName)
const span = document.createElement('span')
raceChartDiv.appendChild(span)
let traceYear = 2000
span.innerText = traceYear
let animation
function changeYear(){
if(!animation){
animation = setInterval(() => {
if( traceYear > lastYear){
traceYear = 2000
}
raceChart.load({
json: dat[traceYear],
keys: {
x: 'name',
value: ["value"]
},
})
span.innerText=traceYear
traceYear++
},5000)
}
}
function stopChangingYear(){
clearInterval(animation)
animation = null
}
const io = new IntersectionObserver((entry, observer) => {
entry = entry[0]
if (entry.intersectionRatio > 0) {
changeYear()
} else {
stopChangingYear()
}
})
io.observe(raceChartDiv)
document.getElementById(dataName + "start").addEventListener("click", changeYear)
document.getElementById(dataName + "stop").addEventListener("click", stopChangingYear)
})
}