Built with blockbuilder.org
Last active
June 25, 2017 22:10
-
-
Save areologist/bcfaa705b643e66485605131af260647 to your computer and use it in GitHub Desktop.
select: update, enter, exit, merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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