gpt4 book ai didi

nodes - 更新 cytoscape.js 中的图形 : node positions not updated

转载 作者:行者123 更新时间:2023-12-04 17:40:20 25 4
gpt4 key购买 nike

我使用了以下行:

 cy.json({ elements: treeData });

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

最佳答案

编辑:

错误位置的问题可能源于 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
  • { 元素:... }
  • { 样式:... }

你想要节点及其位置,所以你必须使用 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/cytoscape@3.3.0/dist/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>
<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>

关于nodes - 更新 cytoscape.js 中的图形 : node positions not updated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com