How to Instruct the OrganicLayouter to Fix Some Node Positions

Questions & Answers

Summary

This article describes how the suite of organic layout algorithms can be instructed to fix the positions of a subset of nodes. It applies to both the SmartOrganicLayouter and the OrganicLayouter, both in flat mode and hierarchically nested mode.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

It is often useful to specify a subset of nodes that should be laid out only or a subset of nodes that should not be laid out during a layout run. Both OrganicLayouter and SmartOrganicLayouter support this feature, even if the two subsets are in the same graph component.
In order to specify the subset to be laid out, bind a boolean DataProvider to the graph using a predefined DataProvider key.
This article describes different ways of how to pass data to layout algorithms.
The following piece of code uses the current selection to specify those nodes to be laid out by the layout algorithm:

//Add the DataProvider to the graph
graph2d.addDataProvider(SmartOrganicLayouter.NODE_SUBSET_DATA,
     Selections.createSelectionNodeMap(graph2d));

//Configure the algorithm to use the data
smartOrganicLayouter.setScope(SmartOrganicLayouter.SCOPE_SUBSET);

//and run the layout
//(buffered in this case)
new BufferedLayouter(smartOrganicLayouter).doLayout(graph2d);

//remove the DataProvider after the layout
graph2d.removeDataProvider(SmartOrganicLayouter.NODE_SUBSET_DATA);
Starting with version 2.7 yFiles for Java Complete provides the new class Graph2DLayoutExecutor which eases performing a layout. We recommend using this class instead of using BufferedLayouter directly, if your distribution includes the Graph2DLayoutExecutor.
Both OrganicLayouter and SmartOrganicLayouter have to be instructed to make use of the information bound to the graphs. This can be achieved by invoking either the OrganicLayouter.setSphereOfAction(byte) method with OrganicLayouter.ONLY_SELECTION as the argument or the SmartOrganicLayouter.setScope(byte) method with SmartOrganicLayouter.SCOPE_SUBSET as the argument respectively.
The positions of the nodes that belong to the fixed subset may still be changed by the ComponentLayouter (the default component layouting algorithm will move the entire component back to the origin after the layout has been performed). However relative node positions of fixed nodes will stay the same.
The ComponentLayouter instance can of course be disabled or customized to your needs.

When using SmartOrganicLayouter, enabling the smart component layout logic using the setSmartComponentLayoutEnabled(boolean) method will prevent ComponentLayouter from moving components that entirely belong to the subset of fixed nodes.

When using SmartOrganicLayouter to layout a subset of nodes, its feature nodeOverlapsAllowed should be enabled, otherwise unselected nodes might be moved to resolve node overlaps.

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Organic Layout
yFiles WPF > yFiles WPF Algorithms > Automatic Graph Layout > Organic Layout
yFiles.NET > yFiles.NET Algorithms > Automatic Graph Layout > Organic Layout
Applies to:
yFiles for Java 2: 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
yFiles.NET: 3.1, 3.2
yFiles WPF: 1.0
Keywords:
SmartOrganicLayouter - fix positions - incremental layout - OrganicLayouter - node subset - ComponentLayouter