Skip to content

Instantly share code, notes, and snippets.

@mukhtyar
Last active January 10, 2018 20:40
Show Gist options
  • Select an option

  • Save mukhtyar/56405be0691ebeff058ce91804cab001 to your computer and use it in GitHub Desktop.

Select an option

Save mukhtyar/56405be0691ebeff058ce91804cab001 to your computer and use it in GitHub Desktop.
D3 - Enter Selection
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>
<svg id="chart"></svg>
<script>
var rectWidth = 50;
var chartHeight = 400;
var chartWidth = 700;
var data = [200, 60, 100, 80, 10];
var svg = d3.select('#chart')
.attr('height', chartHeight)
.attr('width', chartWidth);
svg.selectAll('rect')
.data(data)
.enter().append('rect') // new line
.attr('x', function(d, i){
return i * rectWidth;
})
.attr('y', function(d){
return chartHeight - d;
})
.attr('width', rectWidth)
.attr('height', function(d){
return d;
})
.attr('fill', 'blue')
.attr('stroke', '#FF00FF')
.attr('stroke-width', '2');
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment