社區,將外部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