documentationfor yFiles for HTML 2.6

LayerConstraintData

Specifies layer constraints for nodes for a HierarchicLayout.

Inheritance Hierarchy
LayoutData
LayerConstraintData

Remarks

Layer constraints affect the assignment of nodes to layers. A layer constraint can be specified either explicitly for a given node or by mapping some or all nodes to an IComparable, e.g., a number, with the nodeComparables property.

In the latter case, the comparables are used to create placeBelow constraints between one node and another if the one precedes the other. These constrains work just like explicitly created constraints. Note that equal values do not result in placeInSameLayer constraints, and no constraints are added for nodes mapped to null..

Examples

One way to map each node to an IComparable is to use the delegate:

Using a delegate to map nodes to objects that can be ordered to define relative layer placements
// Map each node to an IComparable, in this case taken from a custom object stored in the Tag
layoutData.layerConstraints.nodeComparables = (node) => {
  const data = node.tag
  if (data !== null) {
    return data.layerOrder
  }
  // Don't constrain layer placement if the tag doesn't contain our custom object.
  return null
}

If only some nodes should be constrained, the mapper can be used instead:

Using a mapper to map certain nodes to objects that can be ordered to define relative layer placements
// Define layer constraints for a few nodes via a mapper. Nodes that are not
// explicitly mapped don't have constraints on their layer placement.
const mapper = layoutData.layerConstraints.nodeComparables.mapper
// Place both node1 and node2 in the same layer by assigning the same value
mapper.set(node1, 5)
mapper.set(node2, 5)
// Place node3 somewhere above node1 and node2 by assigning a smaller value
mapper.set(node3, 2)
// Place node4 somewhere below node1 and node2 by assigning a larger value
mapper.set(node4, 8)
// The exact values don't matter much they just define an ordering. However, all
// values must have the same type you cannot mix int and string or double, for example.// Define layer constraints for a few nodes via a mapper. Nodes that are not
// explicitly mapped don't have constraints on their layer placement.
const mapper = layoutData.layerConstraints.nodeComparables
  .mapper as IMapper<INode, any>
// Place both node1 and node2 in the same layer by assigning the same value
mapper.set(node1, 5)
mapper.set(node2, 5)
// Place node3 somewhere above node1 and node2 by assigning a smaller value
mapper.set(node3, 2)
// Place node4 somewhere below node1 and node2 by assigning a larger value
mapper.set(node4, 8)
// The exact values don't matter much they just define an ordering. However, all
// values must have the same type you cannot mix int and string or double, for example.

If those mappings have been prepared beforehand, e.g. in a HashMap<TKey,TValue> or IMapper<K,V>, that property on the ItemMapping<TItem,TValue> can also be set:

Setting a prepared mapper or Map for relative layer assignment
layoutData.layerConstraints.nodeComparables = layerOrderMapper
// Or create the mapping from a Map, instead:
layoutData.layerConstraints.nodeComparables = layerOrderMap

IComparable is only the most natural option if such an object can be readily produced from a node, or there already exists such a mapping from nodes to something that can be compared. In many cases it can be easier to construct constraints manually with the respective methods on LayerConstraintData:

Defining custom layer constraints
// Place all nodes in sameLayerNodes into the same layer
const firstSameLayerNode = sameLayerNodes.first()
for (const node of sameLayerNodes) {
  layoutData.layerConstraints.placeInSameLayer(firstSameLayerNode, node)
}
// Ensure that node2 is placed two layers below node1
layoutData.layerConstraints.placeBelow(node1, node2, 2)
// Also place another node in the top layer
layoutData.layerConstraints.placeAtTop(nodeAtTop)

Type Details

yfiles module
view-layout-bridge
yfiles-umd modules
view-layout-bridge
Legacy UMD name
yfiles.hierarchic.LayerConstraintData

See Also

If there is a fixed layer assignment, for example because the layers are defined by business data, the givenLayersLayererIds should be used instead.

Sample Graphs

Constructors

Properties

Methods