有没有办法让节点的邻居知道方向?
例如,我有节点:
[
{ 'id': 'a' },
{ 'id': 'b' },
{ 'id': 'c' }
]
和边缘:
[
{ 'id': 'ab', 'source': 'a', 'destination': 'b' },
{ 'id': 'ac', 'source': 'a', 'destination': 'c' },
]
现在,当我打电话给nodeB.neighborhoods()
时,我得到一个nodeA
的结果。
但是nodeB
未连接到nodeA
(仅nodeA
已连接到nodeB
)。
也许cytoscape.js
已经具有功能,可以用来将所有连接的节点都连接到特定节点?
答案 0 :(得分:0)
您可以在具有源和目标属性的边缘上进行选择器查询
请注意,由于以下原因,此片段没有console.log():
cytoscape返回的集合包含大量信息,因此stackoverflow控制台无法显示该信息,它试图将其显示为格式化的字符串或其他内容,然后冻结必须处理的行数...
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
boxSelectionEnabled: false,
autounselectify: true,
style: [{
selector: 'node',
css: {
'content': 'data(id)',
'text-valign': 'center',
'text-halign': 'center',
'height': '60px',
'width': '60px',
'border-color': 'black',
'border-opacity': '1',
'border-width': '10px'
}
},
{
selector: 'edge',
css: {
'target-arrow-shape': 'triangle'
}
}
],
elements: {
nodes: [{
data: {
id: 'a'
}
},
{
data: {
id: 'b'
}
},
{
data: {
id: 'c'
}
}
],
edges: [{
data: {
source: 'a',
target: 'b'
}
},
{
data: {
source: 'a',
target: 'c'
}
}
]
},
layout: {
name: 'concentric'
}
});
// First option: getting the edges with the sourceId "a" and then all targtes of these edges
var targets = cy.edges('[source = "a"]').targets();
// Second option: getting the node with the id "#a" and then this nodes outgoers (all outgoing edges and their target node). After that, you can either get all edges with .edges() or all nodes with .nodes()
var alternative = cy.$('#a').outgoers().nodes();
body {
font: 14px helvetica neue, helvetica, arial, sans-serif;
}
#cy {
height: 100%;
width: 75%;
position: absolute;
left: 0;
top: 0;
float: left;
}
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.js"></script>
</head>
<body>
<div id="cy"></div>
</body>
</html>