cytoscape.js - 试图将节点扩展为空父节点
问题描述
我正在使用cytoscape.js和cytoscape.js-expand-collapse在我的图形结构中创建动态层次结构。我希望能够动态创建一个折叠(合并)节点,该节点可能会扩展、删除或可能与其他节点重新合并。我每次打电话都有麻烦nodes.move({parent:null})
。它们与图表分离,我无法将它们重新附加到新的父节点。如果我打电话restore
给他们,我会看到错误,说他们已经存在于图表中。
merge
并且unmerge
本身在没有现有折叠节点的简单情况下工作正常。然而,对已经包含复合折叠节点的东西调用合并会破坏事情。我将得到没有先前折叠的合并候选者的子节点的结果合并节点
更新 #1我意识到我的问题是返回错误的移动节点。对集合调用 .move 会返回一组新节点。所以unmerge
应该返回那些。
更新#2 cytoscape-expand-collapse 实用程序在父节点折叠/展开期间通过在元边缘上的“originalEnds”数据道具中隐藏节点数据来进行一些内部簿记。因此,如果我现在通过将节点移入/移出父节点来动态更改图形,这些 originalEnds 指针会不同步,从而导致无限循环。我正在做一些我自己的额外节点跟踪作为一种解决方法。
function merge(nodes){
//if one of the nodes is a compound itself, first uncollapse, then merge
var collapsed = nodes.filter(function(c){return typeof c.data('collapsedChildren')!=='undefined';});
if (collapsed.length>0) {
// for now just assume collapsed is a length of 1
var unmerged = unmerge(collapsed); //unmerged should be now the former collapsed children
nodes = nodes.subtract(collapsed).union(unmerged.nodes());
}
var parentNode = cy.add({group:'nodes', data: {id: parentID}});
nodes.move({parent: parentID});
collapseApi.collapse(parentNode);
return parentNode;
}
function unmerge(parentNode){
collapseApi.expand(parentNode);
var children = parentNode.children();
var moved = children.move({parent:null});
//at this point children become "detached" from the graph
// .removed() returns true, however, calling restore() logs an error and they are still technically in the graph
parentNode.remove();
return moved;
}
解决方案
ele.move()
在 cytoscape>=3.4 中有更方便的实现:元素被就地修改,而不是创建替换元素。
使用返回的集合的旧代码ele.move()
仍然可以工作。完全不必使用返回的集合可以简化新代码。
推荐阅读
- c++ - 将 Eigen::MatrixXd 矩阵转换为 cgal 容器类型
- reactjs - 如何在 Electron 应用程序中实现 ReactJs?
- python - 如何在 Matplotlib 中获取包装文本的高度?
- mysql - 如何找到针对不同数据库的查询执行时间差异的原因?
- ios - 如何使用iOS图表仅在饼图中显示所选切片上的文本
- java - JAXB 覆盖 package-info.java:“命名空间”应该是什么?
- java - 用于查找连续字符串中的两个字符是否为数字的程序在java中不起作用
- nlp - 如何在spaCy中获取句子中标记的索引?
- openid - OpenID:未经身份验证的 POST 身份验证后变为 GET
- react-native - 领域反应本机模式版本控制