最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Proper way to select child nodes in D3 - Stack Overflow

programmeradmin0浏览0评论

I have created a 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 the onMouseOver I want to change the colour of highlighted circle, but I can not get this 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 a way to get child node with d3?

I have created a 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 the onMouseOver I want to change the colour of highlighted circle, but I can not get this 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 a way to get child node with d3?

Share Improve this question edited Mar 9, 2016 at 14:54 Eduard Luca 6,61218 gold badges88 silver badges142 bronze badges asked Jun 4, 2015 at 5:44 DmitryDmitry 2,1494 gold badges18 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

d is the data object and i the index. Both are not d3 instances that provide access to any of the d3 select functions.
Try this:

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

评论列表(0)

  1. 暂无评论