私は、SVGファイル(データ/ humanTest.svg)作成:SVGファイルでポリゴンのIDを取得するにはどうすればよいですか?
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 19.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 800 800" style="enable-background:new 0 0 800 800;" xml:space="preserve">
<polygon fill="yellow" id="forehead" stroke="blue" stroke-width="2" points="318.1,24.6 308.7,40.3 308.7,79.6 360.6,82 378.6,46.6 370,8.9 346.4,17.5 "/>
</svg>
をし、私はSVG要素の上にカーソルを置くが、私は自分のコードを実行したとき、私はnullを取得するときに、ID「額」をログに記録します。誰も私を助けてくださいできれば
var main_chart_svg = d3.select("#diagram")
.append("svg")
.attr({
"width": 800,
"height": 800
}).append('g');
d3.xml("data/humanTest.svg").mimeType("image/svg+xml").get(function (error, xml) {
if (error) throw error;
var svgNode = xml
.getElementsByTagName("svg")[0];
main_chart_svg.node().appendChild(svgNode);
var innerSVG = main_chart_svg.select("svg");
innerSVG.append("text")
.attr("dy", "1em")
.attr("dx", "1em")
.text("Test text");
innerSVG.on("mousemove", function (d) {
//console.log(innerSVG.id);
//d3.select("#diagram svg").selectAll("g")
console.log(d3.select(this).attr('id')); //doesnt work,
//gives me the svg id (Layer_1) not the id of the polygons within the svg tag
})
});
は非常に感謝し、次のようになります。
は、ここに私のD3コードです。
あなたの答えをありがとうが、私は例として1つのポリゴンを与えました。あなたのコードは動作しますが、1つの多角形に対してのみ、複数の多角形に対してどのように変更して、マウスがホバリングしているポリゴンを拾うことができますか? –