2

有没有办法让节点的邻居知道方向?

例如,我有节点:

[
    { '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已经有功能,我可以用它来让所有连接的节点到特定的节点?

4

1 回答 1

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>

于 2019-01-15T20:52:45.167 回答