Skip to content

Instantly share code, notes, and snippets.

@areologist
Last active June 25, 2017 22:10
Show Gist options
  • Save areologist/bcfaa705b643e66485605131af260647 to your computer and use it in GitHub Desktop.
Save areologist/bcfaa705b643e66485605131af260647 to your computer and use it in GitHub Desktop.
select: update, enter, exit, merge
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<div class="chart">
<div>Billy</div>
<div>Cindy</div>
<div>Walter</div>
</div>
<script>
const scores = [
{ name: 'Alice', score: 96 },
{ name: 'Billy', score: 83 },
{ name: 'Cindy', score: 91 },
{ name: 'David', score: 96 },
{ name: 'Emily', score: 88 }
];
const update = d3.select('.chart')
.selectAll('div')
.data(scores, (d, i, n) =>
d ? d.name : n[i].innerText
)
.style('color', 'blue');
const enter = update.enter()
.append('div')
.text(d => d.name)
.style('color', 'green');
const exit = update.exit()
.style('color', 'red');
update.merge(enter)
.style('font-style', 'italic');
update.merge(exit)
.style('font-weight', 'bold');
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment