【问题标题】:Cytoscape.js how to layout connected nodesCytoscape.js 如何布局连接的节点
【发布时间】:2018-11-21 13:11:32
【问题描述】:

我首先创建一个包含 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 布局的所有选项,但找不到任何东西可以让它创建树。

【问题讨论】:

    标签: cytoscape.js


    【解决方案1】:

    编辑: 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>

    【讨论】:

    • 嗨斯蒂芬,谢谢。我遇到的问题是,当我在节点集合而不是整个图形上调用布局时,集合没有按预期布局。如果我在 cy 上调用它,则整个图形都已布置好,并且我已移动的节点集合 I。
    • 你确定吗?也许你没有在所有相关元素上调用布局函数,你有没有像我在上面的 sn-p 中那样在集合中包含边缘?
    • 是的,我创建了所有合适的组件。您可以在图像中看到顶部的样子。它有边缘。
    • 你能告诉我你运行布局的集合吗?我问这个,因为代码似乎很好......
    • 你是 100% 正确的!我没有将边缘添加到集合中。一旦我添加了它们,它的布局就正确了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    相关资源
    最近更新 更多