我的公司正在为聊天机器人构建一个图形视图编辑器。我们正在使用 Cytoscape 和cytoscape-cola
扩展来实现这一点。我们面临的问题之一是动态地向图中添加新节点,而不会使它们与图上的现有节点重叠。
我查看了以前的类似问题(如下所列),但无济于事:
Cytoscape - 处理锁定节点我在那里尝试了解决方案,即仅在新添加的节点上应用布局,但它们一直堆叠在屏幕的中心。
Cytoscape - 忽略锁定的节点我尝试了此处提到的解决方案,但无论是一次性锁定节点还是cy.nodes().lock()
单独cy.nodes().forEach(node => node.lock())
锁定节点,锁定的节点仍会继续移动。这里还需要注意的是,当单独锁定节点时,新添加的节点也会被锁定,无论我是否在上面的调用中锁定。
我也尝试了这个解决方案,但锁定的节点仍然移动并且整个布局发生了变化——有时完全改变,有时只是一点点。
代码
这是我目前用来构建图表的方法:
const layoutConfig = {
name: "cola",
handleDisconnected: true,
animate: true,
avoidOverlap: true,
infinite: false,
unconstrIter: 1,
userConstIter: 0,
allConstIter: 1,
ready: e => {
e.cy.fit()
e.cy.center()
}
}
this.graph = Cytoscape({ ... })
this.layout = this.grapg.makeLayout(layoutConfig)
this.layout.run();
这是我用来向图中添加新节点的方法:
const addElements = (elements: ElementSingular | ElementMultiple) => {
this.graph.nodes().forEach(node => {
node.lock();
})
this.graph.add(elements)
this.layout = this.graph.makeLayout(layoutConfig)
this.layout.on("layoutready", () => {
this.nodes().forEach(node => {
node.unlock();
})
})
this.layout.run()
this.graph.nodes().forEach(node => {
node.unlock();
})
}
我想做以下其中一项:
- 如果我想要完成的事情是可能的但我的代码没有实现它,请了解我做错了什么
- 了解为什么我无法完成它,即管理它的限制