[英]Proper way to select child nodes in D3
我創建了一個帶有一些節點的SVG元素:
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");
在onMouseOver中我想更改突出顯示的圓圈的顏色,但我無法從收到的數據中獲取此項目:
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
}
使用d3獲取子節點的方法是什么?
d
是數據對象, i
是索引。 兩者都不是提供對任何d3 select
功能的訪問的d3實例。
試試這個:
myelement.on('mouseenter', function(d,i) {
d3.select(this).select('circle');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.