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.

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

The current implementation of OrthogonalEdgeRouter requires that RouteAllEdges of the SphereOfAction enumeration is set with property SphereOfAction when there are multiple edges with a strong port constraint at the same coordinate.

Categories this article belongs to:
yFiles WPF > yFiles WPF Algorithms > Automatic Graph Layout > Orthogonal Edge Routing
yFiles for Silverlight > yFiles for Silverlight Algorithms > Automatic Graph Layout > Orthogonal Edge Routing
yFiles.NET > yFiles.NET Algorithms > Automatic Graph Layout > Orthogonal Edge Routing
Applies to:
yFiles.NET: 4.0, 4.1, 4.2, 4.3
yFiles WPF: 2.0, 2.1, 2.2, 2.3, 2.4
yFiles for Silverlight: 1.0, 1.0.1, 2.0, 2.1, 2.2, 2.3
Keywords:
OrthogonalEdgeRouter - PortConstraint - port constraint - SphereOfAction - RouteAllEdges