Published
Edited
Nov 25, 2020
1 star
Insert cell
Insert cell
{
const div = d3.create('div').attr('id', 'viz');

while (true) {
const children = div.selectAll('.children').data(randomiseData());

const enter = children
.enter()
.append('div')
.attr('class', 'children')
.style('width', d => `${d.w}%`)
.style('transition', 'width 0.5s')
.style('border-width', (d, i) => computeBorder(d, i))
.style('background-color', d => d.color);

const exit = children.exit().remove();

children.merge(children).style('width', d => `${d.w}%`);

yield div.node();
await Promises.tick(1000);
}
}
Insert cell
Insert cell
computeBorder = (d, i) => {
switch (i) {
case 0: // the first bar has only ever border-right...
return '0 1px 0 0';
break;
case d.length - 1: // the last one only ever border-left
return '0 0 0 1px';
break;
default: // and all the others: borders left and right
return '0 1px 0 1px';
}
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more