나는 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 요소 위에 마우스를 올려하지만 난 내 코드를 실행하면 내가 널 도착하면 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 코드입니다.
답장을 보내 주셔서 감사합니다.하지만 한 폴리곤 만 예를 들었습니다. 코드는 작동하지만 하나의 다각형에만 적용됩니다. 어떻게 다각형을 수정하여 마우스가 움직이는 다각형을 선택할 수 있습니까? –