First day with d3.js, going well, but I need to change my placeholder circle shapes to something a litte more complex.
Can SVG shapes that I\'ve created in, say, Ill
If by "imported" you mean be part of the page markup and then used by your d3 code, then yes you can use svg defs element to hold the definition of your custom shape. Then later in your code you create a use
element to reference it:
var node = svg.selectAll("g.node")
.data(nodes)
.enter()
.append("g")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; } )
node.append("use")
.attr("xlink:href","#myshape")
Here is a full example of the above approach. I used Inkscape but the concept is the same:
http://bl.ocks.org/explunit/5988971
Note that the xlink namespace in the svg definition is important for the use
element to work properly, and I see you have it your code already:
xmlns:xlink="http://www.w3.org/1999/xlink"
If by "imported" you mean loaded on-the-fly, then a different approach is needed, as suggested by @LarsKotthoff. But it sounds like you just want to reuse an existing shape definition, and the above approach lets you do it. The <g>
element sets the position of the shapes, and then child node <use>
is added to pull in the actual shape defined earlier.
The definition of the shape is in the svg element in the body, not in the javascript itself. This differs from many D3.js examples which have the svg element created dynamically by the javascript code.
The only connection between the two is the string ID that you put in the href ("myshape" in this case) to match the id in the defs
section:
node.append("use").attr("xlink:href","#myshape")