Specifies layer constraints for nodes for a HierarchicalLayout.
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 placeInOrder constraints between one node and another if the one precedes the other. These constraints 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 mapperFunction:
// 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:
// 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:
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<TNode>:
// 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.placeInOrder(node1, node2, 2)
// Also place another node in the top layer
layoutData.layerConstraints.placeAtTop(nodeAtTop)
Type Parameters
- TNode
Type Details
- yFiles module
- algorithms
Sample Graphs
Properties
Gets or sets the mapping from nodes to an IComparable (for example a number or string) defining their layer constraints.
Remarks
null
.Examples
layoutData.layerConstraints.nodeComparables = (node: INode): any =>
node.labels.size > 0 ? node.labels.get(0).text : null
See Also
Sample Graphs
Methods
Clears all previously defined constraints.
Remarks
All constraints that were previously defined via placeInOrder, placeInSameLayer, placeAtTop or placeAtBottom were cleared so that new constraints can be declared.
This doesn't affect indirect constraints specified by the nodeComparables.
Examples
layoutData.layerConstraints.clearConstraints()
Adds a constraint that places the node
on the bottom layer.
Parameters
A map of options to pass to the method.
- node - TNode
- the node that should lie on the bottom layer
- priority - number
- the priority of the constraint. If multiple constraints conflict, the ones with lower priority will be discarded.
Examples
layoutData.layerConstraints.placeAtBottom(node)
Sample Graphs
Adds a constraint that places the node
on the topmost layer.
Parameters
A map of options to pass to the method.
- node - TNode
- the node that should lie on the top layer
- priority - number
- the priority of the constraint. If multiple constraints conflict, the ones with lower priority will be discarded.
Examples
layoutData.layerConstraints.placeAtTop(node)
Sample Graphs
placeInOrder
(upperNode: TNode, lowerNode: TNode, minimumDistance?: number, weight?: number, priority?: number)Adds a constraint that forces the upperNode
to lie at least minimumDistance
layers above the lowerNode
with a given weight
penalty for larger layer differences.
Remarks
Parameters
A map of options to pass to the method.
- upperNode - TNode
- the upper node
- lowerNode - TNode
- the lower node that should lie below the upper node
- minimumDistance - number
- the minimum layer distance between the node and its reference node
- weight - number
- the weight penalty for larger layer differences
- priority - number
- the priority of the constraint. If multiple constraints conflict, the ones with lower priority will be discarded.
Examples
layoutData.layerConstraints.placeInOrder(referenceNode, node)
Sample Graphs
Adds a constraint that forces the sameLayerNode
to lie in the same layer as the referenceNode
.
Parameters
A map of options to pass to the method.
- referenceNode - TNode
- the reference node
- sameLayerNode - TNode
- the node that should lie in the same layer
- priority - number
- the priority of the constraint. If multiple constraints conflict, the ones with lower priority will be discarded.
Examples
layoutData.layerConstraints.placeInSameLayer(referenceNode, node)