【发布时间】:2019-06-22 09:36:53
【问题描述】:
我学习了cytoscape.js 和相关的扩展,并尝试了它的许多惊人的功能。
我发现当孩子的数量大于5并展开一个节点时,整个图形飞出屏幕。
我构建了更复杂的数据,每个父节点拥有 3 个父节点和 5 个子节点。任意两个子节点之间存在连接。 所以有3个父节点,15个子节点和14+13+12..1个链接。
综上所述,当链接较多时,布局行为看起来异常。
请参阅下面的演示。
你可以修改我的函数getInitData()的参数看看效果。
document.addEventListener('DOMContentLoaded', function(){
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
ready: function(){
var api = this.expandCollapse({
layoutBy: {
name: "cose-bilkent",
animate: 'end',
randomize: false,
fit: false,
idealEdgeLength : 150
},
fisheye: false,
animate: true,
undoable: false
});
api.collapseAll();
},
style: [
{
selector: 'node',
style: {
'background-color': '#ad1a66'
}
},
{
selector: ':parent',
style: {
'background-opacity': 0.333
}
},
{
selector: "node.cy-expand-collapse-collapsed-node",
style: {
"background-color": "darkblue",
"shape": "rectangle"
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': '#ad1a66'
}
},
{
selector: 'edge.meta',
style: {
'width': 2,
'line-color': 'red'
}
},
{
selector: ':selected',
style: {
"border-width": 3,
"border-color": '#DAA520'
}
}
],
elements : getInitData(3, 5)
});
var api = cy.expandCollapse('get');
var elements = null;
});
function getInitData(parentNum, childrenNum){
var data = [], children = [], i, j, n;
for(i = 0; i < parentNum; i++){
n = "parent"+i;
data.push({"group":'nodes',data:{"id":n}});
for(j = 0; j < childrenNum; j++){
children.push({"group":'nodes',data:{"id":n+"_child_"+j, parent:n}});
}
}
var s,t;
for(i = 0; i < children.length - 1; i++){
s = children[i].data.id;
for(j = i+1; j < children.length; j++){
t = children[j].data.id;
data.push({"group":'edges',data:{"id":s+"_"+t, source:s, target:t}});
}
}
return data.concat(children);
}
body {
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
font-size: 14px;
}
#cy {
z-index: 999;
width: 100%;
height: 100%;
}
h1 {
opacity: 0.5;
font-size: 1em;
font-weight: bold;
}
<script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="https://unpkg.com/cytoscape@3.1.0/dist/cytoscape.min.js"></script>
<!-- for testing with local version of cytoscape.js -->
<!--<script src="../cytoscape.js/build/cytoscape.js"></script>-->
<script src="https://unpkg.com/cytoscape-cose-bilkent@4.0.0/cytoscape-cose-bilkent.js"></script>
<script src="https://unpkg.com/cytoscape-expand-collapse@3.1.1/cytoscape-expand-collapse.js"></script>
<div id="cy"></div>
【问题讨论】:
-
更改以下内容并查看差异:graph init 中的
fit: true和 css 中#cy 中的position: absolute -
fit:true有效,而position: absolute似乎无效。我发现所有关于"cose-bilkent"的演示都需要将fit设置为true。这是否意味着fit:false可能有错误或工作不令人满意?如果设置fit=true并且图上只有一个 or 节点,你会得到一个非常大的圆形节点,看起来很难看。 -
Fit 是指将图形拟合到视口,如果要防止单个节点太大,请将图形的 padding 设置得更高
标签: javascript layout cytoscape.js