Monday, 26 August 2013

How to create "svg" object without appending it?

How to create "svg" object without appending it?

Consider the following code:
var svg = d3.select('#somediv').append("svg").attr("width",
w).attr("height", h);
I would like to refactor this code so that it reads more like this:
var svg = makesvg(w, h);
d3.select("#somediv").append(svg);
Note that, in contrast to the situation shown in the first version, in
this second version append does not create the "svg" object; it only
appends it to d3.select("#somediv").
The problem is how to implement the function makesvg. This in turn reduces
to the problem: how to instantiate an "svg" object without using append to
do this, since one could then do something like:
function makesvg(width, height) {
return _makesvg().attr("width", w).attr("height", h);
}
So my question boils down to what is the generic equivalent of the
hypothetical _makesvg() factory mentioned above?

No comments:

Post a Comment