Skip to content

Instantly share code, notes, and snippets.

@jwilber
Created April 6, 2020 18: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 jwilber/6ce2dffe79d2b0bf6b8983e3a706e098 to your computer and use it in GitHub Desktop.
Save jwilber/6ce2dffe79d2b0bf6b8983e3a706e098 to your computer and use it in GitHub Desktop.
fresh block
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; }
.bar {
float: left;
width: 30px;
margin-right: 20px;
background-color: red;
border: 1px solid #C5C5C5;
}
</style>
</head>
<body>
<div id="chart">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<script>
let numbers = [15, 8, 42, 4, 32];
let update = () => {
let selection = d3.select('#chart')
.selectAll('.bar').data(numbers)
.style('height', d => `${d}px`)
.style('margin-top', d => `${100 - d}px`)
selection.enter()
.append('div')
.attr('class', 'bar')
.style('height', d => `${d}px`)
.style('margin-top', d => `${100 - d}px`)
.on('click', (e, i) => {
numbers.splice(i, 1)
update()
})
selection.exit().remove()
}
update()
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment