Created
April 7, 2019 17:42
-
-
Save matt-oxley/f8f149177749c45aac85a41aaecc29f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8" /> | |
<body> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<script> | |
const height = 500; | |
const width = 200; | |
const svg = d3 | |
.select("body") | |
.style("text-align", "center") | |
.append("svg") | |
.style("border", "1px solid black") | |
.attr("width", 200) | |
.attr("height", height); | |
setInterval(() => { | |
const data = Array(Math.floor(Math.random() * 10)) | |
.fill(true) | |
.map(() => Math.random() * 10); | |
console.log("data: ", data); | |
const yScale = d3 | |
.scaleLinear() | |
.domain([0, d3.max(data)]) | |
.range([0, height]); | |
const barWidth = width / data.length; | |
const binding = svg.selectAll("rect").data(data); | |
binding | |
.enter() | |
.append("rect") | |
.merge(binding) | |
.attr("height", d => yScale(d)) | |
.attr("width", barWidth) | |
.attr("x", (d, i) => i * barWidth) | |
.attr("y", d => height - yScale(d)) | |
.attr("stroke", "white") | |
.attr("fill", "steelblue"); | |
}, 1000); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment