How do I prevent nodes from overlapping

181 views Asked by At

How do I prevent nodes in a graph from stacking on each other? I want there to be a minimum distance between nodes and if someone was to drag a node over another the graph throws an error to the user. I saw this solution and tried it but it doesnt work Prevent node overlap in JGraphX

I'll appreciate any ideas.

Nodes overlapping

1

There are 1 answers

0
cordback On

For rearranging your diagram automatically with some distance between them you can try to use something like this:

let layout = new mxHierarchicalLayout(this.graph, mxConstants.DIRECTION_WEST);
layout.intraCellSpacing = 50; //horizontal
layout.interRankCellSpacing = 200; //vertical
layout.execute(this.parent);

The mxHierarchicalLayout method will automatically rearrange your current nodes. The other two settings will give spacing between them ( horizontally and vertically )

Also, run this before rendering the screen or if you want to change after the display has already rendered, just this.graph.refresh() if needed