### Hierarchy

- Layout
  - LayeredDigraphLayout
    - SwimLaneLayout

This arranges nodes of directed graphs into layers (rows or columns). There are many samples that use LayeredDigraphLayout.

If you want to experiment interactively with most of the properties, try the Layered Digraph Layout sample. See samples that make use of LayeredDigraphLayout in the samples index.

The layerSpacing property controls the distance between layers. The columnSpacing property controls the breadth of each "column" -- this affects the distance between nodes within a layer, although the exact distance also depends on the breadth of each node. The layeringOption property determines whether nodes without links coming in or without links going out are lined up at the edge of the graph, or whether they are positioned close to their connected nodes.

By default the layout will route the links in a manner that is consistent with the direction. So, for example, if the direction is 90 degrees (i.e. downward), the links are expected to go from the top towards the bottom. That means the links should come out from the bottom of the ports and should go into the top of the ports. Basically the layout will set Link.fromSpot to `Spot.Bottom` and Link.toSpot to `Spot.Top`.

If you want to the links to use the spots that are given by the ports or by the links themselves, you will need to set setsPortSpots to false to prevent this layout from setting the spots on the links. For example, if each node only has one port that is the whole node, and if you want the links to be spread out along the sides of the nodes, then you should set setsPortSpots to false and set the node's GraphObject.fromSpot to `Spot.BottomSide` and GraphObject.toSpot to `Spot.TopSide`.

This layout handles links that form cycles better than TreeLayout does. The normal routing behavior for "backwards" links is to route them "around" the source node and "around" the destination node, so that all links come in one side and go out the other side. However if you want "backwards" links to go more directly between nodes, set setsPortSpots to false and the node's GraphObject.fromSpot and GraphObject.toSpot both to `Spot.TopBottomSides`. (Of course if the direction is zero or 180, you'll want to use `Spot.LeftRightSides`.)

If the diagram is structured in a tree-like fashion, it may be better to use TreeLayout, which has more options specific to trees. TreeLayout is much faster than LayeredDigraphLayout, and can handle a limited number of links that would prevent the graph structure from being a true tree (i.e. some nodes having multiple parents).

This layout makes use of a LayoutNetwork of LayeredDigraphVertexes and LayeredDigraphEdges that normally correspond to the Nodes and Links of the Diagram.

The layout algorithm consists of four-major steps: Cycle Removal, Layer Assignment, Crossing Reduction, and Straightening and Packing. The layout cannot guarantee that it provides optimal positioning of nodes or routing of links.

The Layer Assignment step can be slow if layeringOption is LayeredDigraphLayout.LayerOptimalLinkLength, which is the default value.

The CrossingReduction step is usually slow, but it can be avoided if you want to maintain a certain order within each layer by setting LayeredDigraphLayout.aggressiveOption to LayeredDigraphLayout.AggressiveNone.

The Straightening and Packing step is usually slow, but you can set packOption at the expense of less nicely placed nodes. Moreover, if you do not set this property, the layout will automatically remove the LayeredDigraphLayout.PackExpand flag if the graph is large enough, in order to improve performance. It is also worth experimenting whether using alignOption to straighten and pack provides good results for your graphs, as it is faster than packOption.

If performance remains a problem, contact us.

## Index

### Constructors

- constructor

### Properties

- aggressiveOption
- alignOption
- columnSpacing
- cycleRemoveOption
- direction
- initializeOption
- iterations
- layerSpacing
- layeringOption
- maxColumn
- maxIndex
- maxIndexLayer
- maxLayer
- minIndexLayer
- packOption
- setsPortSpots

### Methods

- assignLayers
- commitLayers
- commitLayout
- commitLinks
- commitNodes
- createNetwork
- doLayout

### Constants

- AggressiveLess
- AggressiveMore
- AggressiveNone
- AlignAll
- AlignLowerLeft
- AlignLowerRight
- AlignNone
- AlignUpperLeft
- AlignUpperRight
- CycleDepthFirst
- CycleFromLayers
- CycleGreedy
- InitDepthFirstIn
- InitDepthFirstOut
- InitNaive
- LayerLongestPathSink
- LayerLongestPathSource
- LayerOptimalLinkLength
- PackAll
- PackExpand
- PackMedian
- PackNone
- PackStraighten

