社区,将外部svg绑定到数据
我想将外部svg文件绑定到我的数据数组。
我装的元素插入我的DOM是这样的:
defs = d3.select("defs");
d3.html("combisymbol.svg", function(data) {
//get a selection of the image so we can pull out the icon
xml = d3.select(data);
icon = document.importNode(xml.select("#star").node(), true);
icon.id = "staricon";
defs.node().appendChild(icon);
// console.log("icon", icon);
然后我试图使其可见。我使用与将数据绑定到圈子时相同的方法。使用它的圆圈,但我的外部svg不可见。
d3.select("body").select("div#divCombiSVG")
.selectAll("star")
.data(combiData)
.enter()
.append("svg:use")
.attr("xlink:href", "#staricon");
我没有看到svgs。
我也试过这样:
d3.select("body").select("div#divCombiSVG")
.selectAll("star")
.data(combiData)
.enter()
.append("svg")
.attr("width",200)
.attr("height",200)
.node().appendChild(icon);
但随后的图标就会只添加到第一个数据元素,而不是第二。即使它被添加到第一个,它仍然不可见。
SVG文件看起来是这样的:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904 /DTD/svg10.dtd">
<svg version="1.0" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="200px" height="200px" viewBox="0 0 37.207 100" enable-background="new 0 0 37.207 100"
xml:space="preserve">
<path xmlns="http://www.w3.org/2000/svg" id="star" cx="50" cy="50" r="20" r2="43"
orient='point' points='3' radial-shift='0' outerCurve='86'
outerOffset='4.1' innerCurve='56' innerOffset='2.2' d="M300,168
C347.7790400396858,178.49361334198113
345.7070270919484,217.64466544113793 337.23909236273084,228.5
C350.87405522189334,226.59422068634012 385.8158673985199,244.3753308862077 371.014083110324,291
C338.0368273588341,327.1310557718112
305.1670281299449,305.76111387252195 300,293 C294.83297187005513,305.76111387252195
261.9631726411659,327.1310557718112 228.98591688967605,291 C214.1841326014801,244.37533088620776
249.12594477810666,226.59422068634015 262.7609076372691,228.50000000000003
C254.29297290805158,217.64466544113793 252.22095996031422,178.4936133419811 300,168 "
fill="yellow" stroke="black" stroke-width="2"></path>
</svg>
combiData目前有两个目标。 我已经找了几个小时在其他例子,但我不能让它的工作。我认为我很接近,虽然...我对d3很新(但很有动力),所以请耐心等待我。 :-)
在此先感谢您的帮助!
该svg被破坏,所以这可能是第一个问题。它正在绘制视口外的路径。再加上它不是明星,如果这很重要的话。 – Anthony 2014-10-10 14:38:53
还要设置use元素的x和y属性。 – Anthony 2014-10-10 14:41:45
这很奇怪。我可以很好地看到它<使用xlink:href =“#star”transform =“translate(200,200)scale(.5,.5)”>(star is the former id) – 2014-10-10 14:42:40