I'd like to create a javascript function which can take a generic D3 selection, and append duplicates of it to an SVG object.
Here's a minimum working example:
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
svg = d3.select("body").append("svg")
.attr("width", 300)
.attr("height", 300);
circle = svg.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 20)
function clone_selection(x, i) {
for (j = 0; j < i; j++) {
// Pseudo code:
// svg.append(an exact copy of x, with all the attributes)
}
}
clone_selection(circle, 5);
</script>
Mike Bostock said that this was impossible here but that was a while back.
Does anyone have any new thoughts about how this might be achieved? Remember, inside the function clone_selection
we have no idea what svg element(s) is/are in x.