This layout algorithm arranges graphs with a tree structure.
Remarks
Layout Style
TreeLayout provides multiple different arrangements of trees and subtrees. It is easy to customize the order of edges, the port assigner and the arrangement of the nodes for each subtree.
Tree layout algorithms are commonly applied to visualize relational data and produce diagrams of high quality that are able to reveal possible hierarchical properties of the graph. More precisely, they find applications in dataflow analysis, software engineering, bioinformatics and business administration.
Concept
The layout algorithm starts from the leaves and continues with their parents, then with the parents of the parents and so on. When a node is processed, the algorithm will use the corresponding ISubtreePlacer instance to move its children (along with their subtrees) to a suitable position and to route the outgoing edges of this node. Then, the next local root node will be processed.
To avoid moving all nodes several times and to know the area that the subtrees occupy, the layout algorithm uses SubtreeShapes. These SubtreeShapes are moved and merged during layout calculation.
Features
Each subtree can have a different style of node placement. ISubtreePlacers are responsible for arranging subtrees and their common root node. They can be specified separately for each local root via layout data property subtreePlacers.
A custom node can be defined as root of the tree using layout data property treeRoot.
The layout algorithm can be configured to reserve space for node labels and place the edge labels along edges such that the labels won't overlap with other graph elements. Edge labels are placed according to the information stored in an EdgeLabelPreferredPlacement instance. However, the placement along the edge will only affect the order of multiple labels at the same edge. The algorithm will always place the labels close to the target node.
Grouping of nodes can also be handled by this layout algorithm. It is important that a group node contains a whole subtree. Otherwise, the group nodes may overlap with each other or with other nodes. Furthermore, the user may specify minimum size constraints for each group node using IMapper<K,V> key MINIMUM_GROUP_NODE_SIZE_DATA_KEY.
TreeLayout supports custom sorting of the outgoing edges of a node. For example, a ISubtreePlacer instance that implements IFromSketchSubtreePlacer provides a comparator that keeps the current order of siblings, allowing to extend the graph incrementally.
nodeTypes are considered such that the type of the nodes is used as a criterion for sorting the child nodes of a local root node, with the effect that nodes of the same type are placed consecutively, if possible. The primary ordering criterion is still specified by the childOrder.
How port placement constraints are handled depends on the selected portAssigners. In addition, the PortPlacementStage is applied as a postprocessing step.
This layout algorithm can only handle graphs with a tree structure. To make it applicable to general graphs, the treeReductionStage is used by default. This stage will temporarily remove some edges of the input graph until a tree is obtained. After the layout calculation, the stage will reinsert the edges that were removed and route them separately.
For particularly fast layout computations you may use the following configuration. Note that this limits the layout to specific styles. All settings not shown in the snippet must be set to their default values.
const subtreePlacer = new LevelAlignedSubtreePlacer()
subtreePlacer.busAlignment = busAlignment
subtreePlacer.layerSpacing = layerSpacing
subtreePlacer.rootAlignment = SubtreeRootAlignment.CENTER_OF_PORTS
subtreePlacer.edgeRoutingStyle = routingStyle
subtreePlacer.spacing = spacing // must be at most busAlignment * layerSpacing
const treeLayout = new TreeLayout({
defaultSubtreePlacer: subtreePlacer,
})
Layout Stages
This class provides a configurable pipeline that contains various ILayoutStages. Each ILayoutStage can incorporate preprocessing or postprocessing steps into the layout calculation to streamline the input graph and enhance the resulting layout. Additionally, custom ILayoutStages can be added and executed either before or after the predefined ones.
The following default ILayoutStages are included:
- PortPlacementStage: Assigns edges to ports. The property routeCorrectionPolicy is set to MOVE_PORTS.
- GroupHidingStage: Removes group nodes and their adjacent edges before layout processing, and reinserts them afterward. Note: This stage is disabled by default.
- SubgraphLayoutStage: Filters a graph to include only specific nodes and edges from a subgraph while maintaining the positions of excluded elements. Note: This stage is disabled by default.
- ComponentLayout: Arranges graph components with customizable styles.
- GenericLabeling: Efficiently places node and edge labels. The property fillEmptyScope is set to
false
and stopDuration is set to ZERO. - OrientationStage: Changes the layout orientation in four possible directions, with or without mirroring on the x or y-axis. The property edgeLabelPlacement is set to IGNORE.
- SelfLoopRouter: Routes self-loops in a graph, allowing for either orthogonal or rounded routing styles.
- ParallelEdgeRouter: Routes multiple edges between the same nodes in parallel.
- TreeReductionStage: Temporarily reduces general graphs to tree structures, allowing them to be processed by a tree layout algorithm. The property nonTreeEdgeRouter is set to a StraightLineEdgeRouter instance.
With these layoutStages the layout algorithm is configured well, so they usually don't need to be changed.
Default Values of Properties
allowMultiParent | false | Multi-parent structures are not allowed. |
defaultPortAssigner | TreeLayoutPortAssigner
| |
defaultSubtreePlacer | SingleLayerSubtreePlacer
| |
edgeLabelPlacement | INTEGRATED
| Edge labels are placed by the layout algorithm. |
nodeLabelPlacement | CONSIDER
| Node labels are considered. |
Type Details
- yFiles module
- algorithms
See Also
Constructors
Creates a new TreeLayout instance with default settings.
Parameters
A map of options to pass to the method.
- fromSketchMode - boolean
- Whether or not to consider the initial coordinates of the graph elements. This option sets the fromSketchMode property on the created object.
- layoutOrientation - LayoutOrientation
- The layoutOrientation of this layout algorithm. This option sets the layoutOrientation property on the created object.
- defaultSubtreePlacer - ISubtreePlacer
- The default ISubtreePlacer instance that arranges all subtrees of local roots that do not have an individual subtree placer assigned. This option sets the defaultSubtreePlacer property on the created object.
- defaultPortAssigner - ITreeLayoutPortAssigner
- The default ITreeLayoutPortAssigner used for those nodes that do not have an individual port assigner specified. This option sets the defaultPortAssigner property on the created object.
- nodeLabelPlacement - NodeLabelPlacement
- How the layout handles the position of node labels. This option sets the nodeLabelPlacement property on the created object.
- edgeLabelPlacement - EdgeLabelPlacement
- How the layout handles the position of edge labels. This option sets the edgeLabelPlacement property on the created object.
- allowMultiParent - boolean
- Whether or not multi-parent structures are allowed for this tree layout. This option sets the allowMultiParent property on the created object.
Properties
Gets or sets whether or not multi-parent structures are allowed for this tree layout.
Remarks
Default Value
false
.Multi-parent structures are not allowed.
Property Value
true
if multi-parent structures are allowed, false
otherwiseSee Also
Sample Graphs
As for the different ways in which the ISubtreePlacers route their edges, multi-parent structures only work for some ISubtreePlacer implementations:
- SingleLayerSubtreePlacer delivers the best results for multi-parent structures. However, routing style ORTHOGONAL_AT_ROOT as well as root alignments LEADING_ON_BUS and TRAILING_ON_BUS are not supported.
- DendrogramSubtreePlacer supports multi-parent structures.
- BusSubtreePlacer supports multi-parent structures.
- LeftRightSubtreePlacer supports multi-parent structures.
The specified ITreeLayoutPortAssigner is ignored. The ports are always placed at the center.
Gets the ComponentLayout from the layoutStages of this instance.
Remarks
Throws
- Exception({ name: 'InvalidOperationError' })
- If there is no instance of the respective type in the
.
Gets or sets the default ITreeLayoutPortAssigner used for those nodes that do not have an individual port assigner specified.
Remarks
Default Value
TreeLayoutPortAssigner.Property Value
Throws
- Exception({ name: 'ArgumentError' })
- if the default
is set to null
See Also
Gets or sets the default ISubtreePlacer instance that arranges all subtrees of local roots that do not have an individual subtree placer assigned.
Default Value
SingleLayerSubtreePlacer.Property Value
Throws
- Exception({ name: 'ArgumentError' })
- if the default
is set to null
See Also
Gets or sets how the layout handles the position of edge labels.
Default Value
Property Value
See Also
Sample Graphs
Gets or sets whether or not to consider the initial coordinates of the graph elements.
Remarks
Default Value
false
.The existing drawing is not used as a sketch.
See Also
Gets or sets the layoutOrientation of this layout algorithm.
Gets the mutable stack of ILayoutStage that will be applied to this layout.
Gets or sets how the layout handles the position of node labels.
Default Value
Property Value
See Also
Sample Graphs
Gets the ParallelEdgeRouter from the layoutStages of this instance.
Remarks
Throws
- Exception({ name: 'InvalidOperationError' })
- If there is no instance of the respective type in the
Gets the SelfLoopRouter from the layoutStages of this instance.
Remarks
Throws
- Exception({ name: 'InvalidOperationError' })
- If there is no instance of the respective type in the
Gets the TreeReductionStage from the layoutStages of this instance.
Remarks
Throws
- Exception({ name: 'InvalidOperationError' })
- If there is no instance of the respective type in the
See Also
Methods
Calculates a tree layout of the graph.
Remarks
Parameters
A map of options to pass to the method.
- graph - LayoutGraph
- the input graph
See Also
Implements
Arranges the given graph as a tree.
createLayoutData
(graph: LayoutGraph) : TreeLayoutData<LayoutNode,LayoutEdge,LayoutNodeLabel,LayoutEdgeLabel>Returns an instance of LayoutData<TNode,TEdge,TNodeLabel,TEdgeLabel> that can be used to perform item-specific configurations for the TreeLayout.
Remarks
Parameters
A map of options to pass to the method.
- graph - LayoutGraph
- the graph that determines the generic type arguments of the created layout data
Returns
- ↪TreeLayoutData<LayoutNode,LayoutEdge,LayoutNodeLabel,LayoutEdgeLabel>
- an instance of layout data that can be used to perform item-specific configurations for the given TreeLayout.
Returns an instance of LayoutData<TNode,TEdge,TNodeLabel,TEdgeLabel> that can be used to perform item-specific configurations for the TreeLayout.
Remarks
Parameters
A map of options to pass to the method.
- graph - IGraph
- the graph that determines the generic type arguments of the created layout data
Returns
- ↪TreeLayoutData<INode,IEdge,ILabel,ILabel>
- an instance of layout data that can be used to perform item-specific configurations for the given TreeLayout.
LayoutExecutor
type is available at runtime.Returns the ITreeLayoutPortAssigner instance that places the ports of the connecting edges of the given node.
Remarks
Parameters
A map of options to pass to the method.
- localRoot - LayoutNode
- the root of the local subtree
Returns
- ↪ITreeLayoutPortAssigner
- the ITreeLayoutPortAssigner instance that assigns the ports of the edges at the given node
See Also
Returns the ISubtreePlacer instance that is used for the placement of the local root node and the SubtreeShapes.
Remarks
The method may be overridden to return custom ISubtreePlacer instances. It is possible to return a shared instance for multiple different nodes because the instances are not used after subsequent calls to this method.
The current implementation returns the ISubtreePlacer defined via the layout data property subtreePlacers. It falls back to the defaultSubtreePlacer if there is no specific ISubtreePlacer for the given node.
Parameters
A map of options to pass to the method.
- localRoot - LayoutNode
- the root of the local subtree
Returns
- ↪ISubtreePlacer
- the ISubtreePlacer instance that places the subtree below the given local root node
See Also
Provides access to the SubtreeShape for the given node.
Remarks
The SubtreeShape contains information about the current extent and location of the subtree rooted at the node. It should only be modified during the layout of the parent node. Also, it won't be initialized before the layout of the corresponding subtree is calculated.
This method updates the SubtreeShape in layoutRoot.
Parameters
A map of options to pass to the method.
- localRoot - LayoutNode
- the root of the subtree
Returns
- ↪SubtreeShape?
- the SubtreeShape instance if it has already been calculated,
null
otherwise
Calculates the layout for the given root node and its subtrees.
Remarks
This method is invoked for each node in the tree exactly once. Thus, children are always handled before their parents. In this manner, the subtrees already have a layout and can be arranged with their parent.
To retrieve the shapes of the subtrees of all children of the local root, this method uses getSubtreeShape.
The method may be overridden to add configuration code.
Parameters
A map of options to pass to the method.
- graph - LayoutGraph
- the input graph the tree layout algorithm is working on
- localRoot - LayoutNode
- the root of the subtree whose layout is calculated
Returns
- ↪SubtreeShape
- the combined SubtreeShape of the local root node and all of its children and connecting edges
Modifies the calculated SubtreeShape for the given node that consists only of the bounds of this particular node.
Remarks
It may also include node labels or node margins.
This method may be overridden to modify the SubtreeShape for the given node immediately after its calculation. The modified shape will be respected during the remaining layout calculations.
Parameters
A map of options to pass to the method.
- node - LayoutNode
- the given node
- subtreeShape - SubtreeShape
- the calculated subtree shape for the given node
Returns
- ↪SubtreeShape
- the SubtreeShape that describes the node's shape for the remaining layout calculations.
Constants
A data key for defining the priority of critical edges.
Remarks
Assign a positive priority to "critical" edges, or 0
to "non-critical" edges.
The layout algorithm tries to align each node pair that is connected by a critical edge. This feature can, for example, be used for highlighting different edge paths that are important for a user. Conflicts between different critical edges are always resolved in favor of the higher priority.
This feature is only supported by the following ISubtreePlacers:
See Also
Sample Graphs
A data key for retrieving descriptors for nodes in multi-parent structures.
Remarks
See Also
A data key for specifying the comparer for the outgoing edges.
Remarks
See Also
A data key for assigning ports to nodes.
Remarks
null
if the defaultPortAssignerdefault assigner should be used.See Also
A data key for marking the node that will be used as root node of the tree.
Remarks
true
to a node if the node should be considered as the root node of the tree, or false
otherwise.See Also
A data key for specifying a child subtree placer for each node.
Remarks
null
if the defaultSubtreePlacer should be used.