Skip to content

Instantly share code, notes, and snippets.

@mukhtyar
Last active January 10, 2018 20:20
Show Gist options
  • Save mukhtyar/1e6652ca51b77c78e4567874137ebc01 to your computer and use it in GitHub Desktop.
Save mukhtyar/1e6652ca51b77c78e4567874137ebc01 to your computer and use it in GitHub Desktop.
D3 - Data Binding 1
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; }
#chart {
margin: 3em;
}
.bar {
background-color: blue;
height: 20px;
margin-top: 2px;
}
</style>
</head>
<body>
<div id="chart">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<script>
var data = [200, 60, 100, 80, 10];
// Pair each number in the array with an empty div
// Set color, height, and spacing via CSS
d3.select('#chart')
.selectAll('div')
.data(data)
.attr('class', 'bar')
.style('width', function(d) {
return d + 'px';
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment