我应该如何格式化我的 JSON 并向我解释一些基本的 d3 图 node-link?

How should i format my JSON and explain me some basic d3 graph node-link?

我想使用 D3.js 将此 JSON 中的数据表示为 node-link。我是 JavaScript 和 D3.js 的新手。我有 3 种类型的数据,我想在这 3 种类型的数据之间建立层次结构。 Parents > Source > Children ,我想将每个 parent 放在源上方, link 每个 parent 到源,每个 child应该在源代码下 link 它们到源代码 :

script.js

var width = 960,
        height = 500;

// i don't really understand what this does 
// except the .linkDistance - gives the dimension of the link
var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(40)
    .on("tick", tick);

// Drag the nodes
var drag = force.drag()
    .on("dragstart", dragstart);

//Appends the svg - the place where i draw all my items 
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

// Select all the links and nodes , from an array ? i don't really get it
var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

// The function where i take the data from the JSON
d3.json("graph.json", function(error, graph) {
  if (error) throw error;

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

  link = link.data(graph.links)
    .enter().append("line")
      .attr("class", "link");

  node = node.data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", 12)
      .on("dblclick", dblclick)
      .call(drag);
});


// Here is the function where i should asign the position of the nodes and the links
// This is the most problematic and i really don't understand it
function tick(){}

// The function to fix and to clear the fix from a node
function dblclick(d) {
  d3.select(this).classed("fixed", d.fixed = false);
}

function dragstart(d) {
  d3.select(this).classed("fixed", d.fixed = true);
}

Index.html

<!DOCTYPE html>
<html lang="en">

    <head>
    <link rel="stylesheet"  href="style.css">
        <meta charset="utf-8">
        <title>D3 Test</title>
        <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>    
        <script src="script.js"></script>
    </head>
</body>

style.css

.link {
    stroke: #777;
    stroke-width: 2px;
}
.nodes circle{
  cursor: move;
  fill: #ccc;
  stroke: #000;
  stroke-width: 1.5px;

}
.node.fixed {
  fill: #f00;
}

graph.json

 {
    "nodes":[
    {
        "id": "Source" , "group" :0
    },
    {
        "id":"Parent_1" , "group" : 1 
    },
    {
        "id": "Parent_2" , "group" :1
    },
    {
        "id": "Parent_3" , "group" :1
    },

    {
        "id":"Child_1" , "group":2
    },
    {
        "id":"Child_2" , "group":2
    },
    {
        "id":"Child_3" , "group":2}
    ],
    "links":[
    {
    "source":"Source","target":"Parent_1"
    },
    {
    "source":"Source","target":"Parent_2"
    },
    {
    "source":"Source","target":"Parent_3"
    },
    {
    "source":"Source","target":"Child_1"
    },
    {
    "source":"Source","target":"Child_2"
    },
    {
    "source":"Source","target":"Child_3"
    },

    ]



    }

如果有人有时间和心情向我解释如何格式化我的 JSON 以便我可以更有效地使用它,并向我解释如何使用 d3 逐步创建 node-link 图步骤或给我一个演示并解释每一块代码我将非常感激。 如果我提出问题的方式有问题或有不清楚的地方,请说出来,以便我进行编辑。谢谢!

好吧,简而言之,力将所有节点放在一个 svg 上,并赋予它们很多属性,一些与数据相关联,一些与力图的行为相关。

一般情况下,节点会相互排斥,但可以通过设置 d3.forceManyBody 的强度来改变这种行为。

链接将在节点之间产生力,可用于将节点拉到一起,或将它们保持在一定距离。

一般情况下,一个力会将所有节点拉向图形的中心,但您可以将它们设置为被吸引到任何地方。在你的情况下,你想要一个位于顶部中心的点(比如 SVG 下方 25%)来吸引父节点(第 1 组)和一个点底部中心(SVG 下方 75%)来吸引子节点(第 2 组) ).您可以将源节点设置为居中;

var position = d3.forceSimulation(graph.nodes).force("charge", d3.forceManyBody()).force("x", d3.forceCenter(width / 2, height/ 2)).force("collide", d3.forceCollide(15 * R));
    nodes.each(function(d) {
        if (d.group == 0) {
            d.fx = wid / 2;//fix X value
            d.fy = wid / 2//fix Y value
        }
    });

如果您可以设置一个 jsfiddle 或类似的东西并使某些东西正常工作,我也许可以看到您被卡住的地方(看起来您的订单在数据加载之前创建链接有点偏离)。另外,您正在加载 d3 版本 3,在您开始时,您不妨现在切换到版本 4。

编辑:这是我的理解,我认为上面链接的资源可能更好!