简体   繁体   English

D3 无法在鼠标悬停时 select 两个元素

[英]D3 unable to select two elements in mouseover

I've created a graph force simulation in a useEffect of a React component and am trying to highlight the node, the nodes' neighbors, and their connecting edges using d3 .我在 React 组件的useEffect中创建了图形力模拟,并尝试使用d3突出显示节点、节点的邻居以及它们的连接边。 I'm able to change the color of the node and it's neighbors using the `.on('mouseover') method, however, I'm unable to also change the link/edge color.我可以使用 `.on('mouseover') 方法更改节点及其邻居的颜色,但是,我也无法更改链接/边缘颜色。 I've ensured I'm getting the right #ids for the links, but I can't change the colors of the links.我已经确保我获得了正确的链接#id,但我无法更改链接的 colors。

nodeAndLabel
   .append("circle")
      .attr("class", "node")
      .attr("r", 10)
      .attr("id", function(d){return d.name})
      .on("mouseover", function(d, i){
             let neighbors = getNeighbors(i);
             
             d3.select(this).attr("class", "hoveredNode");

             d3.selectAll(neighbors.nodes)
                .attr("class", "hoveredNeighbor") //working
                        
            d3.selectAll(neighbors.links)
                .attr("class", "hoveredNeighbor"); // not working

           console.log("have hovered over: ",d3.selectAll(neighbors.links))
               });

Not working Output for d3.selectAll(neighbors.links) NodeList is what I'm trying to select the ids for d3.selectAll(neighbors.links) NodeList 不工作 Output 是我正在尝试的 select 的 id

      _groups: Array(1)
          0: NodeList(3) [line#link_0, line#link_1, line#link_2]
      length: 1
         __proto__: Array(0)
         _parents: [html]
         __proto__: Object

Working Output for d3.selectAll(neighbors.nodes)d3.selectAll(neighbors.nodes)工作 Output

     _groups: Array(1), _parents: Array(1)}
     _groups: Array(1)
        0: NodeList(3) [circle#Antonia, circle#Mark, circle#Devin]
     length: 1
        __proto__: Array(0)
        _parents: [html]
        __proto__: Object

Any help is greatly appreciated!任何帮助是极大的赞赏! I'm having trouble understanding why I can change add a class to neighbors.nodes , but not neighbors.links .我无法理解为什么我可以更改添加 class 到neighbors.nodes ,而不是neighbors.links

For some reason, I was unable to add a class, so I modified the style directly using:由于某种原因,我无法添加 class,所以我直接使用以下方式修改了样式:

.style("stroke", "aliceblue");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM