Tips for Passing Data to Layout Algorithms
Tips & TricksSummary
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)); ```