在循环中的节点子集上调用布局不会正确布局节点

Calling layout on a subset of nodes in a loop does not properly layout the nodes

我有几组节点,我需要每组都是围绕不同位置的自己的圆圈。

我有一个将节点添加到图表的 for 循环,最后获取这些节点,并尝试将它们循环化。

然而,当我 运行 程序时,节点以网格格式结束。 如何获得预期的行为?

    for( group in groups ){
        cy.add([
                { group:'nodes', data: { id: 1, customgroup: group}, position:{x: 0,y: 0} },
                { group:'nodes', data: { id: 2, customgroup: group}, position:{x: 0,y: 0} },
                { group:'nodes', data: { id: 3, customgroup: group}, position:{x: 0,y: 0} },
                { group:'nodes', data: { id: 4, customgroup: group}, position:{x: 0,y: 0} },
                { group:'nodes', data: { id: 5, customgroup: group}, position:{x: 0,y: 0} },
                { group:'edges', data: { source: '1',target: '2', weight:1} },
                { group:'edges', data: { source: '2',target: '3', weight:1} },
                { group:'edges', data: { source: '3',target: '4', weight:1} },
                { group:'edges', data: { source: '4',target: '5', weight:1} },
                { group:'edges', data: { source: '5',target: '1', weight:1} }
               ]);
        cy.elements('[customgroup="'+group+'"]').layout({ name: 'circle', radius:1, startAngle: 13*((Math.PI)/8)});
    }

感谢您的帮助!

几个小时没有进展后,我走开了,在壁炉里生了火,喝了一杯苏格兰威士忌。就在我一夜之间完全忘记了这个问题的那一刻,解决方案击中了我。

Cytoscape 在初始化时可以选择指定节点的布局。当我实现上面的内容时,我从 init 中删除了布局以避免两次定位节点。

处理此问题的正确方法是设置:

layout: {
        name: 'preset',
        padding: 10
      },

这允许我的自定义代码正确处理所需的行为,并且还可以防止节点的双重定位。