## Inherited Members

### Properties

- Layout.arrangementOrigin
- Layout.boundsComputation
- Layout.diagram
- Layout.group
- Layout.isInitial
- Layout.isOngoing
- Layout.isRealtime
- Layout.isRouting
- Layout.isValidLayout
- Layout.isViewportSized
- Layout.network

### Methods

- Layout.collectParts
- Layout.copy
- Layout.getLayoutBounds
- Layout.initialOrigin
- Layout.invalidateLayout
- Layout.makeNetwork
- Layout.updateParts

## Constructors

### constructor

- new LayeredDigraphLayout(init?: Partial<LayeredDigraphLayout>): LayeredDigraphLayout

- Constructs a LayeredDigraphLayout with no Layout.network and with no owning Layout.diagram.

  #### Parameters

  - ##### Optional init: Partial<LayeredDigraphLayout>

    Optional initialization properties.

  #### Returns LayeredDigraphLayout

## Properties

### aggressiveOption: EnumValue

- Gets or sets which aggressive option is being used to look for link crossings. The default value is LayeredDigraphLayout.AggressiveLess. The fastest option is LayeredDigraphLayout.AggressiveNone.

### alignOption: number

- Gets or sets the options used by the straighten and pack function, as a potentially faster alternative to packOption.

  When using this option, nodes are assigned coordinates within their layers to produce straighter paths of nodes and small edge lengths.

  When used as an alternative to packOption, this tends to be faster, particularly for larger graphs. Larger graphs, however, will usually be less compact than when using packOption. If this option is set, packOption is ignored.

  This option does not use columns, but rather uses columnSpacing to space nodes within a layer.

  The value must be a combination of the following bit flags: LayeredDigraphLayout.AlignUpperLeft, LayeredDigraphLayout.AlignUpperRight, LayeredDigraphLayout.AlignLowerLeft, and LayeredDigraphLayout.AlignLowerRight.

  Using LayeredDigraphLayout.AlignAll will tend to provide the most balanced results and is what we recommend starting with.

  The default value is LayeredDigraphLayout.AlignNone, meaning packOption is used. LayeredDigraphLayout.AlignAll may become the default in a future major version.

  see

    packOption

  since

    2.3

### columnSpacing: number

- Gets or sets the size of each column. This value must be positive and it defaults to 25.

  When using alignOption, this will act as node spacing for nodes within a layer.

### cycleRemoveOption: EnumValue

- Gets or set which cycle removal option is used. The value must be one of the following values: LayeredDigraphLayout.CycleDepthFirst, LayeredDigraphLayout.CycleGreedy, or LayeredDigraphLayout.CycleFromLayers. The default value is LayeredDigraphLayout.CycleDepthFirst.

### direction: number

- Gets or sets the direction the graph grows towards. 0 is towards the right, 90 is downwards, 180 is towards the left, and 270 is upwards. The default value is 0.

### initializeOption: EnumValue

- Gets or sets which indices initialization option is being used. The value must be one of the following values: LayeredDigraphLayout.InitDepthFirstOut, LayeredDigraphLayout.InitDepthFirstIn, or LayeredDigraphLayout.InitNaive. The default value is LayeredDigraphLayout.InitDepthFirstOut.

### iterations: number

- Gets or sets the number of iterations to be done. The value must be non-negative. The default value is 4.

### layerSpacing: number

- Gets or sets the space between each layer. This value must be non-negative and it defaults to 25.

### layeringOption: EnumValue

- Gets or sets which layering option is being used. The value must be one of the following values: LayeredDigraphLayout.LayerLongestPathSink, LayeredDigraphLayout.LayerLongestPathSource, or LayeredDigraphLayout.LayerOptimalLinkLength. The default value is LayeredDigraphLayout.LayerOptimalLinkLength, which is also the slowest option.

### Read-only maxColumn: number

- This read-only property returns the largest column value.

### Read-only maxIndex: number

- This read-only property returns the largest index value.

### Read-only maxIndexLayer: number

- This read-only property returns the larges index layer.

### Read-only maxLayer: number

- This read-only property returns the largest layer value.

### Read-only minIndexLayer: number

- This read-only property returns the smallest index layer.

### packOption: number

