yFiles for HTML provides many flexible layout algorithms
which arrange the graph elements in different styles.
A lot of properties and customizable descriptors offer a maximum of flexibility.
However, sometimes it is necessary to customize the predefined algorithms even more.
In this case, developers can write a custom layout stage or even a complete layout algorithm.
A layout stage encapsulates arbitrary layout functionality, and provides a general means to string together
multiple stages into a compound layout process. It is based on the interface ILayoutStage which extends
ILayoutAlgorithm. Just like any other ILayoutAlgorithm its layout gets invoked with a call
to applyLayout. In addition, it has a coreLayout property,
which takes another ILayoutAlgorithm implementation. The coreLayout's
applyLayout method is supposed to be invoked somewhere during the stage’s
applyLayout method.
It is recommended to derive a custom layout stage from class LayoutStageBase. Its method
applyLayoutCore should be invoked to safely call the coreLayout's
applyLayout method. Safely means that if no coreLayout is set,
applyLayoutCore silently returns, doing nothing.
yFiles for HTML already provides a large number of layout stages. Even the full-fledged layout styles and edge routers
are implemented as layout stages, too. Therefore, these layout stages can be plugged together to realize
complex layout processes tailored to your requirements.
Common Purposes of a Layout Stage
Layout stages can be used to add additional steps to the layout process. Generally, a layout stage performs
preprocessing steps on the input graph before the core layouter’s invocation, and postprocessing steps thereafter.
A few examples (although only a small subset of possible applications are):
Temporarily adding group nodes or edges to emphasize relations between elements which are not represented by the graph structure
Temporarily hide elements which should not influence the overall layout
Post-Process coordinates, e.g. prolong edges from the node’s rectangular bounding box to the real outline of the node
The Custom Layout Stage demo shows different use cases for custom layout stages.
Example: Decorating a Graph
A typical use case for a custom layout stage is to add or hide some elements. In this example
we hide some edges which do not represent structural information. This is usually achieved in three steps:
Hiding (i.e. temporarily removing) these edges from the graph
Applying the core layout
un hiding the edges again. In this example, an edge router is applied exclusively to these edges
The following example shows how to achieve this.
Note that it is good practice to put the clean up step in a finally block.
In this example, it is assumed that there is a data provider which returns true for edges
which should be hiddeb. When applying the layout on an IGraph or a GraphComponent the most convenient way
to set up such a provider is to use a GenericLayoutData instance.
This is shown in example Applying the custom layout on a GraphComponent:
Example: Pre-Processing
This example shows a pre-processing. Before the actual layout is done by the coreLayout
some of the nodes are resized.
In this example it is assumed that there is a data provider which returns the size of the node. For simplicity
this is a number which is used for both width and height of the node. If no value is set (default: 0)
the current size is not changed.
Providing Data for Custom Layout Stages
Most of the above examples need custom data. It is usual and to provide these data in a data provider.
Defining data providers for the layout graph is discussed in section Binding Data to Graph Elements.