Specifies sequence constraints for TNode
and TEdge
for a HierarchicalLayout.
Remarks
Sequence constraints affect the sequence of nodes within a layer.
If itemComparables are specified, they are used to sort all nodes and edges accordingly and create placeNodeBeforeNode, placeNodeBeforeEdge, placeEdgeBeforeNode and placeEdgeBeforeEdge constraints that are handles just like manually added constraints.
Examples
One way to set sequence constraints is by mapping nodes or edges to an IComparable (e.g. a number) which define the order in which those nodes are placed within the same layer. This can be done with the itemComparables property. One way to map each item to such an IComparable is to use the mapperFunction:
// Map each item to an IComparable, in this case taken from a custom object stored in the tag
layoutData.sequenceConstraints.itemComparables = (item) => {
const data = item.tag
if (data !== null) {
return data.sequenceOrder
}
// Don't constrain sequencing 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 sequence constraints for a few nodes via a mapper. Nodes that are not
// explicitly mapped don't have constraints on their placement within the layer.
const mapper = layoutData.sequenceConstraints.itemComparables
.mapper as IMapper<INode, any>
// Place node1 before node2
mapper.set(node1, 1)
mapper.set(node2, 3)
// Place node3 between both of them
mapper.set(node3, 2)
// Place node4 after the others 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.sequenceConstraints.itemComparables = sequenceMapper
// Or create the mapping from a Map, instead:
layoutData.sequenceConstraints.itemComparables = sequenceMap
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 SequenceConstraintData<TNode,TEdge,TItem>:
// Ensure that node1 is placed before node2
layoutData.sequenceConstraints.placeNodeBeforeNode(node1, node2)
// Also place another node at the very start
layoutData.sequenceConstraints.placeNodeAtHead(firstNodeInLayer)
Type Parameters
- TNode: TItem
- TEdge: TItem
- TItem
Type Details
- yFiles module
- algorithms
- Sequence constraints that are specified for a group child node will be applied to the parent node instead.
Sample Graphs
Properties
Gets or sets the mapping from nodes or edges to an IComparable (for example a number or string) defining their sequence sort order.
Remarks
null
, no constraints are added.Examples
layoutData.layerConstraints.nodeComparables = (node: INode): any =>
node.labels.size > 0 ? node.labels.get(0).text : null
Sample Graphs
Methods
Clears all previously defined constraints.
Remarks
All constraints that were previously defined via the various methods to place nodes and edges before or after other nodes and edges or to place nodes and edges either at the start or at the end of a sequence were cleared so that new constraints can be declared.
This doesn't affect indirect constraints specified by the itemComparables.
Examples
layoutData.sequenceConstraints.clearConstraints()
Adds a constraint that places a TEdge
at the start of the sequence.
Parameters
A map of options to pass to the method.
- edge - TEdge
- the edge that should be placed at the start
Examples
layoutData.sequenceConstraints.placeEdgeAtHead(edge)
Sample Graphs
Adds a constraint that places a TEdge
at the end of the sequence.
Parameters
A map of options to pass to the method.
- edge - TEdge
- the edge that should be placed at the end
Examples
layoutData.sequenceConstraints.placeEdgeAtTail(edge)
Sample Graphs
Adds a constraint that forces the afterEdge
of type TEdge
to lie after the beforeEdge
of type TEdge
.
Parameters
A map of options to pass to the method.
- beforeEdge - TEdge
- the first edge
- afterEdge - TEdge
- the edge that should be placed after the first edge
Examples
layoutData.sequenceConstraints.placeEdgeBeforeEdge(
beforeEdge,
afterEdge,
)
Sample Graphs
after
edge should be placed after that of the before
edge, if possible). Constraints of edges with recursiveEdgePolicy are ignored.Adds a constraint that forces the afterNode
of type TNode
to lie after the beforeEdge
of type TEdge
.
Parameters
A map of options to pass to the method.
- beforeEdge - TEdge
- the edge
- afterNode - TNode
- the node that should be placed after the edge
Examples
layoutData.sequenceConstraints.placeEdgeBeforeNode(
beforeEdge,
afterNode,
)
Sample Graphs
after
edge should be placed after that of the before
edge, if possible). Constraints of edges with recursiveEdgePolicy are ignored.Adds a constraint that places a TNode
at the start of the sequence.
Parameters
A map of options to pass to the method.
- node - TNode
- the node that should be placed at the start
Examples
layoutData.sequenceConstraints.placeNodeAtHead(node)
Sample Graphs
Adds a constraint that places a TNode
at the end of the sequence.
Parameters
A map of options to pass to the method.
- node - TNode
- the node that should be placed at the end
Examples
layoutData.sequenceConstraints.placeNodeAtTail(node)
Sample Graphs
Adds a constraint that forces the afterEdge
of type TEdge
to lie after the beforeNode
of type TNode
.
Parameters
A map of options to pass to the method.
- beforeNode - TNode
- the node
- afterEdge - TEdge
- the edge that should be placed after the node
Examples
layoutData.sequenceConstraints.placeNodeBeforeEdge(
beforeNode,
afterEdge,
)
Sample Graphs
after
edge should be placed after that of the before
edge, if possible). Constraints of edges with recursiveEdgePolicy are ignored.Adds a constraint that forces the afterNode
of type TNode
to lie after the beforeNode
of type TNode
.
Parameters
A map of options to pass to the method.
- beforeNode - TNode
- the first node
- afterNode - TNode
- the node that should be placed after the first node
Examples
layoutData.sequenceConstraints.placeNodeBeforeNode(
beforeNode,
afterNode,
)