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.
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.:
Graph2D g = view.getGraph2D();
g.addDataProvider(Layouter.SELECTED_NODES, Selections.createSelectionNodeMap(g));
Layouter layouter = createLayouter();
layouter.doLayout(g);
graph.removeDataProvider(Layouter.SELECTED_NODES);
|
HierarchicLayouter
e.g. uses another important DataProvider key:
PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY
and
PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY
can be registered with the graph and used to assign port constraints to edges. E.g.:
sourcePortMap = graph.createEdgeMap();
targetPortMap = graph.createEdgeMap();
graph.addDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY,sourcePortMap);
graph.addDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY,targetPortMap);
sourcePortMap.set(edge1, PortConstraint.create(PortConstraint.NORTH));
sourcePortMap.set(edge2, PortConstraint.create(PortConstraint.NORTH));
targetPortMap.set(edge2, PortConstraint.create(PortConstraint.SOUTH));
|
| Warning |
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. |
| Note |
|
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. |