D3.js selectAll (). each along the svg .. undefined path?

I import svg (static content from the server) this way

d3.xml("http://localhost:3000/mysvg.svg", "image/svg+xml", function(xml) {
    var importedNode = document.importNode(xml.documentElement, true);
    var mySvg = d3.select("#somediv").node().appendChild(importedNode);

then I try to iterate over all the SVG paths and do something with them

    d3.selectAll("#somediv svg path").each(function(d, i) {
        console.log(this, d, i);
    });
}

what i get is the problem

  • i - from 1 to the path number, which is correct.

  • dis undefinedinstead of the correct svg path element.

  • this is an svg path element like this

    <path id="m021" fill="#00AAFF" d="M225.438,312.609c-0.665-1.084-1.062-1.691-2.368-1.963c-0.582-0.121-1.686-0.271-2.265-0.069 c-0.507,0.174-0.637,0.649-1.431,0.368c-0.934-0.33-0.665-1.272-0.71-2.104c-0.597-0.021-1.18,0-1.733,0.262 ...etc" ></path>

I was expecting to dbe a real SVG path, why is it not?

EDIT:

A little understanding of what I want to do can help.

svg . . , -. ( ) .

?

+5
2

, , . , . , , .

var svg = d3.select("#somediv svg");
var districts = svg.selectAll("path");

var district_centers = districts[0].map(function(d, i) {
    var bbox = this.getBBox();
    return [bbox.left + bbox.width/2, bbox.top + bbox.height/2];
});

svg
  .selectAll("circle")
  .data(district_centers)
  .enter()
  .append("circle")
     .attr("class", "district_circle")
     .attr("cx", function(d){ return d[0]})
     .attr("cy", function(d){ return d[1]})
     .attr("r", 10)
     .attr("fill", "red");
+6

API doc selection.each, d , , . data() . , , - SVG .

, , , , , .data

+2

All Articles