elk
elk copied to clipboard
Fixing node positions in the stress layout
I am using the stress majorization layout but I am having issues with keeping nodes on a fixed position. Initially, I let the layout run to determine first positions. Later more nodes and links can be added and I recalculate the layout to include them. This currently leads to everytime different layouts or at least different positions which makes keeping a mental map of the graph very hard for the user. I see that the stress majorization algorithm supports setting nodes as fixed. But when I do that to all but the new nodes then they all get positioned almost on the same spot without any spread.
Looking through the code I found that it seems to first run the Force Layout which does not take into consideration fixed node positions and only after runs the stress majorization layout.
Here is my graph with its settings.
"id":"root",
"children":[
{"id":"0" },
{"id":"1" },
{"id":"2" },
{"id":"3" }
],
"layoutOptions":{
"algorithm":"stress",
"elk.padding":"[top=50.0,left=50.0,bottom=50.0,right=50.0]",
"nodeSize.constraints":"[]",
"interactive":"false",
"omitNodeMicroLayout":"true",
"epsilon":"1.0E-4",
"hierarchyHandling":"SEPARATE_CHILDREN"
},
"edges":[
{
"id":"0",
"sources":[ "0" ],
"targets":[ "1" ]
},
{
"id":"1",
"sources":[ "1" ],
"targets":[ "2" ]
},
{
"id":"2",
"sources":[ "2" ],
"targets":[ "0" ]
},
{
"id":"3",
"sources":[ "2" ],
"targets":[ "3" ]
}
]
}
Note on the interactive setting: When enabled all nodes receive the same position. It is always (50.5|50.5). No matter what graph I import. No matter whether omitNodeMicroLayout is true or false. Only when rerunning the layout different positions are assigned. But they are all aligned on a diagonal line as each node's y value is the same as its x value.
Here are the visualized results I am currently achieving:
- small example graph after stress majorization layout without fixing any positions:
- fixing the positions on 2 of the nodes (the red ones) results in this layout:
- without fixed positions but interactive = true. First all nodes receive the same position (50.5|50.5). And when rerunning the layout it results in this:
For my use case I really like the outcome of the stress majorization layout. I'd be grateful to get some tips on how to correctly fix node positions or use the interactive setting. Thanks!
fixed
does only take effect after an initial run of elk.force. interactive
is designed to work with nodes that already have a position as seen here. Nodes that are neither fixed nor have a position are handled as you described.