代码之家  ›  专栏  ›  技术社区  ›  jbassking10

Cytoscape.js如何布局连接的节点

  •  0
  • jbassking10  · 技术社区  · 6 年前

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

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

    看起来是这样的: enter image description here

    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();
    

    enter image description here

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

    1 回复  |  直到 6 年前
        1
  •  3
  •   Stephan T.    6 年前

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