Built with blockbuilder.org
forked from lnmunhoz's block: learning d3: displaying data
license: mit |
Built with blockbuilder.org
forked from lnmunhoz's block: learning d3: displaying data
<!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 y = d3.scaleLinear() | |
.domain([0, 350]) // size of the tallest building | |
.range([0, 400]) // height of the svg container | |
const rects = svg.selectAll("rect").data(buildingsData) | |
rects.enter().append("rect") | |
.attr("x", (d, i) => y(70 * i)) | |
.attr("y", 0) | |
.attr("width", 50) | |
.attr("height", (d, i) => y(d.height)) | |
.attr("fill", "gray") | |
</script> | |
</body> |