TL; 博士
使用最新版本的 D3,您可以selection.raise()
按照tmpearce在其答案中的解释使用。请向下滚动并投票!
原答案
您必须更改对象的顺序并使鼠标悬停的圆圈成为添加的最后一个元素。正如您在此处看到的:https : //gist.github.com/3922684并按照nautat 的建议,您必须在主脚本之前定义以下内容:
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
然后你只需要在鼠标悬停时调用moveToFront
你的对象上的函数(比如circles
):
circles.on("mouseover",function(){
var sel = d3.select(this);
sel.moveToFront();
});
编辑:正如Henrik Nordberg所建议的,有必要使用.data()
. 这对于不失去对元素的约束是必要的。请阅读 Henrick 的回答(并点赞!)了解更多信息。作为一般建议,始终使用.data()
when 将数据绑定到 DOM的第二个参数,以利用 d3 的全部性能功能。
编辑:
正如Clemens Tolboom所提到的,反向功能是:
d3.selection.prototype.moveToBack = function() {
return this.each(function() {
var firstChild = this.parentNode.firstChild;
if (firstChild) {
this.parentNode.insertBefore(this, firstChild);
}
});
};