更新cytoscape.js中的图表:节点位置未更新

问题描述 投票:1回答:1

我使用了以下行:

 cy.json({ elements: treeData });

为了更新我的图表中的数据(使用cytoscape.js)。该图现在具有新数据,但节点位于先前的位置。我该如何解决这个问题?我没有特定的位置,我想根据新节点获得默认位置。

nodes cytoscape.js
1个回答
0
投票

编辑:

错误位置的问题可能源于cy.json()在布局完成之前没有更新图形,尝试使用您的代码:

cy.json({ elements: treeData }); 
cy.ready(function () {
    var layout = cy.layout({ name: 'breadthfirst', directed: true, padding: 2 }); 
    layout.run();
});

函数cy.json()有两个功能,第一个是图的导出,第二个是图的导入。使用导入时,您可以指定要导入的内容:

  • cyjson =整个导出的json
  • {elements:...}
  • {style:...}

你想要节点的位置,所以你必须使用cyjson。以下是整个事情的一个例子:

var json;
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: "dagre"
  }
}));
document.getElementById('save').addEventListener('click', function() {
  json = cy.json();
});
document.getElementById('load').addEventListener('click', function() {
  cy.json(json);
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 85%;
  width: 100%;
  float: right;
  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://unpkg.com/[email protected]/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/[email protected]/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <b id="save" style="cursor: pointer; color: darksalmon">Save graph</b> / <b id="load" style="cursor: pointer; color: darkmagenta">Load graph</b>
  <div id="cy"></div>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.