Populates a graph from custom data where node data items know about their predecessors and/or successors.
Remarks
This class can be used when the data consists of one or more collections of nodes, each of which knows its neighbors, and optionally, groups. The methods createNodesSource, createGroupNodesSource, createSuccessorsSource, createPredecessorsSource, createOutEdgesSource, and createInEdgesSource create new source collections from which nodes and group nodes will be created.
Generally, using the AdjacencyGraphBuilder class consists of a few basic steps:
- Create an AdjacencyGraphBuilder and optionally, provide an IGraph on which it should operate.
const adjacencyBuilder = new AdjacencyGraphBuilder(graph)
- Create one or more nodes sources from which the AdjacencyGraphBuilder creates the initial nodes in the graph. By adding group node sources, group nodes ar created.
const orangeData = getAdjacencyNodesData() // create source for initial nodes (orange nodes) const orangeNodesSource = adjacencyBuilder.createNodesSource( orangeData, // nodes data // nodes data (orangeDataItem) => orangeDataItem.nodeId ) // node id provider
- To create neighbor nodes and edges to or from them, create new successor sources or predecessor sources as neighbor sources of the initial sources.
By adding existing successor sources or predecessor sources, edges can be defined between existing sources.
// create an EdgeCreator for edges adjacent to orange nodes const orangeEdgeCreator = new EdgeCreator() // create a nodes source for the green nodes as successor of orangeNodesSource const greenNodesSource = orangeNodesSource.createSuccessorsSource( (orangeNodeDataItem) => orangeNodeDataItem.successorNodes, orangeEdgeCreator ) // add the same source also as predecessor orangeNodesSource.addPredecessorsSource( (orangeNodeDataItem) => orangeNodeDataItem.predecessorNodes, greenNodesSource, orangeEdgeCreator )
// create an EdgeCreator for edges adjacent to orange nodes const orangeEdgeCreator = new EdgeCreator<OrangeData>() // create a nodes source for the green nodes as successor of orangeNodesSource const greenNodesSource = orangeNodesSource.createSuccessorsSource( (orangeNodeDataItem) => orangeNodeDataItem.successorNodes, orangeEdgeCreator ) // add the same source also as predecessor orangeNodesSource.addPredecessorsSource( (orangeNodeDataItem) => orangeNodeDataItem.predecessorNodes, greenNodesSource, orangeEdgeCreator )
By adding existing successor ids or predecessor ids, edges can be defined between the nodes of existing sources where the neighbors are defined by their id.
const greenEdgeCreator = new EdgeCreator() // register EdgeCreator for edges to orange successor nodes created by orangeNodesSource greenNodesSource.addSuccessorIds( (greenNodeDataItem) => greenNodeDataItem.successorNodeIds, greenEdgeCreator ) // register EdgeCreator for edges to orange predecessor nodes created by orangeNodesSource greenNodesSource.addPredecessorIds( (greenNodeDataItem) => greenNodeDataItem.predecessorNodeIds, greenEdgeCreator )
const greenEdgeCreator = new EdgeCreator<GreenData>() // register EdgeCreator for edges to orange successor nodes created by orangeNodesSource greenNodesSource.addSuccessorIds( (greenNodeDataItem) => greenNodeDataItem.successorNodeIds, greenEdgeCreator ) // register EdgeCreator for edges to orange predecessor nodes created by orangeNodesSource greenNodesSource.addPredecessorIds( (greenNodeDataItem) => greenNodeDataItem.predecessorNodeIds, greenEdgeCreator )
Using the methods mentioned above, edges are always created implicitly between the nodes of an AdjacencyNodesSource<TDataItem> and their added neighbors providing the data items of the nodes to the EdgeCreator<TDataItem>.
When the data items of an AdjacencyNodesSource<TDataItem> do not know their neighbor items directly but know their outgoing and incoming edges which in turn know the data items of the opposite nodes, the corresponding methods createOutEdgesSource, createInEdgesSource, addOutEdgesSource, addInEdgesSource, addOutEdgesSourceToId, and addInEdgesSourceToId can be used that provide the specified edge data items to the EdgeCreator<TDataItem>.
// create and register EdgeCreator for purple edges const purpleEdgeCreator = new EdgeCreator() orangeNodesSource.addOutEdgesSource( (nodeDataItem) => nodeDataItem.purpleOutEdges, (purpleEdgeDataItem) => purpleEdgeDataItem.greenTargetNode, greenNodesSource, // use existing nodesSource for target nodes purpleEdgeCreator ) // create and register EdgeCreator for yellow edges orangeNodesSource.addOutEdgesSourceToId( (orangeNodeDataItem) => orangeNodeDataItem.yellowOutEdges, (yellowEdgeDataItem) => yellowEdgeDataItem.targetNodeId, new EdgeCreator() )
// create and register EdgeCreator for purple edges const purpleEdgeCreator = new EdgeCreator<PurpleEdgeData>() orangeNodesSource.addOutEdgesSource<PurpleEdgeData, GreenData>( (nodeDataItem) => nodeDataItem.purpleOutEdges, (purpleEdgeDataItem) => purpleEdgeDataItem.greenTargetNode, greenNodesSource, // use existing nodesSource for target nodes purpleEdgeCreator ) // create and register EdgeCreator for yellow edges orangeNodesSource.addOutEdgesSourceToId<YellowEdgeData>( (orangeNodeDataItem) => orangeNodeDataItem.yellowOutEdges, (yellowEdgeDataItem) => yellowEdgeDataItem.targetNodeId, new EdgeCreator() )
- Each AdjacencyNodesSource<TDataItem> provides a NodeCreator<TDataItem> that allows for further specification of how the items from this source should be created. You can provide defaults for the default styling but also bind more specific styling based on the actual data item with the styleProvider and styleBindings.
orangeNodesSource.nodeCreator.defaults.shareStyleInstance = false orangeNodesSource.nodeCreator.defaults.style = new ShapeNodeStyle({ stroke: 'darkRed', fill: 'orange' }) // nodes without successor should have a diamond shape orangeNodesSource.nodeCreator.styleBindings.addBinding( 'shape', (redDataItem) => redDataItem.successorNodes.length > 0 ? 'roundRectangle' : 'diamond' )
It also allows for obtaining layout information from the data item (layoutProvider), as well as further specification of the created node's tag (tagProvider).
- The edge creation can be adjusted by passing suitable EdgeCreator<TDataItem> instances when adding a neighbor source. It allows to specify the styling of the created edges (defaults, styleProvider, styleBindings).
greenEdgeCreator.defaults.style = new PolylineEdgeStyle({ stroke: 'forestGreen' })
Furthermore, it allows for obtaining bend locations from the data item (bendsProvider), as well as further specification of the created edge's tag (tagProvider).
greenEdgeCreator.bendsProvider = (edgeDataItem) => edgeDataItem.bendPoints
- Optionally, labels can be added by providing one or multiple label bindings. If there is a varying number of labels per node, createLabelsSource can be used, instead. Similar methods are also available on the EdgeCreator<TDataItem>.
The LabelsSource<TDataItem> provides a LabelCreator<TDataItem> that allows for adjusting all aspects of the label creation, like style, text, size, and tag.
const edgeLabelCreator = purpleEdgeCreator.createLabelBinding( (purpleEdgeData) => purpleEdgeData.connectionType ) edgeLabelCreator.defaults.style = new DefaultLabelStyle({ backgroundFill: 'white', backgroundStroke: 'purple', textSize: 8 })
- Call buildGraph to populate the graph. You can apply a layout algorithm afterward to make the graph look nice.
// Build a graph initially adjacencyBuilder.buildGraph() // Apply a layout in a subsequent step graph.applyLayout(new HierarchicLayout())
- If your items or collections change later, call updateGraph to make the changes visible in the graph. If the data item instances have changed, you can call setData.
// Set the new data collections: adjacencyBuilder.setData(orangeNodesSource, getAdjacencyNodesData()) // Update a graph after the business data has changed adjacencyBuilder.updateGraph() // Apply a layout in a subsequent step graph.applyLayout(new HierarchicLayout())
You can further customize how nodes, groups, and edges are created by adding event handlers to the various events and modifying the items there. This can be used to modify items in ways that are not directly supported by the available bindings or defaults. There are creation and update events for all items, which allow for separate customization when nodes, groups, and edges are created or updated. For completely static graphs, where updateGraph is not needed, the update events can be safely ignored.
Related Reading in the Developer's Guide
AdjacencyGraphBuilder
, in particular, is topic of section AdjacencyGraphBuilder.Type Details
- yfiles module
- view-component
- yfiles-umd modules
- All view modules
- Legacy UMD name
- yfiles.binding.AdjacencyGraphBuilder
See Also
This class serves as a convenient way to create graphs. Some aspects to look out for:
- buildGraph does not clear the graph. If needed, call clear to empty the graph before building it anew.
- The AdjacencyGraphBuilder ignores and preserves items manually created on the graph in between calls to updateGraph.
If you need to extensively modify your data in order to fit the requirements of the AdjacencyGraphBuilder, it is often better to write the code interfacing with the graph by hand instead of relying on AdjacencyGraphBuilder.
update
methods on the creators
(e.g. updateStyle), resolves the provider
(e.g. styleProvider) and applies the bindings
(e.g. applyStyleBindings). The latter can also be applied solely.For example, to update any aspect of node creation:
// configure the NodeCreator to update non-structural aspects
nodesSource.nodeCreator.addNodeUpdatedListener((sender, evt) => {
nodesSource.nodeCreator.updateLayout(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateStyle(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateTag(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateLabels(evt.graph, evt.item, evt.dataItem)
})
builder.updateGraph()
Constructors
Initializes a new instance of the AdjacencyGraphBuilder class that operates on the given graph.
Properties
Gets the graph used by this builder.
Remarks
Methods
addNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, nodesSource: AdjacencyNodesSource<TDataItem>)Binds a collection of data items to the given nodesSource
.
Type Parameters
- TDataItem
- The type of the data items in the source.
Parameters
A map of options to pass to the method.
- data - TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>
- The collection of objects that is bound to the source.
- nodesSource - AdjacencyNodesSource<TDataItem>
- The source to which the data is bound.
Populates the graph with items generated from the bound data.
Remarks
Returns
See Also
createGroupNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, idProvider?: function(TDataItem, Object):Object) : AdjacencyNodesSource<TDataItem>Creates a new AdjacencyNodesSource<TDataItem> and binds a collection of group node data items to it.
Remarks
The NodeCreator<TDataItem> of the returned AdjacencyNodesSource<TDataItem> can be used to further specify creation properties of these items, e.g. style, layout, etc.
Data items of this collection are created as group nodes in the graph.
Type Parameters
- TDataItem
- The type of the data items in the source.
Parameters
A map of options to pass to the method.
- data - TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>
- The collection of objects to iterate and create the group nodes from.
- idProvider - function(TDataItem, Object):Object
- An optional function that yields an ID for each element in the
data
. This ID is used by parentIdProvider, addSuccessorIds, and addPredecessorIds to resolve the parent, source, or target nodes. The ID is also used to identify nodes during updateGraph.Signature Details
function(dataItem: TDataItem, canonicalId: any) : any
A callback that provides an unique identifier for thedataItem
.id provider are used in NodesSource.idProvider, EdgesSource.idProvider and LabelsSource.idProvider to identify the created nodes, edges and labels and avoid duplicate creation of items with the same ID.
The ID can also be used by parentIdProvider and sourceIdProvider and targetIdProvider to resolve the parent, source, or target nodes.
The ID is further used to identify nodes, edges, and labels during updateGraph.
Parameters
- dataItem - TDataItem
- The value that will be passed in.
- canonicalId - any
- The original canonical id of the value. For data arrays and iterables this is the index into the collection. For Maps and data objects this is the key associated with a value.
Returns
- any
Returns
- ↪AdjacencyNodesSource<TDataItem>
- A new AdjacencyNodesSource<TDataItem> instance that can be used to further customize the creation of nodes, e.g. provide specific style defaults.
createNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, idProvider?: function(TDataItem, Object):Object) : AdjacencyNodesSource<TDataItem>Creates a new AdjacencyNodesSource<TDataItem> and binds a collection of node data items to it.
Remarks
Type Parameters
- TDataItem
- The type of the data items in the source.
Parameters
A map of options to pass to the method.
- data - TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>
- The collection of objects to iterate and create the nodes from.
- idProvider - function(TDataItem, Object):Object
- An optional function that yields an ID for each element in the
data
. This ID is used by parentIdProvider, addSuccessorIds, and addPredecessorIds to resolve the parent, source, or target nodes. The ID is also used to identify nodes during updateGraph.Signature Details
function(dataItem: TDataItem, canonicalId: any) : any
A callback that provides an unique identifier for thedataItem
.id provider are used in NodesSource.idProvider, EdgesSource.idProvider and LabelsSource.idProvider to identify the created nodes, edges and labels and avoid duplicate creation of items with the same ID.
The ID can also be used by parentIdProvider and sourceIdProvider and targetIdProvider to resolve the parent, source, or target nodes.
The ID is further used to identify nodes, edges, and labels during updateGraph.
Parameters
- dataItem - TDataItem
- The value that will be passed in.
- canonicalId - any
- The original canonical id of the value. For data arrays and iterables this is the index into the collection. For Maps and data objects this is the key associated with a value.
Returns
- any
Returns
- ↪AdjacencyNodesSource<TDataItem>
- A new AdjacencyNodesSource<TDataItem> instance that can be used to further customize the creation of nodes, e.g. provide specific style defaults.
Returns the data item the given node
was created for.
Remarks
Parameters
A map of options to pass to the method.
- node - INode
- The node that was created for the data item.
Returns
- ↪any
- The data item the given
node
was created for.
Returns the data item the given edge
was created for.
Remarks
The returned data item was processed by one of the added AdjacencyNodesSource<TDataItem>.
For edges created via createSuccessorsSource or createPredecessorsSource the data item is the same as the one used for the node the successors or predecessors were created for.
For edges created via createInEdgesSource or createOutEdgesSource the data item is the one provided by the edge data provider of these methods.
Parameters
A map of options to pass to the method.
- edge - IEdge
- The edge that was created for the data item.
Returns
- ↪any
- The data item the given
edge
was created for.
Returns the INode that was created for a data item with the given id
.
Remarks
id
for the data item was provided by the idProvider of one of the added AdjacencyNodesSource<TDataItem>.Type Parameters
- TId
- The type of the id.
Parameters
A map of options to pass to the method.
- id - TId
- The id the node was created for.
Returns
Returns the INode that was created for the given item
.
Remarks
Type Parameters
- TDataItem
- The type of the data item.
Parameters
A map of options to pass to the method.
- item - TDataItem
- The data item the node was created for.
Returns
Triggers the EdgeCreated event.
Triggers the EdgeRemoved event.
Triggers the EdgeUpdated event.
Triggers the LabelAdded event.
Triggers the LabelRemoved event.
Triggers the LabelUpdated event.
Triggers the NodeCreated event.
Triggers the NodeRemoved event.
Triggers the NodeUpdated event.
setData
<TDataItem>(nodesSource: AdjacencyNodesSource<TDataItem>, data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>)Binds a new data collection to an AdjacencyNodesSource<TDataItem>, replacing the old one.
Remarks
nodesSource
, such that updateGraph considers the new collection.Type Parameters
- TDataItem
- The type of the data items in the source.
Parameters
A map of options to pass to the method.
- nodesSource - AdjacencyNodesSource<TDataItem>
- The source whose data source should be re-assigned.
- data - TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>
- The collection of objects that is specified for the given source.
Updates the graph after changes in the bound data.
Remarks
update
methods on the creators
(e.g. updateStyle), resolves the provider
(e.g. styleProvider) and applies the bindings
(e.g. applyStyleBindings). The latter can also be applied solely.For example, to update any aspect of node creation:
// configure the NodeCreator to update non-structural aspects
nodesSource.nodeCreator.addNodeUpdatedListener((sender, evt) => {
nodesSource.nodeCreator.updateLayout(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateStyle(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateTag(evt.graph, evt.item, evt.dataItem)
nodesSource.nodeCreator.updateLabels(evt.graph, evt.item, evt.dataItem)
})
builder.updateGraph()
Events
Occurs when an edge has been created by any one of AdjacencyNodesSource<TDataItem>'s add or create methods.
Remarks
This event can be used to further customize the created edge.
New edges are created either in response to calling buildGraph, or in response to calling updateGraph when there are new items in the edges sources.
See Also
Event Registration
addEdgeCreatedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Event Deregistration
removeEdgeCreatedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<IEdge,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<IEdge,Object>
- An object that contains the event data.
Occurs when an edge has been removed.
Remarks
This event can be used to further handle the removal of the removed edge.
Edges are removed in response to calling updateGraph.
See Also
Event Registration
addEdgeRemovedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Event Deregistration
removeEdgeRemovedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<IEdge,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<IEdge,Object>
- An object that contains the event data.
Occurs when an edge has been updated.
Remarks
This event can be used to further customize the updated edge.
Edges are updated in response to calling updateGraph.
See Also
Event Registration
addEdgeUpdatedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Event Deregistration
removeEdgeUpdatedListener(function(this, GraphBuilderItemEventArgs<IEdge,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<IEdge,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<IEdge,Object>
- An object that contains the event data.
Occurs when a label has been added to a node or edge.
Remarks
This event can be used to further customize the created label.
New labels are added in response to calling buildGraph, or in response to calling updateGraph.
See Also
Event Registration
addLabelAddedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Event Deregistration
removeLabelAddedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<ILabel,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<ILabel,Object>
- An object that contains the event data.
Occurs when a node or edge label has been removed.
Remarks
This event can be used to further handle the removal of the removed label.
Labels are removed in response to calling updateGraph.
See Also
Event Registration
addLabelRemovedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Event Deregistration
removeLabelRemovedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<ILabel,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<ILabel,Object>
- An object that contains the event data.
Occurs when a node or edge label has been updated.
Remarks
This event can be used to further customize the updated label.
Labels are updated in response to calling updateGraph.
See Also
Event Registration
addLabelUpdatedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Event Deregistration
removeLabelUpdatedListener(function(this, GraphBuilderItemEventArgs<ILabel,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<ILabel,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<ILabel,Object>
- An object that contains the event data.
Occurs when a node has been created by any one of the nodes sources.
Remarks
This event can be used to further customize the created node.
New nodes are created either in response to calling buildGraph, or in response to calling updateGraph when there are new items in the nodes sources.
See Also
Event Registration
addNodeCreatedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Event Deregistration
removeNodeCreatedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<INode,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<INode,Object>
- An object that contains the event data.
Occurs when a node has been removed from any one of the nodes sources.
Remarks
This event can be used to further handle the removal of the removed node.
Nodes are removed in response to calling updateGraph.
See Also
Event Registration
addNodeRemovedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Event Deregistration
removeNodeRemovedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<INode,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<INode,Object>
- An object that contains the event data.
Occurs when a node has been updated.
Remarks
This event can be used to further customize the updated node.
Nodes are updated in response to calling updateGraph.
See Also
Event Registration
addNodeUpdatedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Event Deregistration
removeNodeUpdatedListener(function(this, GraphBuilderItemEventArgs<INode,Object>):void)
Signature Details
function(sender: this, evt: GraphBuilderItemEventArgs<INode,Object>)
Parameters
- sender - this
- The source of the event.
- evt - GraphBuilderItemEventArgs<INode,Object>
- An object that contains the event data.