- CLayerConstraintData<TNode>
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<TNode> property.
In the latter case, the comparables are used to create placeInOrder<TNode> 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<TNode> constraints, and no constraints are added for nodes mapped to null.
Type Parameters
TNode
Examples
One way to map each node to an IComparable is to use the ItemMapping.mapperFunction<TItem, TValue>:
// 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 ItemMapping.mapper<TItem, TValue> 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 = layerOrderMapIComparable 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)Sample Graphs
See Also
Developer's Guide
Members
Constructors
Properties
Gets or sets the mapping from nodes to an IComparable (for example a number or string) defining their layer constraints.
null.Examples
layoutData.layerConstraints.nodeComparables = (node: INode): any =>
node.labels.at(0)?.text ?? nulllayoutData.layerConstraints.nodeComparables = (node) =>
LayerConstraintData.createComponentComparable(
node.labels.at(0)?.text ?? null,
graph.getParent(node),
)Sample Graphs
See Also
Methods
All constraints that were previously defined via placeInOrder<TNode>, placeInSameLayer<TNode>, placeAtTop<TNode> or placeAtBottom<TNode> were cleared so that new constraints can be declared.
This doesn't affect indirect constraints specified by the nodeComparables<TNode>.
Examples
layoutData.layerConstraints.clearConstraints()Parameters
- 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
See Also
Developer's Guide
Parameters
- 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
See Also
Developer's Guide
Parameters
- 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
See Also
Developer's Guide
Parameters
- 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)Sample Graphs
See Also
Developer's Guide