Skip to content

Instantly share code, notes, and snippets.

@aogriffiths
Created June 8, 2012 21:31
Show Gist options
  • Save aogriffiths/2898199 to your computer and use it in GitHub Desktop.
Save aogriffiths/2898199 to your computer and use it in GitHub Desktop.
d3 transitions
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>2898199-d3 transitions</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
Demonstrates some simple d3 transitions
see it more clearly at http://bl.ocks.org/2898199
<html lang="en">
<head>
<title>D3 Transitions</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- JS -->
<!--
<script type="text/javascript" src="https://raw.github.com/mbostock/d3/master/d3.min.js"></script>
-->
<script type="text/javascript" src="../2902093/d3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="vis"></div>
<script type="text/javascript">
main();
</script>
</body>
</html>
function main() {
var svg = d3.select("#vis").append("svg")
.attr("width", 800)
.attr("height", 600);
var data =
[
{'vert':1, 'size':2,'name':'One', 'colour':'red'},
{'vert':2, 'size':5,'name':'Two', 'colour':'green'},
{'vert':3, 'size':3,'name':'Three', 'colour':'orange'}
];
var topg = svg.append("g")
.attr("transform", "rotate(270)translate(-120,0)");
var g = topg.selectAll("g.item")
.data(data);
g.enter()
.append("g")
.attr("class", 'item');
g
.attr("transform", function(d){
return "translate(" + 0 + "," + d.vert * 25 +")";
});
var size = function(d){return d.size * 20;};
var t1 = 2000;
var t2 = 4000;
var r1 = g.select("rect.r1");
if(r1.empty()) r1 = g
.append("rect")
.attr("class", 'r1');
r1
.attr("fill", "lightblue")
.attr("stroke-width", 0)
.attr("height", 20)
.attr("width", 0);
r1
.transition()
.duration(t1+t2)
.attr("fill", function(d){return d.colour})
.transition()
.duration(t1)
.ease("bounce")
.attr("width", size);
var r2 = g.select("rect.r2");
if(r2.empty()) r2 = g
.append("rect")
.attr("class", 'r2');
r2
.attr("fill", "darkred")
.attr("height", 20)
.attr("width", 3)
.attr("stroke-width", 0)
r2
.transition()
.duration(t1)
.ease("bounce")
.attr("x", size);
r2
.transition()
// .ease("back")
.delay(t1)
.duration(t2)
.attr("x", 0);
g.append("text")
.attr("dy", 15)
.attr("dx", 5)
.text(function(d){return d.name});
}
{"rawdata":[{
"Capability":"Manage Customer Calls",
"Project":"Project A",
"Type":"Customer Service",
"Year":"2011"
},{
"Capability":"R&D Lab",
"Project":"Project B",
"Type":"Enabler",
"Year":"2011"
},{
"Capability":"Telesales",
"Project":"Project A",
"Type":"Revenue Generation",
"Year":"2012"
},{
"Capability":"CRM System",
"Project":"Project A",
"Type":"Enabler",
"Year":"2012"
},{
"Capability":"Home Delivery",
"Project":"Project C",
"Type":"Enabler",
"Year":"2013"
},{
"Capability":"High Street Shops",
"Project":"Project C",
"Type":"Enabler",
"Year":"2012"
}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment