Ich habe eine SVG-Datei (Daten/humanTest.svg):Wie kann ich die ID eines Polygons in einer SVG-Datei erhalten?
<?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>
, und ich will die id "Stirn" anmelden, wenn ich über das svg Element schweben, aber wenn ich meinen Code ausführen i null erhalten.
Hier ist mein d3 Code:
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
})
});
Wäre sehr dankbar, wenn mir jemand bitte helfen konnte.
Danke für Ihre Antwort, aber ich gab nur ein Polygon als Beispiel. Ihr Code funktioniert aber nur für ein Polygon, wie kann ich es für mehrere Polygone modifizieren, damit es das Polygon aufnehmen kann, auf dem die Maus schwebt? –