Tips for Passing Data to Layout Algorithms
Applies to: yFiles 2.4, yFiles 2.3, yFiles 2.2, yFiles 2.1, yFiles 2.0, yFiles.NET 2.4, yFiles.NET 2.3, yFiles.NET 2.2 print article email article

Type: Tips & Tricks

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Concepts

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.:
//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);
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.:
//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));
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.

Keywords: data - layout - algorithms - DataProvider - EdgeMap - NodeMap - PortConstraint - PortConstraintKeys - databinding - binding - layouter

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2008 yWorks · ALL RIGHTS RESERVED imprint | top | home