Make edges start from the same port using OrthogonalEdgeRouter
Tips & TricksSummary
Description
One of the main objectives of class
OrthogonalEdgeRouter
is to find non-overlapping routes for edges.
This is why, by default, OrthogonalEdgeRouter will set starting/ending edges side by side at source/target nodes.
However, you can make these edges start from one port by setting a strong port constraint for your edges, i.e., you give an explicit coordinate where the edges should start/end.
The following sample code fragment shows how to specify a strong port constraint for all outgoing edges at a given node.
What the sample code does:
-
Creates an
IMapper
to hold a PortConstraint object for each edge of a given
IGraph
.
- For each outgoing edge sets explicit coordinates for the source end. The coordinates are specified relative to the node's center.
-
Creates a strong port constraint using the appropriate static method of class
PortConstraint
.
The port constraint is then associated with an edge by means of the IMapper.
- Adds the IMapper to the mapper registry of the graph so that it can be retrieved by OrthogonalEdgeRouter.
// 'graph' is of type yWorks.yFiles.UI.Model.IGraph.
// 'specificNode' is of type yWorks.yFiles.UI.Model.INode.
// Create a mapper.
IMapper<IEdge, PortConstraint> pcMap = new DictionaryMapper<IEdge, PortConstraint>();
foreach (IEdge e in graph.OutEdgesAt(specificNode)) {
// Set the coordinates for the edge's source port. (Actually, this could also
// be done anywhere prior to invoking the layout algorithm.)
graph.SetRelativeLocation(edge.SourcePort, new PointD(-10, 20));
// Strong port constraint that determines an edge end to connect to the
// lower (South) side of its respective node. The actual end point is at a
// fixed coordinate.
PortConstraint pc = PortConstraint.Create(PortSide.South, true);
// Establish a mapping from edges to port constraints.
pcMap[e] = pc;
}
// Add the mapper to the mapper registry of the graph.
// Use the "well-known" look-up key defined in class PortConstraintKeys as tag.
// Note that the above defined port constraints are set so that they apply to
// the source ends of their respective edges only.
graph.MapperRegistry.AddMapper(PortConstraintKeys.SourcePortConstraintDpKey, pcMap);
// Invoke the layouter.
graph.ApplyLayout(new IncrementalHierarchicLayouter());
OrthogonalEdgeRouter supports both "strong" and "weak" port constraints. (A strong port constraint is a combination of an explicit coordinate together with a node side, weak means only a specific node side.)