Skip to content

Instantly share code, notes, and snippets.

@lnmunhoz
Last active February 22, 2019 02:34
Show Gist options
  • Save lnmunhoz/f4be2278d82aa3e96e9ec8465c026b82 to your computer and use it in GitHub Desktop.
Save lnmunhoz/f4be2278d82aa3e96e9ec8465c026b82 to your computer and use it in GitHub Desktop.
learning d3: displaying data
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>
<script>
const buildingsData = [
{
"name": "Burj Khalifa",
"height": "350"
},
{
"name": "Shanghai Tower",
"height": "263.34"
},
{
"name": "Abraj Al-Bait Clock Tower",
"height": "254.04"
},
{
"name": "Ping An Finance Centre",
"height": "253.20"
},
{
"name": "Lotte World Tower",
"height": "230.16"
}
]
var svg = d3.select("body").append("svg")
.attr("width", 400)
.attr("height", 400)
.style("border", "1px solid black")
const rects = svg.selectAll("rect").data(buildingsData)
rects.enter().append("rect")
.attr("x", (d, i) => 70 * i)
.attr("y", 0)
.attr("width", 50)
.attr("height", (d, i) => d.height)
.attr("fill", "gray")
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment