chartSeparateTransitions = {
const enterBtn = document.getElementById('enter-btn-separate-transitions')
const updateBtn = document.getElementById('update-btn-separate-transitions')
const returnBtn = document.getElementById('return-btn-separate-transitions')
const customElastic = d3.easeElastic.period(0.6);
const width = 600
const height = 600
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
const drawGraph = (data) => {
const rects = svg.selectAll('circle')
.data(data, d => d.id)
.join(
enter => {
return enter.append('circle')
.attr('cx', 0)
.attr('cy', (d, i) => 100 + i*80)
.style('stroke-width', 0)
.style('fill', d => colourScale[d.category])
.transition().duration(1000).ease(customElastic)
.attr('cx', 300)
.attr('r', d => d.value)
},
update => {
return update
//.attr('cx', 300)
// 👇🏻 if you set the colour here you won't get a transition from the exit value of black
//.style('fill', d => colourScale[d.category])
.style('stroke-width', 3) // stroke will flash just for a second on update
.transition().duration(2000)
// 👇🏻 500 -> 300 here or 300 -> 300 i.e. no transition if cx is set above for elements
// coming back from having exited
.attr('cx', 300)
// 👇🏻 black -> colour here or no colour transition if fill is set above for elements
// coming back after exiting; no effect on elements which are only changing other properties
.style('fill', d => colourScale[d.category])
// 👇🏻 if this is set then the circles will shift up based on index whenever some
// of the circles exit; if this is not set then there will be gaps for missing circles
.attr('cy', (d, i) => 100 + i*80)
.attr('r', d => d.value)
.style('stroke-width', 0) // stroke will disappear just after flashing on update
},
exit => {
exit
// Position and properties that we will animate *to* for the exiting elements
.transition().duration(1000)
.attr('r', 0) // radius shrinks on exiting elements
.style('fill', '#000') // fill becomes black as the element shrinks
.attr('cx', 500) // element moves to the right as it disappears
}
) // Only add constant values that even change on enter update or exit below
.style('stroke', '#000')
// Add text so we can see the elements
const text = svg
.selectAll("text")
.data(data, d => d.id)
.join('text')
.attr("x", 180)
.attr("dy", "0.35em")
.text(d => d.category)
.style('fill', d => colourScale[d.category])
.style('font-size', '20px')
.transition().duration(1000)
.attr("y", (d, i) => 100 + i*80)
}
drawGraph(initialData)
// Simulate an enter pattern
enterBtn.addEventListener('click', () => {
// Remove the elements before we draw again so we can see the enter pattern
d3.selectAll('circle').remove()
d3.selectAll('text').remove()
drawGraph(initialData)
})
updateBtn.addEventListener('click', () => {
drawGraph(changedData)
})
returnBtn.addEventListener('click', () => {
drawGraph(initialData)
})
return svg.node();
}