最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - using foreignObject to add SVG dynamically using D3js - Stack Overflow

programmeradmin3浏览0评论

working

<g>
<foreignObject width="100%" height="100%">
<body>
<div style="width:4em;height:4em">
<object height="100%" width="100%" 
        data="icons/cloud.svg" type="image/svg+xml">


</object>
</div>
</body>
</foreignObject>
<text x="0" y="15" fill="red">I love SVG</text>
</g>

</svg>

Not working

I am trying to add the same thing dynamically using d3js. But it is simply adding DOM element structure, not loading SVG image.

d3.select("body").append("svg")
.append("foreignOject").attr("height","100%").attr("width","100%")
.append("body")
.append("div").style("width","4em").style("height","4em")
.append("object").attr("height","100%").attr("width","100%")
.attr("data","icons/cloud.svg").attr("type","image/svg+xml");

After xhtml: prefix also same. I don't know why that 'object' tag is not loading SVG image. Please check the following SC:

working

<g>
<foreignObject width="100%" height="100%">
<body>
<div style="width:4em;height:4em">
<object height="100%" width="100%" 
        data="icons/cloud.svg" type="image/svg+xml">


</object>
</div>
</body>
</foreignObject>
<text x="0" y="15" fill="red">I love SVG</text>
</g>

</svg>

Not working

I am trying to add the same thing dynamically using d3js. But it is simply adding DOM element structure, not loading SVG image.

d3.select("body").append("svg")
.append("foreignOject").attr("height","100%").attr("width","100%")
.append("body")
.append("div").style("width","4em").style("height","4em")
.append("object").attr("height","100%").attr("width","100%")
.attr("data","icons/cloud.svg").attr("type","image/svg+xml");

After xhtml: prefix also same. I don't know why that 'object' tag is not loading SVG image. Please check the following SC:

Share Improve this question edited Feb 24, 2013 at 5:27 aWebdesigner09 asked Feb 23, 2013 at 1:11 aWebdesigner09aWebdesigner09 2672 gold badges5 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You need to prefix the outer html element with xhtml: so that d3 creates it in the xhtml namespace. So append("body") is correctly written as append("xhtml:body") for instance.

d3 Elements take a default namespace from their parent so if you write xhtml:body, the inner div can be written either as "div" or as "xhtml:div"

You've also misspelled foreignObject as foreignOject.

fixed by adding a fallback 'img' tag for 'object' tag, as shown below:

var svg = d3.select('body').append('svg');
svg.append('foreignObject')
  .attr('width', '100%')
  .attr('height', '100%')
  .attr('x', 0) 
 .append('xhtml:div').style('height','1100px').style('width','1100px')
.append('xhtml:object')
.attr('height','100%').attr('width','100%')
.attr('type','image/svg+xml')
.attr('data','http://upload.wikimedia/wikipedia/mons/8/84/Konqi_svg.svg')
.append('img').attr('alt','notloaded');
发布评论

评论列表(0)

  1. 暂无评论