Skip to content

Commit

Permalink
race module
Browse files Browse the repository at this point in the history
  • Loading branch information
codestance committed Feb 11, 2022
1 parent 876763d commit 0c08f6a
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions race.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export default function(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'
},
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)
})
}

0 comments on commit 0c08f6a

Please sign in to comment.