You can append an svg:g element, and then add multiple circles inside.
For example:
var g = vis.append("svg:g").attr("transform", "translate(20,20)");
g.append("svg:circle").attr("r", 10);
g.append("svg:circle").attr("r", 20);
g.append("svg:circle").attr("r", 30);
Mike
I was thinking more like a kind-of-class way to go. My idea was to
first build a generic "circles" object, than derive different objects
from that and finally add that objects to the data
var circles = /* some magic happening */
var circleType1 = /* some magic happening */(circles)
var circleType2 = /* some magic happening */(circles)
vis.selectAll(".empty").data(Data1).enter().append(circleType1); (or
.circleType1();)
vis.selectAll(".empty").data(Data2).enter().append(circleType2); (or
.circleType2();)
vis.selectAll(".empty").data(Data3).enter().append(circles); (or .circles();)
I hope now my idea is somewhat clearer.
2011/7/1 Mike Bostock <mbos...@cs.stanford.edu>:
--
Mate ist gesunder Schlaf in Halbliterflaschen
vis.selectAll(".empty")
.data(data)
.enter().append("svg:circle")
While it works to select an empty selection as ".empty", it's more
idiomatic if you declare what you are about to add. This way, you know
you have a way to select those elements again if you need to. For
example, you might say:
vis.selectAll(".type1")
.data(data)
.enter().append("svg:circle")
.attr("class", "type1")
What you're describing sounds a lot like D3's chart templates, so you
might take a look at how they are implemented. A very simple way of
reusing shape templates is to put your code inside a function, and
then call that function whenever you want to instantiate the template.
For example, if I wanted a template that added three circles to a
given container:
function addCircles(g) {
g.selectAll("circle")
.data([10, 20, 30])
.enter().append("svg:circle")
.attr("r", function(d) { return d; });
}
Note that this particular function will do nothing if the specified
`g` selection already contains three circles, but you can easily
change the implementation to handle update and exit. To use the
function, you can say:
addCircles(vis.select("g"));
Or, you can use the `call` operator, which is equivalent and supports
method chaining:
vis.select("g").call(addCircles);
Mike