Make edges start from the same port using OrthogonalEdgeRouter
Applies to: yFiles 2.6, yFiles WPF 1.0, yFiles.NET 3.1 print article email article

Type: Tips & Tricks

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.

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

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

Keywords: OrthogonalEdgeRouter - PortConstraint - port constraint - setSphereOfAction(byte) - ROUTE_ALL_EDGES

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