Tips for Passing Data to Layout Algorithms

Tips & Tricks

Summary

Most of the layout algorithms accept additional data bound to nodes and edges. This article describes common use cases and gives tips for the correct usage of this feature.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

In yFiles most of the layout algorithms not only take the LayoutGraph as an input but also allow for binding data to single nodes and edges. This data will be interpreted by the algorithms and may lead to totally different results.
The Graph.addDataProvider(java.lang.Object, y.base.DataProvider) method plays a key role to this mechanism:
Data is bound to node and edge instances using DataProvider instances that are registered using public keys with the graph instance using the addDataProvider() method.
One popular example of a public dataprovider key is Layouter.SELECTED_NODES. This key can be used to tell various layout algorithms to work on a selection only. E.g.:``` //get the graph instance Graph2D g = view.getGraph2D(); //create a DataProvider and bind it to the graph g.addDataProvider(Layouter.SELECTED_NODES, Selections.createSelectionNodeMap(g)); // create the layouter Layouter layouter = createLayouter(); // calculate the layout layouter.doLayout(g); // cleanup: remove the DataProvider graph.removeDataProvider(Layouter.SELECTED_NODES);

<api-link ref-type="yfiles_java_api" type="y.layout.hierarchic.HierarchicLayouter">HierarchicLayouter</api-link> e.g. uses another important DataProvider key: <api-link ref-type="yfiles_java_api" type="y.layout.PortConstraintKeys" member="SOURCE_PORT_CONSTRAINT_KEY">PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY</api-link> and <api-link ref-type="yfiles_java_api" type="y.layout.PortConstraintKeys" member="TARGET_PORT_CONSTRAINT_KEY">PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY</api-link> can be registered with the graph and used to assign port constraints to edges. E.g.:

//create DataProvider implementations sourcePortMap = graph.createEdgeMap(); targetPortMap = graph.createEdgeMap();

// bind them to the graph using the predefined keys graph.addDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY,sourcePortMap); graph.addDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY,targetPortMap);

// assign some constraints to edges sourcePortMap.set(edge1, PortConstraint.create(PortConstraint.NORTH)); sourcePortMap.set(edge2, PortConstraint.create(PortConstraint.NORTH)); targetPortMap.set(edge2, PortConstraint.create(PortConstraint.SOUTH)); ```

Be careful when writing custom DataProvider implementations and not using BufferedLayouter. During the layout process the graph is very likely to be modified and node indices and edge indices might get messed up. So don't rely on indices when using non-buffered layouts.
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.

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Concepts
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 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: 2.2, 2.3, 2.4
Keywords:
data - layout - algorithms - DataProvider - EdgeMap - NodeMap - PortConstraint - PortConstraintKeys - databinding - binding - layouter