d3.js 将单击操作添加到强制布局圆?
我正在努力创建一个具有力布局的无向图。此外,我尝试通过单击事件更改每个圆圈(节点)的颜色。有没有什么想法可以在圆形元素上添加此类事件。我尝试了这段代码,但它不起作用。
vis.selectAll("circle.node").on("click", function(d){
vis.select(d).attr(r, 25)
.style("fill","lightcoral")
.style("stroke","red");
});
I am working on to create a undirected graph with force layout. In addition, I try to chage each circle's (node's) color with click event. Is there any idea to add such event on the circle elements. I tyry this code however it is not working.
vis.selectAll("circle.node").on("click", function(d){
vis.select(d).attr(r, 25)
.style("fill","lightcoral")
.style("stroke","red");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
select(d)
引用数据,而不是元素。您需要选择(此)
select(d)
references the data, not the element. You need toselect(this)
vis.select(this)
给了我一个 DOM 异常。d3.select(this)
对我有用。您还可以使用 d3.event.target 来访问被单击的 DOM 元素。vis.select(this)
gives me a DOM exception.d3.select(this)
works for me. You can also used3.event.target
to access the DOM element that is clicked on.