强制有向图改变鼠标悬停时所有连接节点的颜色

时间:2018-05-04 13:14:20

标签: d3.js force-layout

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

我想这样做..

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图。

1 个答案:

答案 0 :(得分:0)

这是一个基于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>
&#13;
&#13;
&#13;

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

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

    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);
        });
    });
    

    在mouseout期间,我们会遍历所有节点并设置原始颜色:

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