javascript - 悬停时增加节点半径

标签 javascript d3.js mouseover force-layout

Link to my code (Plunker)

我正在 D3.js 强制布局中开发网络图,我被鼠标悬停功能所困扰。当我将鼠标悬停在节点上时,我希望与其关联的链接和子节点(一跳)的大小扩大。现在,我的代码增加了悬停节点和与其关联的链接的大小,但不增加与其关联的节点的大小。

这是我迄今为止尝试过的,

鼠标悬停在节点上时将展开 -

function mouseover(d) {
    link.style('stroke-width', function(l) {
    if (d === l.source || d === l.target)
        return 4;
    });

    d3.select(this).select("circle").transition()
    .duration(300)
    .attr("r", 12);
}

鼠标移开时,悬停的节点将恢复到之前的大小 -

function mouseout() {
    link.style('stroke-width', 1.5);
    d3.select(this).select("circle")
    .transition()
    .duration(750)
    .attr("r", function(d) { return Math.sqrt(d.size) / 10 || 4.5; });
}

提前致谢。

最佳答案

您需要一些 for 循环才能完成此任务:

在鼠标悬停函数内执行以下操作:

//links for which source or traget is hovered       
var filtered = link.filter(function(l){
  return (d === l.source || d === l.target);
})
filtered.style("stroke-width", 4);
//select all the data associated with the link i.e. source and target data
var selectedData = [];
filtered.each(function(f){
  selectedData.push(f.source);
  selectedData.push(f.target);
});
//select all the circles for which we have collected the data above.
var circleDOM = [];
selectedData.forEach(function(sd){
  d3.selectAll("circle")[0].forEach(function(circle){
    console.log(d3.select(circle).data()[0].name, sd.name)
    if (d3.select(circle).data()[0].name == sd.name){
      circleDOM.push(circle);//collect all the DOM Elements for which the data matches.
    }
  });
});
//do transition with all selected DOMs
d3.selectAll(circleDOM).transition()
    .duration(300)
    .attr("r", 12);

工作示例here

关于javascript - 悬停时增加节点半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621867/

相关文章:

javascript - 如何在 UI5 中使用 Input 的约束属性?

javascript - 将 javascript 文件内容包含在 ob_start 缓冲区中

javascript - 如何在使用 React 提交表单后重定向用户

javascript - d3.js - 使用 .selectAll() 根据属性值选择 Node 元素

javascript - 如何改变节点的不透明度?

java - Selenium Webdriver 的鼠标悬停功能不适用于 Opera 39,并且与 Chrome 53 的工作不一致

javascript - 在 codeigniter 中使用 ajax 时出错

c# - 如何在单击游戏对象时创建菜单? (统一)

javascript - Sound Manager 2 基本示例重定向问题(mp3 在单独页面上)

javascript - 删除 d3 强制布局的动画