Populates a graph from custom data where objects corresponding to nodes have a parent-child relationship.
Remarks
This class can be used when the data consists of one or more collections of nodes, each of which knows its child nodes, and optionally, groups. The methods createRootNodesSource, createRootGroupNodesSource, and createChildNodesSource create new source collections from which nodes and group nodes will be created.
Generally, using the TreeBuilder class consists of a few basic steps:
- Create a TreeBuilder and optionally, provide an IGraph on which it should operate.
const treeBuilder = new TreeBuilder(graph)
- Create one or more root node sources from which the TreeBuilder creates the root nodes in the tree. Group nodes can be created by adding group node sources.
const redData = getRootNodesData() // create source for root nodes (red nodes) const redNodesSource = treeBuilder.createRootNodesSource(redData)
- To create child nodes, create new sources as child sources of the root sources. By adding existing sources as child sources, the recursive structure of the tree can be defined:
// create source for children of red nodes (blue nodes) as child source of redNodeSource const blueNodesSource = redNodesSource.createChildNodesSource( (n) => n.childrenBlue ) // tie the knot blueNodesSource.addChildNodesSource((n) => n.childrenRed, redNodesSource)
Edges are created by the implicit parent-child relationship between the sources. For each child node, the TreeBuilder creates an edge between the child node and its (tree) parent providing the child node data item to the parentEdgeCreator.
- Each TreeNodesSource<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.
redNodesSource.nodeCreator.defaults.shareStyleInstance = false redNodesSource.nodeCreator.defaults.style = new ShapeNodeStyle({ stroke: 'darkred', fill: 'orange' }) // leaf nodes should have a diamond shape redNodesSource.nodeCreator.styleBindings.addBinding('shape', (redDataItem) => redDataItem.childrenBlue.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 accessing the parentEdgeCreator that allows for specifying the styling of the created edges (defaults, styleProvider, and styleBindings).
blueNodesSource.parentEdgeCreator.defaults.style = new PolylineEdgeStyle({ stroke: 'cornflowerBlue' })
Furthermore, it allows for obtaining bend locations from the data item (bendsProvider), as well as further specification of the created edge's tag (tagProvider).
blueNodesSource.parentEdgeCreator.bendsProvider = (nodeDataItem) => nodeDataItem.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 = blueNodesSource.parentEdgeCreator.createLabelBinding( (blueDataItem) => blueDataItem.name ) edgeLabelCreator.defaults.style = new DefaultLabelStyle({ backgroundFill: 'rgb(225,242,253)', backgroundStroke: 'lightSkyBlue', 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 treeBuilder.buildGraph() // Apply a layout in a subsequent step graph.applyLayout(new TreeLayout())
- 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: treeBuilder.setData(redNodesSource, getRootNodesData()) // Update a graph after the business data has changed treeBuilder.updateGraph() // Apply a layout in a subsequent step graph.applyLayout(new TreeLayout())
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
TreeBuilder
, in particular, is topic of section TreeBuilder.Type Details
- yfiles module
- view-component
- yfiles-umd modules
- All view modules
- Legacy UMD name
- yfiles.binding.TreeBuilder
See Also
This class serves as a convenient way to create trees. 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 TreeBuilder 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 TreeBuilder, it is often better to write the code interfacing with the graph by hand instead of relying on TreeBuilder.
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 TreeBuilder class that operates on the given graph.
Properties
Gets the graph used by this builder.
Remarks
Methods
addRootNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, nodesSource: TreeNodesSource<TDataItem>)Binds a collection of root 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 - TreeNodesSource<TDataItem>
- The source to which the data is bound.
Populates the graph with items generated from the bound data.
Remarks
Returns
See Also
createRootGroupNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, idProvider?: function(TDataItem, Object):Object) : TreeNodesSource<TDataItem>Registers a collection of root group data items functioning as entities for the NodeCreator<TDataItem> of the returned TreeNodesSource<TDataItem>.
Type Parameters
- TDataItem
- The type of the root 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 root nodes from.
- idProvider - function(TDataItem, Object):Object
- An optional function that yields an id for each element in the
data
.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
- ↪TreeNodesSource<TDataItem>
- A new TreeNodesSource<TDataItem> instance that can be used to further customize the creation, e.g. provide specific style defaults.
See Also
createRootNodesSource
<TDataItem>(data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>, idProvider?: function(TDataItem, Object):Object) : TreeNodesSource<TDataItem>Registers a collection of root data items functioning as entities for the NodeCreator<TDataItem> of the returned TreeNodesSource<TDataItem>.
Type Parameters
- TDataItem
- The type of the root 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 root nodes from.
- idProvider - function(TDataItem, Object):Object
- An optional function that yields an id for each element in the
data
.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
- ↪TreeNodesSource<TDataItem>
- A new TreeNodesSource<TDataItem> instance that can be used to further customize the creation, e.g. provide specific style defaults.
See Also
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
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 TreeNodesSource<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: TreeNodesSource<TDataItem>, data: TDataItem[] | Iterable<TDataItem> | Map<any,TDataItem> | { [id: string]: TDataItem; } | (() => GeneratorLike<TDataItem>)Binds a new data collection to a TreeNodesSource<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 - TreeNodesSource<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.
Remarks
This event can be used to further customize the created node.
New edges are created either in response to calling buildGraph, or in response to calling updateGraph.
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 customize the created node.
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 created node.
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 customize the created 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 created 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.
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.
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.
Remarks
This event can be used to further customize the created 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
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.