简体   繁体   English

D3.js中的节点和子节点链接

[英]node and subnodes linking in D3.js

I have this data format: 我有这种数据格式:

[{
  "name": "node1",
  "inputs": [{
      "name": "input1",
      "value": 1
  }, {
      "name": "input2",
      "value": 1
  }],
  "outputs": [{
      "name": "output1",
      "value": 1
  }]
}, {
  "name": "node2",
  "inputs": [{
      "name": "input1",
      "value": node1.output1
  }, {
      "name": "input2",
      "value": 1
  }, {
      "name": "input3",
      "value": 1
  }],
  "outputs": [{
      "name": "output1",
      "value": 1
  }]
}] 

i want to crate a d3 visualitation showing each node as a rect an inside of rect show his inputs and output and then show links betweens inputs and outputs of each node 我想创建一个d3视觉效果,将每个节点显示为rect,在rect内部显示其输入和输出,然后显示每个节点的输入和输出之间的链接

im working on it in this plunk http://plnkr.co/edit/bXrcbe?p=preview 我正在这个笨拙的http://plnkr.co/edit/bXrcbe?p=preview中工作

first i create a group: 首先,我创建一个组:

var group = svg.selectAll(".node")
               .data(graph)
               .enter().append("g");

and then append its inputs: 然后附加其输入:

var input = group.selectAll(".input")
                 .data(function(d){return d.inputs})  
                 .enter().append("circle")
                 .attr("r", 3)
                 .attr("cx",10)
                 .attr("cy", function(d,i) { return (i+1)*10; })

but now i want to link the inputs wht other node input and the data is on the group element and not in the input element... 但是现在我想链接其他节点输入的输入,并且数据在组元素上而不在输入元素中...

anyone has any aproach to do this? 任何人都可以做到这一点吗?

thanks 谢谢

You probably don't want to do another operation on the data. 您可能不想对数据进行其他操作。 For creating links, it would probably be better to create another attribute on each node describing the links in that node. 对于创建链接,可能最好在每个节点上创建另一个属性来描述该节点中的链接。 You could either pre-define them (useful if not all inputs in a node connect to all outputs), or you could loop through your object and populate a link between each input and each output, like so: (pseudocode) 您可以预先定义它们(如果不是节点中的所有输入都连接到所有输出,则很有用),或者可以遍历对象并填充每个输入与每个输出之间的链接,如下所示:(伪代码)

for(i = 0; i < graph.length; i++){
  graph[i].links = [];

  for(j = 0; j < graph.inputs.length){
    for(k = 0; k < graph.outputs.length){
      graph[i].links.push(linkForInOutPair);
    }
  }
};

With links defined on each node, you can add them the same way as you added your nodes: 使用在每个节点上定义的链接,您可以像添加节点一样添加链接:

var links = group.selectAll('.link')
    .data(function(d){ return d.links })
    .append('line') //or whatever type of element links should be
    .classed('.link', true)
    ...

Also, you aren't actually giving your inputs the .input class right now. 另外,您现在实际上并没有为输入提供.input类。

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

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