Skip to content

Instantly share code, notes, and snippets.

@tizon9804
Last active June 6, 2017 15:35
Show Gist options
  • Save tizon9804/e8c3195ab5aae3e1b494144605c9b6a7 to your computer and use it in GitHub Desktop.
Save tizon9804/e8c3195ab5aae3e1b494144605c9b6a7 to your computer and use it in GitHub Desktop.
Tutorial D3
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; }
</style>
</head>
<body>
<div id="chart"></div>
<script>
data = [100,200,300,400,500]
var svg = d3.select("#chart").append("svg")
.attr("width", 960)
.attr("height", 500)
update (data)
function update(mdata){
//todo add text into each bar
//selection and map data
barra = svg.selectAll("rect").data(mdata);
//update
barra.enter()
.append("rect")
.merge(barra).transition().duration(500)
.attr("x",0)
.attr("y",function(d,i){return i*(50+1)})
.attr("width",function (d){return d})
.attr("height",50)
.style("fill","green")
//remove
barra.exit()
.transition().duration(500)
.remove()
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment