简体   繁体   English

D3强制定向JSON邻接表

[英]D3 Force Directed JSON Adjacency List

I have a Graph Adjacency List, like so: 我有一个图邻接列表,如下所示:

{
  nodes: [
  {
    "id": 1,
    "label": "Mark
  },...],

  edges: [
  {
    "source": 1,
    "target": 2
  },....]
}

The edges use the id from the node! 边缘使用节点的ID!

I've been using PHP to echo this list onto client side. 我一直在使用PHP将此列表回显到客户端。

D3 cannot load my JSON from file. D3无法从文件加载我的JSON。 So I try to parse it manually: 所以我尝试手动解析它:

var width = window.innerWidth,
      height = window.innerHeight;

  var color = d3.scale.category20();

  var force = d3.layout.force()
      .linkDistance(40)
      .linkStrength(2)
      .charge(-120)
      .size([width, height]);

  var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

  var nodes = [], links = [];

  d3.json( null, function( error )
  {
    JSONData.nodes.forEach( function(node)
    {
      nodes.push( node );
    });

    JSONData.edges.forEach( function(edge)
    {
      links.push({source: edge.source, target: edge.target});
    });

    force.nodes(nodes)
     .links(links)
     .start();

    var link = svg.selectAll(".link")
          .data(links)
          .enter().append("path")
          .attr("fill", "none")
          .attr("class", "link");

    var node = svg.selectAll(".node")
          .data(nodes)
          .enter().append("circle")
          .attr("class", "node")
          .attr("r", 5)
          .style("fill", "#4682B4" )
          .call(force.drag);

    node.append("title").text(function(d) { return d.label; });

    force.on("tick", function() 
    {
      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; });
    });
  });

However, all I can see is the nodes, but not the edges. 但是,我只能看到节点,而不是边缘。 I have added a CSS file with: 我添加了一个CSS文件:

.node {
  stroke: #4682B4;
  stroke-width: 1.5px;
}

.link {
  stroke: #ddd;
  stroke-width: 1.5px;
}

But to no avail. 但无济于事。 Is there some way around this? 有办法解决吗?

You need to use: 您需要使用:

      .enter().append("line")

instead of 代替

      .enter().append("path")

(maybe also you need to change some other attributes so that they correspond to a line , not a path ). (也许您还需要更改一些其他属性,以便它们对应于line而不是path )。

Alternatively, you can draw some more complex paths instead of straight lines, but I think it would be an overkill in your case. 另外,您可以绘制一些更复杂的路径而不是直线,但是我认为这对您来说是一个过大的选择。

EDIT: 编辑:

I just found two related and interesting questions: 我刚刚发现了两个相关且有趣的问题:

enter link description here - describes another kind of problems while displaying links in a force layout - make sure you don't hit a similar obstacle 在此处输入链接说明 -在以强制布局显示链接时描述另一种问题-确保不会碰到类似的障碍

enter link description here - a guy trying to draw curvbes instead of lines, if you decide to do something similar 在此处输入链接说明 -如果您决定做类似的事情,那么他会尝试绘制曲线而不是线条

But hope that this answer and hints in the comments are sufficient for you to get what you want. 但希望此答案和注释中的提示足以使您获得想要的东西。

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

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