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

标签 nodes cytoscape.js

我使用了以下行:

 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/

相关文章:

database - 为什么在这个 B-Tree 查找中有两个叶节点?

javascript - 如何使用 cytoscape.js 在单个动画中向特定方向移动所有节点?

r - R 中的桑基图

java - 如何插入二叉搜索树上的下一个可用节点?

javascript - 如何创建流程图? JavaScript 还是 Flash?

javascript - jQuery:正则表达式测试评论节点

javascript - 如何使用 Cytoscape.js 使节点的名称与其 ID 不同?

javascript - Cytoscape.js,使用正确的设置无法显示图表

javascript - 如何找到子图

javascript - 如何设置边缘跳过另一条边缘?