d3.js 将单击操作添加到强制布局圆?

发布于 2024-12-29 18:56:56 字数 264 浏览 1 评论 0原文

我正在努力创建一个具有力布局的无向图。此外,我尝试通过单击事件更改每个圆圈(节点)的颜色。有没有什么想法可以在圆形元素上添加此类事件。我尝试了这段代码,但它不起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来是傀儡 2025-01-05 18:56:56

select(d) 引用数据,而不是元素。您需要选择(此)

 vis.selectAll("circle.node").on("click", function(){
            d3.select(this).attr('r', 25)
                .style("fill","lightcoral")
                .style("stroke","red");
        });

select(d) references the data, not the element. You need to select(this)

 vis.selectAll("circle.node").on("click", function(){
            d3.select(this).attr('r', 25)
                .style("fill","lightcoral")
                .style("stroke","red");
        });
挽心 2025-01-05 18:56:56

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 use d3.event.target to access the DOM element that is clicked on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文