I have an .svg file and want to embed it in the svg strucure of my d3-graphic.
I also need to reference all paths/polygons attached to g-elements via an id of certai
The proper format for embedding SVG content from another file is to use a
If you want to be able to modify the graphic described by the external file, you would be better off reading the external file as a text/XML file (using d3.text ), and then using that XML text to create the SVG graphic in the DOM by writing the content as the inner HTML of a container Your DOM should now look like You can now select elements as normal, and set the position or size of the svg you created or remove g elements or whatever -- they are all just normal elements in your DOM. Make sure that your "id" attributes don't conflict -- they have to be unique for the entire page. Example here, showing additional content added to the SVG with d3, and a d3 transition selecting and modifying an element from the external SVG: The original version of this answer suggested adding the external SVG content as a nested SVG inside the SVG that had already been created with D3. As Seb discovered, this method does not work on the latest Safari and Opera browsers, or on Chrome 31 and under. The problem is in the way the webkit browsers implement the What's most confusing is that the method doesn't return any errors, and some versions of Chrome actually display the SVG content without adding it to the DOM (as if you used d3.text("mySVGpicture.svg", function(error, externalSVGText) {
if (error) {console.log(error); return;}
chart_div.html(externalSVGText);
svg = chart_div.select("svg");
do_stuff();
});
body
div.chart
svg
g#anIDWhichIsInTheSVGFile
g#DE
path
/*etc*/
g#anotherIDWhichIsInTheSVGFile
http://jsfiddle.net/J8sp3/2/WARNING
innerHTML
function, which is called by d3's selection.html()
function. Specifically, the innerHTML function isn't implemented at all on SVG elements! ).