小编Seb*_*nee的帖子

(嵌入和)通过D3和/或javascript参考外部SVG

我有一个.svg文件,想要将它嵌入我的d3-graphic的svg结构中.

我还需要通过某些g元素的id引用附加到g元素的所有路径/多边形.

我尝试了不同的方法来嵌入和引用svg(g's),但由于某些原因它不起作用:

(1)第一次尝试

// Firefox displays my svg but when i open it with Chrome the svg     
//is not displayed (just default placeholder icon)
// I can't reference the svg-g id's with d3.select functions. 
main_chart_svg
        .append("svg:image")
        .attr("xlink:href", "mySVGpicture.svg")
        .attr("x", "50")
        .attr("y", "50")
        .attr("width", "500")
        .attr("height", "500");  

main_chart_svg.select("#anIdWhichIsInTheSvgFile").remove(); //// This doesn't work
Run Code Online (Sandbox Code Playgroud)

(2)第二次尝试

// This displays the svg but -not- inside my main-chart-svg. I want to keep the graphic   
//things seperate to html-stuff.
d3.xml("mySVGpicture.svg", "image/svg+xml", function(xml) {
        document.body.appendChild(xml.documentElement);
    });
//----------or----------// …
Run Code Online (Sandbox Code Playgroud)

javascript xml svg d3.js

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

d3.js ×1

javascript ×1

svg ×1

xml ×1