简体   繁体   English

使用cytoscape.js循环节点

[英]Looping nodes in with cytoscape.js

I am currently trying to do looping for a graph by using cytoscape.js The problem is I don't quite get how to do it since there are some repeating nodes. 我目前正在尝试使用cytoscape.js为图形循环。问题是我不知道怎么做,因为有一些重复的节点。

I have these data like this : 我有这样的数据:

Sheraton Cherry, Rosaceae, Dentate, Pinnate
Pineapple Guava, Myrtaceae, Entire, Pinnate
Chinese Sumac, Rosaceae, Entire, Pinnate

And as you can see some data is repeating. 正如您所看到的,一些数据正在重复。

So far this is what I got : 到目前为止,这是我得到的:

  elements: {
nodes: [
  { data: { id: 'a', name: 'Sheraton Cherry' } },
  { data:  { id: 'a1', name: 'Rosacea'}},
  { data: { id: 'a2', name: 'Dentate' } },
  { data: { id: 'a3', name: 'Pinnate' } },
  { data: { id: 'b', name: 'Pineapple Guava' } },
  { data:  { id: 'b1', name: 'Myrtaceae'}},
  { data: { id: 'b2', name: 'Entire' } },
  { data: { id: 'a3', name: 'Pinnate' } }

],
edges: [
  { data: { source: 'a1', target: 'a' }},
  { data: { source: 'a2', target: 'a' }},
  { data: { source: 'a3', target: 'a' }},
  { data: { source: 'b1', target: 'b'}},
  { data: { source: 'b2', target: 'b'}},
  { data: { source: 'a3', target: 'b'}}
]

}, },

I typed it manually in which the graph came out perfectly but since this is only some of the data and I have 10 more of it, I should loop the nodes and edges. 我手动输入了图形,但是由于这只是一些数据,我还有10个,我应该循环节点和边缘。

But how can I do it? 但是我怎么能这样做呢?

Found something like this : 找到这样的东西:

 var demoNodes = []; var demoEdges = []; demoNodes.push({ data: { id: b[0], name:b[0] } }); for (var i = 0; i < a.length; i++) { demoNodes.push({ data: { id: a[i], name:a[i] } }); } for (var i = 0; i < a.length; i++) { demoEdges.push({ data: { source: b[0], target: a[i] } }) } 

but still it's not working. 但它仍然不起作用。

Parse your input data and convert it to the elements JSON format . 解析输入数据并将其转换为元素JSON格式

If your data is just comma separated, use .split() and .trim() on the input string. 如果您的数据只是逗号分隔,请在输入字符串上使用.split().trim() See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Cytoscape does not allow elements with duplicate IDs, but it would be better in principle if you disallow duplicates as you parse (or with another pass post parsing). Cytoscape不允许具有重复ID的元素,但如果您在解析时(或使用其他传递解析后)禁用重复项,原则上会更好。

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

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