Make edges start from the same port using OrthogonalEdgeRouter

Tips & Tricks

Summary

This article explains how you can avoid OrthogonalEdgeRouter from setting edges side by side on a node and make them start from one port instead.

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

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 a default EdgeMap with the graph that contains the node. This edge map is used as a data provider later on.
  • 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 edge map.
  • Properly registers the edge map as a data provider with the graph so that it can be retrieved by OrthogonalEdgeRouter.

Graph graph = node.getGraph();
// Create an edge map that is used as a data provider later on. 
EdgeMap pcMap = graph.createEdgeMap();

for (EdgeCursor ec = node.outEdges(); ec.ok(); ec.next())
{
  Edge e = ec.edge();
  // Set the coordinates for the edge's source port. 
  graph.setSourcePointRel(e, new YPoint(-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(PortConstraint.SOUTH, true);
  // Establish a mapping from edges to port constraints. 
  pcMap.set(e, pc);
}

// Register the edge map as a data provider with the graph. 
// Use the "well-known" look-up key defined in interface PortConstraintKeys. 
// The look-up key specifies that the data provider is to be used for the 
// source ports only. 
graph.addDataProvider(PortConstraint.SOURCE_PORT_CONSTRAINT_KEY, pcMap);

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.)

The current implementation of OrthogonalEdgeRouter requires the option ROUTE_ALL_EDGES with method setSphereOfAction(byte) when there are multiple edges with a strong port constraint at the same coordinate.
The same approach applies to EdgeRouter that was added in yFiles for Java 2.10. It supports "strong" and "weak" port constraints.

Categories this article belongs to:
yFiles for Java > yFiles Layout > Automatic Graph Layout > Orthogonal Edge Routing
Applies to:
yFiles for Java 2: 2.6, 2.7, 2.8, 2.9
Keywords:
OrthogonalEdgeRouter - PortConstraint - port constraint - setSphereOfAction(byte) - ROUTE_ALL_EDGES