Skip to content

Instantly share code, notes, and snippets.

@jeffcatania
Last active March 2, 2016 00:51
Show Gist options
  • Save jeffcatania/1aff961362e9f30b6379 to your computer and use it in GitHub Desktop.
Save jeffcatania/1aff961362e9f30b6379 to your computer and use it in GitHub Desktop.
fresh block
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width:100%; height: 100% }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see!
var props = {
width: 600,
height: 400,
barheight: 10
}
var svg = d3.select("body").append("svg")
var data = [
{ year: 2016, donothing: 90, opportunity: 10 },
{ year: 2017, donothing: 70, opportunity: 30 },
{ year: 2018, donothing: 50, opportunity: 50 }
];
var sectionData = d3.merge(data.map((d,i)=>d3.range(
Math.round(100*(d.donothing)/
(d.donothing+d.opportunity)),
100).map(d2=> ({ set: i, section: d2}))))
var bar = svg.selectAll(".bar")
.data(data)
bar.enter().append("rect")
bar
.attr("class", "bar")
.attr("x", 0)
.attr("y", (d,i)=>i*props.barheight*2)
.attr("height", props.barheight)
.attr("width", d=>props.width * d.donothing/(d.donothing+d.opportunity))
.style("fill", "steelblue")
var unitWidth = props.width / 100
var unit = svg.selectAll(".unit")
.data(sectionData)
unit.enter().append("rect")
.attr("class", "unit")
.attr("fill", "red")
.attr("x", (d,i) => Math.random() > .5 ? Math.random() * 1000 + props.width : -1000 * Math.random() )
.attr("y", (d,i) => Math.random() > .5 ? Math.random() * 1000 + props.height : -1000 * Math.random() )
function redraw() {
unit
.transition().duration(1000).ease("linear")
.attr("x", (d,i) => d.section * unitWidth)
.attr("y", d=>d.set*props.barheight*2)
.attr("width", unitWidth)
.attr("height",props.barheight)
unit
.transition().delay(3000).duration(1000).ease("quad")
.attr("x", (d,i) => Math.random() > .5 ? Math.random() * 1000 + props.width : -1000 * Math.random() )
.attr("y", (d,i) => Math.random() > .5 ? Math.random() * 1000 + props.height : -1000 * Math.random() )
}
setInterval(redraw, 5000)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment