Custom Node Alignment for HierarchicLayouter

Tips & Tricks

Summary

This article describes how HierarchicLayouter can be customized to produce different node alignments.

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

Description

yFiles HierarchicLayouter provides no default means for specifying the alignment of nodes within a common layer but always places them in the center of the layer.
However the layout algorithm has been implemented in a modular fashion and different stages of the layout can be customized completely.
In order to modify the node alignment the drawer stage can be wrapped with a custom implementation.

Copy the attached java source file to your application, compile it and use the following lines of code to reconfigure HierarchicLayouter to work with the customization class:

// wrap the default drawer implemenation
hierarchicLayouter.setDrawer(
    new AlignmentDrawer(hierarchicLayouter.getDrawer()));

The provided implementation provides different default alignment modes. For a top-level alignment (in LayoutOrientation.TOP_TO_BOTTOM style) you can use the following code to configure the customization:

//register a DataProvider, providing alignment data for each node
graph.addDataProvider(
    AlignmentDrawer.NODE_ALIGNMENT_POINT_DPKEY, 
    new AlignmentDrawer.TopAlignmentDataProvider());
These DataProvider implementations will only work properly if the layout is performed in buffered mode.
Note that IncrementalHierarchicLayouter included in yFiles 2.3 offers this functionality by default. The behavior can be controlled for each node separately using the NodeLayoutDescriptor.setLayerAlignment(double) method.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Hierarchical Layout
Applies to:
yFiles.NET: 2.2, 2.3, 2.4
yFiles for Java 2: 2.0, 2.1, 2.2
Keywords:
HierarchicLayouter - layer - center - nodes - alignment - customization - align - top - bottom - HierarchicGroupLayouter - Drawer