{
const rectWidth = 50
const svgHeight = 100
const svg = html`<svg height=${svgHeight} width='500' style='overflow: visible' />`
const code = html`<code />`
const button = html`<button>new data!</button>`
function updateBars() {
const t1 = d3.select(svg).transition().duration(2000)
const t2 = d3.select(svg).transition().duration(3000)
const t3 = d3.select(svg).transition().duration(1000)
const data = _.times(_.random(1, 10), i => _.random(0, 100))
d3.select(svg).selectAll('rect').data(data, d=>d).join(
enter => {
d3.select('#c1').text(_.values(enter._groups[0]))
return enter.append('rect')
.attr('y', svgHeight)
.attr('x', (d, i) => i * rectWidth)
.transition(t1)
.attr('y', d=>svgHeight-d)
.attr('height', d=>d)
.attr('fill', 'yellow')
},
update => {
d3.select('#c2').text(_.values(update._groups[0]))
return update
.attr('height', d=>d)
.transition(t2)
.attr('x', (d, i) => i * rectWidth)
.attr('fill', 'blue')
},
exit => {
d3.select('#c3').text(_.values(exit._groups[0]))
exit
.transition(t3)
.attr('fill-opacity', 1)
.attr('fill', 'red')
.remove()
}
)
.attr('width', rectWidth)
.attr('fill-opacity', 0.3)
.attr('stroke-width', 2)
.attr('stroke', 'gray')
d3.select(code).text(JSON.stringify(data).replace(/\,/g, ', '))
}
updateBars()
d3.select(button).on('click', updateBars)
return html`
${svg}
<p>
${button} ${code}
</p>
<p>
Enter:<br/>
<code id='c1'/>
</p>
<p>
Update:<br/>
<code id='c2'/>
</p>
<p>
Exit:<br/>
<code id='c3'/>
</p>`
}