C

TreeBuilder

Populates a graph from custom data where objects corresponding to nodes have a parent-child relationship.
Inheritance Hierarchy

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:

  1. Create a TreeBuilder and optionally, provide an IGraph on which it should operate.
    const treeBuilder = new TreeBuilder(graph)
  2. 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, 'id')
  3. 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.

  4. 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).

  5. 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
  6. 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 LabelStyle({
      backgroundFill: 'rgb(225,242,253)',
      backgroundStroke: 'lightSkyBlue',
      textSize: 8,
    })
  7. 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())
  8. 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.

See Also

The different graph builders are discussed in the section Creating a Graph from Business Data. Class TreeBuilder, in particular, is topic of section TreeBuilder.

Developer's Guide

API

GraphBuilder

Members

Show:

Constructors

Initializes a new instance of the TreeBuilder class that operates on the given graph.

Parameters

graph?: IGraph
An IGraph instance on which this builder operates. If omitted a new Graph will be created.

Properties

Gets the graph used by this builder.
The style defaults of this graph are considered in the NodeCreator<TDataItem>, EdgeCreator<TDataItem> and LabelCreator<TDataItem> of the sources that are created on this TreeBuilder.
readonlyfinal

Methods

Binds a collection of root data items to the given nodesSource.
final

Parameters

data: any
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.
This graph is not cleared before the new items are added. To clear the graph, explicitly call clear beforehand.

Return Value

IGraph
The graph of this builder populated by the defined sources.

See Also

API
updateGraph
Registers a collection of root group data items functioning as entities for the NodeCreator<TDataItem> of the returned TreeNodesSource<TDataItem>.
If an optional ID provider is used, the identifiers created by said function have to be unique for the elements in the given data set. Otherwise, the behavior of the buildGraph and the updateGraph methods is undefined.
final

Parameters

data: any
The collection of objects to iterate and create the group root nodes from.
idProvider: function(TDataItem, any): any
An optional function that yields an id for each element in the data.

Return Value

TreeNodesSource<TDataItem>
A new TreeNodesSource<TDataItem> instance that can be used to further customize the creation, e.g. provide specific style defaults.

See Also

Developer's Guide
Registers a collection of root data items functioning as entities for the NodeCreator<TDataItem> of the returned TreeNodesSource<TDataItem>.
If an optional ID provider is used, the identifiers created by said function have to be unique for the elements in the given data set. Otherwise, the behavior of the buildGraph and the updateGraph methods is undefined.
final

Parameters

data: any
The collection of objects to iterate and create the root nodes from.
idProvider: function(TDataItem, any): any
An optional function that yields an id for each element in the data.

Return Value

TreeNodesSource<TDataItem>
A new TreeNodesSource<TDataItem> instance that can be used to further customize the creation, e.g. provide specific style defaults.

See Also

Developer's Guide
Returns the data item the given node was created for.
The returned data item was processed by one of the added TreeNodesSource<TDataItem>.

Parameters

node: INode
The node that was created for the data item.

Return Value

any
The data item the given node was created for.
Returns the data item the given edge was created for.
The returned data item was processed by one of the added TreeNodesSource<TDataItem>.

Parameters

edge: IEdge
The edge that was created for the data item.

Return Value

any
The data item the given edge was created for.
Returns the INode that was created for a data item with the given id.
The id for the data item was provided by the idProvider of one of the added TreeNodesSource<TDataItem>.

Parameters

id: TId
The id the node was created for.

Return Value

INode
The INode that was created for a data item with the given id.
Returns the INode that was created for the given item.
The data item was processed by one of the added TreeNodesSource<TDataItem>.

Parameters

item: TDataItem
The data item the node was created for.

Return Value

INode
The INode that was created for the given item.
Triggers the edge-created event.
protected

Parameters

edge: IEdge
The edge that has been created.
dataItem: any
The data item from which the edge has been created.
Triggers the edge-removed event.
protected

Parameters

edge: IEdge
The edge that has been removed.
dataItem: any
The corresponding data item of the removed edge.
Triggers the edge-updated event.
protected

Parameters

edge: IEdge
The edge that has been updated.
dataItem: any
The data item with which the edge has been updated.
Triggers the label-added event.
protected

Parameters

label: ILabel
The label that has been added.
dataItem: any
The data item from which the label has been created.
Triggers the label-removed event.
protected

Parameters

label: ILabel
The label that has been removed.
dataItem: any
The corresponding data item of the removed label.
Triggers the label-updated event.
protected

Parameters

label: ILabel
The label that has been updated.
dataItem: any
The data item with which the label has been updated.
Triggers the node-created event.
protected

Parameters

node: INode
The node that has been created.
dataItem: any
The data item from which the node has been created.
Triggers the node-removed event.
protected

Parameters

node: INode
The node that has been removed.
dataItem: any
The corresponding data item of the removed node.
Triggers the node-updated event.
protected

Parameters

node: INode
The node that has been updated.
dataItem: any
The data item with which the node has been updated.
Binds a new data collection to a TreeNodesSource<TDataItem>, replacing the old one.
This can be used to assign an entirely different collection instance to an existing nodesSource, such that updateGraph considers the new collection.
final

Parameters

nodesSource: TreeNodesSource<TDataItem>
The source whose data source should be reassigned.
data: any
The collection of objects that is specified for the given source.
Updates the graph after changes in the bound data.
Graph elements corresponding to objects that are still present in the source collections are kept, new graph elements are created for new objects in the collections, and obsolete ones are removed.
updateGraph merely updates the graph structure. Any other bound data must be updated explicitly if necessary. Calling the respective 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.addEventListener('node-updated', (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.

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.

Properties of

GraphBuilderItemEventArgs<IEdge, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
edge-updated, edge-removed
Occurs when an edge has been removed.

This event can be used to further customize the created node.

Edges are removed in response to calling updateGraph.

Properties of

GraphBuilderItemEventArgs<IEdge, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
edge-updated, node-removed
Occurs when an edge has been updated.

This event can be used to further customize the created node.

Edges are updated in response to calling updateGraph.

Properties of

GraphBuilderItemEventArgs<IEdge, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
edge-created, node-removed
Occurs when a label has been added to a node or edge.

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.

Properties of

GraphBuilderItemEventArgs<ILabel, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
label-updated, label-removed
Occurs when a node or edge label has been removed.

This event can be used to further customize the created label.

Labels are removed in response to calling updateGraph.

Properties of

GraphBuilderItemEventArgs<ILabel, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
label-added, label-updated
Occurs when a node or edge label has been updated.

This event can be used to further customize the created label.

Labels are updated in response to calling updateGraph.

Properties of

GraphBuilderItemEventArgs<ILabel, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
label-added, label-removed
Occurs when a node has been created.

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.

Properties of

GraphBuilderItemEventArgs<INode, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
node-updated, node-removed
Occurs when a node has been removed.

This event can be used to further customize the created node.

Nodes are removed in response to calling updateGraph.

Properties of

GraphBuilderItemEventArgs<INode, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
node-created, node-updated
Occurs when a node has been updated.
This event can be used to further customize the created node.

Properties of

GraphBuilderItemEventArgs<INode, any>
dataItem: TDataItem
Gets the object the item has been created from.
graph: IGraph
Gets the graph that can be used to modify the item.
item: TItem
Gets the item that is the subject of the event.

See Also

API
node-created, node-removed