【问题标题】:Showing connected nodes on mouseover shows crossed links在鼠标悬停时显示连接节点显示交叉链接
【发布时间】:2012-09-20 09:11:25
【问题描述】:

我有一个力导向图,在节点鼠标悬停时,未连接到当前节点的元素会淡出,基于以下example。问题是,如果光标快速移动并且它穿过一个不属于目标节点连接的链接,它仍然显示为完全不透明,因为在途中触发了交叉链接鼠标悬停,但显然没有时间完成它的mouseout功能。例如,您可以看到:

在图片中,鼠标从上到下快速移动,光标停在 HSA-MIR-424 上。与目标节点的连接正确显示,但还可以看到三个额外的链接,因为当光标穿过它们时会触发鼠标悬停。如果我缓慢地重复从上到下的运动到同一个节点或从下到上移动到节点,则不会出现此类问题(因为在该方向上没有要穿过的链接,在图片中)。

如何避免这个问题?

相关部分代码,链接mouseover/mouseout:

    .on("mouseover", function(d) {
      var selection = d3.select(this);
      var initialWidth = Number( selection.style("stroke-width") );
      selection.transition().style("stroke-width", initialWidth + Number(4) )
      .style("stroke-opacity", 1.0);
      //.style("stroke", linkOverColor);
    } )
    .on("mouseout", function(d) {
      var selection = d3.select(this);
      selection.transition().style("stroke-width", getLinkStroke( selection.data()[0] ) )
      .style("stroke-opacity", conf.link_def_opacity);
    })

节点鼠标悬停/鼠标移出:

    // display closest neighbors and fade others out
    .on("mouseover", fade(0.10) )
    // return to default view
    .on("mouseout", fade(conf.node_def_opacity) );

褪色功能:

    function fade(opacity) {
      return function(d) {

        d3.event.stopPropagation();

        var thisOpacity;

        // return to default view
        if( opacity === conf.node_def_opacity )
        {
          d3.selectAll('marker > path').transition().style("opacity", 1);
          nodeGroup.transition().style("opacity", conf.node_def_opacity);
          link.style("stroke-opacity", conf.link_def_opacity);
        }
        else // fade not-neighborhood away
        {
          d3.selectAll('marker > path').transition().style("opacity", 0);
          // d3.selectAll('marker > path').transition().style('display', 'none');
          nodeGroup.transition().style("opacity", function(o)
          {
            thisOpacity = isConnected(d, o) ? conf.node_def_opacity : opacity;
            return thisOpacity; 
          });

          link.style("stroke-opacity", function(o) {
            return o.source === d || o.target === d ? conf.link_def_opacity : opacity;
          });              
        }
      }
    }   

【问题讨论】:

    标签: javascript events graph d3.js fade


    【解决方案1】:

    通过为节点和链接鼠标悬停动作引入计时器解决了这个问题。代码列在这个answer中。

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多