- Gets or sets the options used by the straighten and pack function; this option is deprecated -- we recommend using alignOption instead for better alignment and speed.

  The value must be a combination of the following bit flags: LayeredDigraphLayout.PackMedian, LayeredDigraphLayout.PackStraighten, and LayeredDigraphLayout.PackExpand. The default value is LayeredDigraphLayout.PackAll, which is a combination of all three flags.

  Each of the flags has a cost; PackExpand is particularly slow. However if you do not set this property, this layout will automatically turn off the PackExpand option for you if the graph is large enough. You can set this property value to LayeredDigraphLayout.PackNone to avoid most of the work.

  This option is ignored if alignOption is set to a value other than LayeredDigraphLayout.AlignNone. This tends to be slower than alignOption, particularly for larger graphs. Larger graphs, however, will usually be more compact than when using alignOption.

  see

    alignOption

### setsPortSpots: boolean

- Gets or sets whether the FromSpot and ToSpot of each link should be set to values appropriate for the given value of LayeredDigraphLayout.direction. The default value is true.

  If you set this to false, the spot values of the links and port objects will be used. If you do not set the spot values to sensible values matching the direction, the routing results may be poor and they may cross over nodes.

## Methods

### Protected Virtual assignLayers

- assignLayers(): void

- Assigns every vertex in the input network to a layer. The layer is a non-negative integer describing which row of vertexes each vertex belongs in. (Do not confuse this concept of "layer" with Layers that control the Z-ordering of Parts.)

  The layering satisfies the following relationship: if L is a link from node U to node V, then U.layer > V.layer.

  This method can be overridden to customize how nodes are assigned layers. Please read the Introduction page on Extensions for how to override methods and how to call this base method. By default, this does the appropriate assignments given the value of layeringOption.

  since

    1.1

  #### Returns void

### Protected Virtual commitLayers

- commitLayers(layerRects: Rect[], offset: Point): void

- This overridable method is called by commitLayout to support custom arrangement of bands or labels across each layout layer. By default this method does nothing.

  The coordinates used in the resulting Rects may need to be offset by the Layout.arrangementOrigin.

  since

    1.4

  #### Parameters

  - ##### layerRects: Rect[]

    an Array of Rects with the bounds of each of the "layers"

  - ##### offset: Point

    the position of the top-left corner of the banded area relative to the coordinates given by the layerRects

  #### Returns void

### Protected Override commitLayout

- commitLayout(): void

- Set the fromSpot and toSpot on each Link, position each Node according to the vertex position, and then position/route the Links.

  This calls the commitNodes and commitLinks methods, the latter only if isRouting is true. You should not call this method -- it is a "protected virtual" method. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

### Protected Virtual commitLinks

- commitLinks(): void

- Routes the links.

  This is called by commitLayout. This is only called if Layout.isRouting is true. See also commitNodes. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

### Protected Virtual commitNodes

- commitNodes(): void

- Commit the position of all nodes.

  This is called by commitLayout. See also commitLinks. Please read the Introduction page on Extensions for how to override methods and how to call this base method.

  #### Returns void

### Override Virtual createNetwork

- createNetwork(): LayeredDigraphNetwork

- Create a new LayoutNetwork of LayeredDigraphVertexes and LayeredDigraphEdges.

  #### Returns LayeredDigraphNetwork

  a new LayoutNetwork.

### Override doLayout

- doLayout(coll: Diagram | Iterable<Part> | Group): void

- Perform the layered digraph layout.

  If there is no Layout.network, this calls makeNetwork to create a LayoutNetwork from the given collection of Parts. This removes any reflexive edges in the network, since they should be ignored.

  In order to influence a vertex's layer, you can override assignLayers.

  Finally this calls Layout.updateParts to commit the Node positions from the vertex positions. Layout.updateParts calls commitLayout within a transaction.

  #### Parameters

  - ##### coll: Diagram | Iterable<Part> | Group

    A Diagram or a Group or a collection of Parts.

  #### Returns void

## Constants

### Static AggressiveLess: EnumValue

The faster, less aggressive, crossing reduction algorithm; a valid value for LayeredDigraphLayout.aggressiveOption.

### Static AggressiveMore: EnumValue

The slower, more aggressive, crossing reduction algorithm, a valid value for LayeredDigraphLayout.aggressiveOption.

### Static AggressiveNone: EnumValue

The fastest, but poorest, crossing reduction algorithm; a valid value for LayeredDigraphLayout.aggressiveOption.

### Static AlignAll: number

Enable all options for the LayeredDigraphLayout.alignOption property; See also LayeredDigraphLayout.AlignUpperLeft, LayeredDigraphLayout.AlignUpperRight, LayeredDigraphLayout.AlignLowerLeft, and LayeredDigraphLayout.AlignLowerRight.

This option will tend to balance nodes compared to their neighbors.

### Static AlignLowerLeft: number

This option aligns and compacts nodes based on lower neighbors, favoring leftmost neighbors, a valid value for LayeredDigraphLayout.alignOption.

### Static AlignLowerRight: number

This option aligns and compacts nodes based on lower neighbors, favoring rightmost neighbors, a valid value for LayeredDigraphLayout.alignOption.

### Static AlignNone: number

This option means the the layout will pack nodes using the LayeredDigraphLayout.packOption instead of LayeredDigraphLayout.alignOption, a valid value for LayeredDigraphLayout.alignOption.

### Static AlignUpperLeft: number

This option aligns and compacts nodes based on upper neighbors, favoring leftmost neighbors, a valid value for LayeredDigraphLayout.alignOption.

### Static AlignUpperRight: number

This option aligns and compacts nodes based on upper neighbors, favoring rightmost neighbors, a valid value for LayeredDigraphLayout.alignOption.

### Static CycleDepthFirst: EnumValue

Remove cycles using depth first cycle removal; a valid value of LayeredDigraphLayout.cycleRemoveOption.

### Static CycleFromLayers: EnumValue

Remove cycles depending on assignLayers determining entirely which links are backwards links; a valid value of LayeredDigraphLayout.cycleRemoveOption.

### Static CycleGreedy: EnumValue

Remove cycles using greedy cycle removal; a valid value of LayeredDigraphLayout.cycleRemoveOption.

### Static InitDepthFirstIn: EnumValue

Initialize using depth first in initialization; a valid value for LayeredDigraphLayout.initializeOption.

### Static InitDepthFirstOut: EnumValue

Initialize using depth first out initialization; a valid value for LayeredDigraphLayout.initializeOption.

### Static InitNaive: EnumValue

Initialize using naive initialization; a valid value for LayeredDigraphLayout.initializeOption.

### Static LayerLongestPathSink: EnumValue

Assign layers using longest path sink layering, so that nodes are placed in the latest layer possible possibly resulting in longer links to nodes at the last/deepest layers; a valid value for LayeredDigraphLayout.layeringOption.

### Static LayerLongestPathSource: EnumValue

Assign layers using longest path source layering, so that nodes without any predecessors are placed in the first layer possibly resulting in longer links from nodes at the first/shallowest layers; a valid value for LayeredDigraphLayout.layeringOption.

### Static LayerOptimalLinkLength: EnumValue

Assign layers using optimal link length layering, so that links tend to cross the fewest number of layers; A valid value for LayeredDigraphLayout.layeringOption.

### Static PackAll: number

Enable all options for the LayeredDigraphLayout.packOption property; but we recommend using alignOption instead for better alignment and speed; See also LayeredDigraphLayout.PackExpand, LayeredDigraphLayout.PackStraighten, and LayeredDigraphLayout.PackMedian.

### Static PackExpand: number

This option gives more chances for the packing algorithm to improve the network, but is very expensive in time for large networks; but we recommend using alignOption instead for better alignment and speed; a valid value for LayeredDigraphLayout.packOption.

### Static PackMedian: number

This option tries to have the packing algorithm center groups of nodes based on their relationships with nodes in other layers, a valid value for LayeredDigraphLayout.packOption; but we recommend using alignOption instead for better alignment and speed.

### Static PackNone: number

Does minimal work in packing the nodes; a valid value for LayeredDigraphLayout.packOption; but we recommend using alignOption instead for better alignment and speed.

### Static PackStraighten: number

This option tries to have the packing algorithm straighten many of the links that cross layers, a valid value for LayeredDigraphLayout.packOption; but we recommend using alignOption instead for better alignment and speed.

Copyright © 1998-2022 by Northwoods Software Corporation.