Correct way to select child nodes in D3

I created an SVG element with some nodes:

gnodes = svg.selectAll("g.node")
    .data(_nodes);   
var newNodes = gnodes.enter().append("g")
     .attr("class", "node")
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) 
     .call(drag)
     .on('mouseover', onMouseOver)
     .on('mouseout', onMouseOut);

newNodes.append("circle")
    .attr("cx", 0)
    .attr("cy", 0)
    .attr("r", radius);

newNodes.append("image")
    .attr("xlink:href", getImage)
    .attr("x", -radius/2)
    .attr("y", -radius/2)
    .attr("width", radius + "px")
    .attr("height", radius + "px");

      

In onMouseOver, I want to change the color of the selected circle, but I cannot get that item from the data I receive:

function onMouseOver(d, i) {
   var c1 = d.select("circle"); // error
   var c2 = i.select("circle"); // error
   var c3 = d.selectAll("circle"); // error
   var c4 = i.selectAll("circle"); // error 
}

      

What is the way to get the child of a node with d3?

+3


source to share


1 answer


d

is a data object and an i

index. Both are not instances of d3, which provide access to any of the d3 functions select

.
Try the following:



myelement.on('mouseenter', function(d,i) {
    d3.select(this).select('circle');
});

      

+4


source







All Articles