【问题标题】:force directed graph change color of all connected node on mouseover强制有向图在鼠标悬停时更改所有连接节点的颜色
【发布时间】:2018-10-14 23:55:50
【问题描述】:

我有一个具有不同节点的力有向图,当用户将鼠标悬停在该节点上时,我想更改所选节点和所有连接(邻居)节点的颜色。

我正在尝试这样做..

function onMouseover(d){
  node.style("fill", function(o){
    var color = isConnected(d, o) ? 'red' : 'blue';
    return color;
  })
  force.resume();
}

function isConnected(d, o){
  return o.index === d,index || 
         (d.children && d.children.indexOf(o.index) !== -1) ||
         (o.children && o.children.indexOf(d.index) !== -1) ||
         (o.parents && o.parents.indexOf(d.index) !== -1) ||
         (d.parents && d.parents.indexOf(o.index) !== -1);
}

任何人都可以在这里帮助我,或者指出一些类似的 d3 图表。

【问题讨论】:

    标签: d3.js force-layout


    【解决方案1】:

    这是一个基于 Mike Bostock 的 Force-Directed Graph 的演示,它改变了悬停节点及其直接连接节点的颜色:

    var svg = d3.select("svg"),
        width = +svg.attr("width"),
        height = +svg.attr("height");
    
    var color = d3.scaleOrdinal(d3.schemeCategory20);
    
    var simulation = d3.forceSimulation()
        .force("link", d3.forceLink().id(function(d) { return d.id; }))
        .force("charge", d3.forceManyBody())
        .force("center", d3.forceCenter(width / 2, height / 2));
    
    d3.json("https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json", function(error, graph) {
      if (error) throw error;
    
      var link = svg.append("g")
          .attr("class", "links")
        .selectAll("line")
        .data(graph.links)
        .enter().append("line")
          .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
    
      var node = svg.append("g")
          .attr("class", "nodes")
        .selectAll("circle")
        .data(graph.nodes)
        .enter().append("circle")
          .attr("r", 5)
          .attr("fill", function(d) { return color(d.group); })
          .call(d3.drag()
              .on("start", dragstarted)
              .on("drag", dragged)
              .on("end", dragended));
    
      node.append("title")
          .text(function(d) { return d.id; });
    
      node.on("mouseover", function(d) {
    
        var connectedNodeIds = graph
          .links
          .filter(x => x.source.id == d.id || x.target.id == d.id)
          .map(x => x.source.id == d.id ? x.target.id : x.source.id);
    
        d3.select(".nodes")
          .selectAll("circle")
          .attr("fill", function(c) {
            if (connectedNodeIds.indexOf(c.id) > -1 || c.id == d.id) return "red";
            else return color(c.group);
          });
      });
    
      node.on("mouseout", function(d) {
        d3.select(".nodes")
          .selectAll("circle")
          .attr("fill", function(c) { return color(c.group); });
      });
    
      simulation
          .nodes(graph.nodes)
          .on("tick", ticked);
    
      simulation.force("link")
          .links(graph.links);
    
      function ticked() {
        link
            .attr("x1", function(d) { return d.source.x; })
            .attr("y1", function(d) { return d.source.y; })
            .attr("x2", function(d) { return d.target.x; })
            .attr("y2", function(d) { return d.target.y; });
    
        node
            .attr("cx", function(d) { return d.x; })
            .attr("cy", function(d) { return d.y; });
      }
    });
    
    function dragstarted(d) {
      if (!d3.event.active) simulation.alphaTarget(0.3).restart();
      d.fx = d.x;
      d.fy = d.y;
    }
    
    function dragged(d) {
      d.fx = d3.event.x;
      d.fy = d3.event.y;
    }
    
    function dragended(d) {
      if (!d3.event.active) simulation.alphaTarget(0);
      d.fx = null;
      d.fy = null;
    }
    .links line {
      stroke: #999;
      stroke-opacity: 0.6;
    }
    
    .nodes circle {
      stroke: #fff;
      stroke-width: 1.5px;
    }
    <svg width="500" height="350"></svg>
    <script src="https://d3js.org/d3.v4.min.js"></script>

    在鼠标悬停期间,想法是:

    1. 检索悬停节点的ID,
    2. 从所有链接中,获取其源或目标是此悬停节点的 id 的链接,
    3. 从这些匹配链接中,检索连接到悬停节点的关联节点 ID,
    4. 遍历所有节点并更改与先前找到的节点匹配的节点的颜色。

    这样:

    node.on("mouseover", function(d) {
    
      var connectedNodeIds = graph
        .links
        .filter(x => x.source.id == d.id || x.target.id == d.id)
        .map(x => x.source.id == d.id ? x.target.id : x.source.id);
    
      d3.select(".nodes")
        .selectAll("circle")
        .attr("fill", function(c) {
          if (connectedNodeIds.indexOf(c.id) > -1 || c.id == d.id) return "red";
          else return color(c.group);
        });
    });
    

    在鼠标移出期间,我们遍历所有节点并设置回原来的颜色:

    node.on("mouseout", function(d) {
      d3.select(".nodes")
        .selectAll("circle")
        .attr("fill", function(c) { return color(c.group); });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2012-01-21
      相关资源
      最近更新 更多