Group Nodes, Labels, and IncrementalHierarchicLayouter
Tips & TricksSummary
Description
In their most basic setup, yFiles layout algorithms work on nodes and edges only when calculating graph layouts. IncrementalHierarchicLayouter (IHL), however, is one of the algorithms that can be configured to take node labels into account as well. Node label support works by reserving space for the union of a node's bounding box and all associated label boxes without actually resizing any nodes. This policy is consistent with the behavior one usually expects from a layout algorithm. For group nodes the situation is slightly different, because one expects group nodes to visually contain all of their child nodes. As a consequence, IHL adjusts group node bounds accordingly.
Since group nodes can grow and shrink as a result of an IHL run, a closely related feature would be to have group nodes grow and shrink in such a way as to encompass the bounds of all their associated labels. Although IHL does not come with out-of-the-box support for the aforementioned resizing policy (technically, labels are not considered group content), as of yFiles release 2.5.0.1, only little additional effort is needed to achieve the desired result nonetheless.
The first step is to wrap IHL's default DrawingDistanceCalculator in the attached custom LabelAwareDrawingDistanceCalculator implementation. The important part being the simple but effective changes to method getMinDistance(LayoutGraph, Layer, LayoutDataProvider, Node, Node).
Please note, that this method returns a minimum. IHL is actually allowed to assign much more space to a group node than actually needed. To prevent that from happening, one can use horizontal group compaction, a feature of IHL's SimplexNodePlacer. LabelAwareIhlModule shows how to activate said feature as well as how to configure IHL to work with LabelAwareDrawingDistanceCalculator.
The DrawingDistanceCalculator is never invoked for empty group nodes, though. (IHL does not differentiate between normal nodes and empty group nodes.) In order to get a consistent behavior for all group nodes, one has to manually adjust node bounds for empty ones. Fortunately, this can be easily accomplished with a specialized layout stage, as demonstrated by the attached AdaptNodeToLabelWidths stage. This stage is also incorporated into LabelAwareIhlModule.