Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@robert-moore
Created February 17, 2018 00:56
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 robert-moore/bbaff6473fae658a2b84f931279ad273 to your computer and use it in GitHub Desktop.
Save robert-moore/bbaff6473fae658a2b84f931279ad273 to your computer and use it in GitHub Desktop.
SVG Bar Chart
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<!-- <svg width="500" height="500">
<rect x="0" y="0" width="500" height="500" fill="lightcoral" />
<circle cx="250" cy="250" r="250" fill="crimson" />
</svg> -->
<script>
const milesRun = [3,2,5,8,3]
const svg = d3.select("body")
.append("svg")
.attr("height", 400)
.attr("width", 600)
const bars = svg.selectAll("rect")
.data(milesRun)
.enter()
.append("rect")
bars
.attr("x", 0)
.attr("y", (d, i) => i*40)
.attr("width", d => d*50)
.attr("height", 32)
.style("fill", "crimson")
bars
.append("title")
.text((d, i) => "You ran " + d + " miles on day " + (i + 1))
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment