This is an extension and not part of the main GoJS library. Note that the API for this class may change at any time. If you intend to use an extension in production, you should copy the code to your own source directory. Extensions can be found in the GoJS kit under the `extensions` or `extensionsTS` folders. See the Extensions intro page for more information.

### Hierarchy

- Layout
  - PackedLayout

Custom layout which attempts to pack nodes as close together as possible without overlap. Each node is assumed to be either rectangular or circular (dictated by the hasCircularNodes property). This layout supports packing nodes into either a rectangle or an ellipse, with the shape determined by the packShape property and the aspect ratio determined by either the aspectRatio property or the specified width and height (depending on the packMode).

Nodes with 0 width or height cannot be packed, so they are treated by this layout as having a width or height of 0.1 instead.

## Index

### Properties

- actualBounds
- actualSpacing
- arrangesToOrigin
- aspectRatio
- comparer
- enclosingCircle
- hasCircularNodes
- packMode
- packShape
- size
- sortMode
- sortOrder
- spacing

### Properties

- Spiral

### Methods

- commitLayout
- doLayout

### Constants

- Area
- Ascending
- AspectOnly
- Descending
- Elliptical
- ExpandToFit
- Fit
- MaxSide
- None
- Rectangular

## Properties

### Read-only actualBounds : Rect

- This read-only property returns the actual rectangular bounds occupied by the packed nodes. This property does not take into account any kind of spacing around the packed nodes.

  Note that this property will only return a valid value after a layout has been performed. Before then, its behavior is undefined.

### Read-only actualSpacing : number

- This read-only property is the effective spacing calculated after PackedLayout.doLayout.

  If the packMode is PackedLayout.AspectOnly, this will simply be the spacing property. However, in the PackedLayout.Fit and PackedLayout.ExpandToFit modes, this property will include the forced spacing added by the modes themselves.

  Note that this property will only return a valid value after a layout has been performed. Before then, its behavior is undefined.

### arrangesToOrigin : boolean

- Gets or sets whether or not to use the Layout.arrangementOrigin property when placing nodes.

  The default value is true.

### aspectRatio : number

- Gets or sets the aspect ratio for the shape that nodes will be packed into. The provided aspect ratio should be a nonzero postive number.

  Note that this only applies if the packMode is PackedLayout.AspectOnly. Otherwise, the size will determine the aspect ratio of the packed shape.

  The default value is 1.

### comparer : function(a: Node, b: Node): number | undefined

- Gets or sets the comparison function used for sorting nodes.

  By default, the comparison function is set according to the values of sortMode and sortOrder.

  Whether this comparison function is used is determined by the value of sortMode. Any value except PackedLayout.None will result in the comparison function being used.

        $(PackedLayout,
          {
            sortMode: PackedLayout.Area,
            comparer: function(na, nb) {
              var na = na.data;
              var nb = nb.data;
              if (da.someProperty < db.someProperty) return -1;
              if (da.someProperty > db.someProperty) return 1;
              return 0;
            }
          }
        )

### Read-only enclosingCircle : Rect

- This read-only property returns the smallest enclosing circle around the packed nodes. It makes use of the hasCircularNodes property to determine whether or not to make enclosing circle calculations for rectangles or for circles. This property does not take into account any kind of spacing around the packed nodes. The enclosing circle calculation is performed the first time this property is retrieved, and then cached to prevent slow accesses in the future.

  Note that this property will only return a valid value after a layout has been performed. Before then, its behavior is undefined.

  This property is included as it may be useful for some data visualizations.

### hasCircularNodes : boolean

- Gets or sets whether or not to assume that nodes are circular. This changes the packing algorithm to one that is much more efficient for circular nodes.

  As this algorithm expects circles, it is assumed that if this property is set to true that the given nodes will all have the same height and width. All calculations are done using the width of the given nodes, so unexpected results may occur if the height differs from the width.

  The default value is false.

### packMode : number

- Gets or sets the mode that the layout will use to determine its size. Valid values are PackedLayout.AspectOnly, PackedLayout.Fit, and PackedLayout.ExpandToFit.

  The default value is PackedLayout.AspectOnly. In this mode, the layout will simply grow as needed, attempting to keep the aspect ratio defined by aspectRatio.

### packShape : number

- Gets or sets the shape that nodes will be packed into. Valid values are PackedLayout.Elliptical, PackedLayout.Rectangular, and PackedLayout.Spiral.

  In PackedLayout.Spiral mode, nodes are not packed into a particular shape, but rather packed consecutively one after another in a spiral fashion. The aspectRatio property is ignored in this mode, and the size property (if provided) is expected to be square. If it is not square, the largest dimension given will be used. This mode currently only works with circular nodes, so setting it cause the assume that layout to assume that hasCircularNodes is true.

  Note that this property sets only the shape, not the aspect ratio. The aspect ratio of this shape is determined by either aspectRatio or size, depending on the packMode.

  When the packMode is PackedLayout.Fit or PackedLayout.ExpandToFit and this property is set to true, the layout will attempt to make the diameter of the enclosing circle of the layout approximately equal to the greater dimension of the given size property.

  The default value is PackedLayout.Elliptical.

### size : Size

- Gets or sets the size for the shape that nodes will be packed into. To fill the viewport, set a size with a width and height of NaN. Size values of 0 are considered for layout purposes to instead be 1.

  If the width and height are set to NaN (to fill the viewport), but this layout has no diagram associated with it, the default value of size will be used instead.

  Note that this only applies if the packMode is PackedLayout.Fit or PackedLayout.ExpandToFit.

  The default value is 500x500.

### sortMode : number

- Gets or sets the method by which nodes will be sorted before being packed. To change the order, see sortOrder.

  The default value is PackedLayout.None, in which nodes will not be sorted at all.

### sortOrder : number

- Gets or sets the order that nodes will be sorted in before being packed. To change the sort method, see sortMode.

  The default value is PackedLayout.Descending

### spacing : number

- Gets or sets the spacing between nodes. This value can be set to any real number (a negative spacing will compress nodes together, and a positive spacing will leave space between them).

  Note that the spacing value is only respected in the PackedLayout.Fit packMode if it does not cause the layout to grow outside of the specified bounds. In the PackedLayout.ExpandToFit packMode, this property does not do anything.

  The default value is 0.

## Properties

### Static Spiral : 2

Causes nodes to be packed into a spiral shape; this value is used for packShape.

The aspectRatio property is ignored in this mode, the size is expected to be square, and hasCircularNodes will be assumed 'true'. Please see packShape for more details.

## Methods

### Virtual Override commitLayout

- commitLayout(): void

- This method is called at the end of doLayout, but before the layout transaction is committed. It can be overriden and used to customize layout behavior. By default, the method does nothing.

  #### Returns void

### Override doLayout

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

- Performs the PackedLayout.

  #### Parameters

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

    A Diagram or a Group or a collection of Parts.

  #### Returns void

## Constants

### Static Area : 22

Nodes will be sorted by their area; this value is used for sortMode.

### Static Ascending : 31

Nodes will be sorted in ascending order; this value is used for sortOrder.

Does nothing if sortMode is set to PackedLayout.None.

### Static AspectOnly : 10

Nodes will be packed using the aspectRatio property, with no size considerations; this value is used for packMode.

The spacing property will be respected in this mode.

### Static Descending : 30

Nodes will be sorted in descending order; this value is used for sortOrder.

Does nothing if sortMode is set to PackedLayout.None.

### Static Elliptical : 0

This value for packShape causes nodes to be packed into an ellipse.

The aspect ratio of this ellipse is determined by either aspectRatio or size.

### Static ExpandToFit : 12

Nodes will be either compressed or spaced evenly to fit the given size; this value is used for packMode.

The spacing property will not be respected in this mode, and will not do anything if set.

### Static Fit : 11

Nodes will be compressed if necessary (using negative spacing) to fit the given size. However, if the size is bigger than the packed shape (with 0 spacing), it will not expand to fit it. This value is used for packMode.

The spacing property will be respected in this mode, but only if it does not cause the layout to grow larger than the size.

### Static MaxSide : 21

Nodes will be sorted by their maximum side length before packing; this value is used for sortMode.

### Static None : 20

Nodes will not be sorted before packing; this value is used for sortMode.

### Static Rectangular : 1

Causes nodes to be packed into a rectangle; this value is used for packShape.

The aspect ratio of this rectangle is determined by either aspectRatio or size.

Copyright © 1998-2020 by Northwoods Software Corporation.