-
-
Save denisemauldin/977dc65d13acf24f7b86bbf2d14eb384 to your computer and use it in GitHub Desktop.
defs in d3 - reuse elements with use
This file contains hidden or 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
<html> | |
<head> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.4.1/d3.min.js"></script> | |
</head> | |
<body> | |
<svg></svg> | |
</body> | |
<script> | |
var circle_data = [ | |
[10,70,10], | |
[20,25,10], | |
[70,35,10], | |
[50,45,20], | |
[20,55,20], | |
[30,45,25] | |
]; | |
var svg = d3.select("svg").attr("width","100%").attr("height",500); | |
var cloudDef = svg.append('defs').append("g").attr("id","cloud").style("opacity", 0.5); | |
cloudDef.selectAll("circle").data(circle_data).enter() | |
.append("circle") | |
.attr("cx", function(d){return d[0]}) | |
.attr("cy", function(d){return d[1]}) | |
.attr("r", function(d){return d[2]}) | |
.style("fill", "steelblue") | |
svg.append("g").attr("transform","translate(0,0)").attr("class","cloud") | |
.append("use").attr("xlink:href","#cloud") | |
svg.append("g").attr("transform","translate(100,0)").attr("class","cloud") | |
.append("use").attr("xlink:href","#cloud") | |
svg.append("g").attr("transform","translate(200,0)").attr("class","cloud") | |
.append("use").attr("xlink:href","#cloud") | |
setInterval( function(){ | |
d3.selectAll("g.cloud").transition() | |
.attr("transform", function(){ | |
return "translate(" + Math.random() * 200 + ", 0)" | |
}) | |
}, 1000) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment