Cytoscape.js如何布局连接节点

Cytoscape.js how to layout connected nodes

我首先创建一个包含 100 个连接节点的图表。添加完所有节点后,我调用

cy.layout({name: "dagre"});

接下来,我创建了 5 个左右的额外连接节点,我在添加的节点上调用布局,但它没有按预期布置它们。所有的节点都在一条直线上,而不是更像一棵树。

看起来像这样:

var collection = cy.collection();
collection.merge(eles);
...
// I merge in another 5 newly created nodes.
// Next I call layout
collection.layout({
            name: "dagre", fit: false,
            boundingBox: {
                x1: mousex - width / 2, y1: mousey - height / 2, x2: mousex + width, y2: mousey + height
            },
            nodeSep: 30
        }).run();

但我希望它看起来像下图。

为了让它看起来像上面那样,我调用了如下所示的布局。

cy.layout({name: "dagre"});

我查看了 dagre 布局的所有选项,但找不到任何东西来创建树。

编辑: dagre 布局需要节点和边来计算节点的正确位置,您使用它的方式,dagre 认为您给它 5 个单独的节点,这解释了你错误的布局。错误在这里:

collection.merge(eles);          // here you should add all relevant nodes and edges

结束

我有一个例子给你--->here<---,只需复制它并添加你的真实数据:

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: "100px",
        shape: "rectangle",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],

  elements: {
    nodes: [
      { data: { id: "Top", faveColor: "#2763c4" } },
      { data: { id: "yes", faveColor: "#37a32d" } },
      { data: { id: "no", faveColor: "#2763c4" } },
      { data: { id: "Third", faveColor: "#2763c4" } },
      { data: { id: "Fourth", faveColor: "#56a9f7" } }
    ],
    edges: [
      { data: { source: "Top", target: "yes" } },
      { data: { source: "Top", target: "no" } },
      { data: { source: "no", target: "Third" } },
      { data: { source: "Third", target: "Fourth" } },
      { data: { source: "Fourth", target: "Third" } }
    ]
  },
  layout: {
    name: "random"
  }
}));

cy.ready(function () {
  setTimeout(function () {
    cy.nodes().layout({ name: 'dagre' }).run(); // this is what you do!!
    setTimeout(function () {
      cy.elements().layout({ name: 'dagre' }).run(); // this is what you should do!!
  },5000);
  }, 5000);
  
  
});
body { 
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  float: left;
  position: absolute;
}
<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>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>