Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3indepth/e25e81566cea413de9873b9339fa7518 to your computer and use it in GitHub Desktop.
Save d3indepth/e25e81566cea413de9873b9339fa7518 to your computer and use it in GitHub Desktop.
General update pattern
license: gpl-3.0
height: 120
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>General update pattern</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
#menu {
margin: 10px;
}
#content div {
display: inline-block;
margin: 2px;
background-color: orange;
color: white;
padding: 8px;
width: 14px;
height: 14px;
text-align: center;
}
</style>
<body>
<div id="menu">
<button onClick="doUpdate();">Update</button>
</div>
<div id="content">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function doUpdate() {
var rand = Math.floor( Math.random() * 26 );
var myData = letters.slice(0, rand).split('');
update(myData);
}
function update(data) {
var u = d3.select('#content')
.selectAll('div')
.data(data);
u.enter()
.append('div')
.merge(u)
.text(function(d) {
return d;
});
u.exit().remove();
}
doUpdate();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment