C

NodeCreator<TDataItem>

This class can be used to create and update INodes based on data items.
Inheritance Hierarchy

Remarks

These data items can be any business or arbitrary data which should be represented by nodes.

NodeCreator<TDataItem> allows for conveniently binding properties of the data item to the properties of the created INode to control aspects like layout and styling.

This class can be used both standalone as well as in conjunction with any of the GraphBuilders, e.g. GraphBuilder.

In the context of the GraphBuilder, it is responsible to create the items from the associated NodesSource<TDataItem>s.

Type Parameters

TDataItem

The type of the data items in the source.

Examples

A NodeCreator<TDataItem> can either be used with a NodesSource<TDataItem> in conjunction with a GraphBuilder

const builder = new GraphBuilder()
const nodesSource = builder.createNodesSource(
  nodeData,
  (nodeData) => nodeData.nodeId,
)
nodesSource.nodeCreator.defaults.shareStyleInstance = false
nodesSource.nodeCreator.defaults.style = new ShapeNodeStyle({
  stroke: 'darkorange',
  fill: 'lightyellow',
  shape: 'round-rectangle',
})
nodesSource.nodeCreator.styleBindings.addBinding(
  'stroke',
  (employee) =>
    employee.position.includes('Chief') ? 'darkred' : 'darkorange',
)
nodesSource.nodeCreator.styleBindings.addBinding(
  'shape',
  (employee) => (employee.freelancer ? 'hexagon' : 'round-rectangle'),
)

builder.buildGraph()

or it can be used standalone. In that case, its createNode<TDataItem> method can be used to create nodes.

const nodeCreator = new NodeCreator<Employee>()
nodeCreator.defaults.shareStyleInstance = true
nodeCreator.defaults.style = new ShapeNodeStyle({
  stroke: 'darkorange',
  fill: 'lightyellow',
  shape: 'round-rectangle',
})
nodeCreator.styleBindings.addBinding('stroke', (employee) =>
  employee.position.includes('Chief') ? 'darkred' : 'darkorange',
)
nodeCreator.styleBindings.addBinding('shape', (employee) =>
  employee.freelancer ? 'hexagon' : 'round-rectangle',
)

const node = nodeCreator.createNode(graph, null, dataItem)

Members

Show:

Constructors

Creates a new creator for nodes or group nodes.

Parameters

Properties

Gets or sets a set of INodeDefaults that will be used for the nodes and node labels.
The defaults of instances of this class created by one of the GraphBuilders, e.g. GraphBuilder, cascade with the defaults of the GraphBuilder's GraphBuilder.graph.
final
Gets or sets a predicate that determines whether the node created for a data item shall be a group node or not.
When this predicate returns true but the node will not have any children associated, it will still be created as group node.
final
Function Details
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.
Parameters
obj: TDataItem
The object to compare against the criteria defined within the method represented by this delegate.
Return Value
boolean
true if obj meets the criteria defined within the method represented by this delegate; otherwise, false.
Gets or sets an optional set of bindings that will be applied to the node's INode.layout.
Gets or sets an optional provider of a Rect that will be used as the node's INode.layout for the source data item.
The layoutBindings<TDataItem> may be used to bind each of the IPoint.x, IPoint.y, ISize.width and ISize.height values, separately. If no layout is provided, the defaults<TDataItem>' INodeDefaults.size will be used and the node will be located at the origin.
conversionfinal
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
Rect
Gets or sets an optional set of bindings that will be applied to the node's INode.style.
Note that bindings will affect the properties of the instance created by getStyle<TDataItem>. If the style is obtained from the defaults<TDataItem> with INodeDefaults.shareStyleInstance set to true, the properties of the same instance are overwritten each time. To get fresh copies, set INodeDefaults.shareStyleInstance to false.
conversionfinal
Gets or sets an optional provider of an INodeStyle instance that will be used as the node's INode.style for the source data item.
By default the defaults<TDataItem>' INodeDefaults.style will be used. Note that INodeDefaults.shareStyleInstance will be ignored if this provider returns a value. Use the styleBindings<TDataItem> to reconfigure properties of the style instances.
conversionfinal
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
INodeStyle
Gets or sets an optional provider of an object that will be used as the node's ITagOwner.tag for the source data item.
By default the source data item itself will be used as the tag.
conversionfinal
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
any

Methods

Adds an event handler for a given event type.
final

Parameters

type: string
A string which represents the type of the event to register as listed in events.
listener: function(EventArgs, this): void
The function which will be invoked when the event is raised.
options?: ListenerOptions
Options which specify how the listener will be invoked.
Adds the given labelsSource to this instance.
final

Type Parameters

TLabelDataItem

The type of the data items in the source.

Parameters

dataProvider: function(TDataItem): any
conversion
A function that provides the label data items from the data item.
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
any
labelsSource: LabelsSource<TLabelDataItem>
The source to which the data is added.
Resolves the layoutBindings<TDataItem> against the given dataItem and applies them on the given node layout.
The layoutProvider<TDataItem> is not evaluated by this method.
final

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used.

See Also

API
updateLayout
Resolves the styleBindings<TDataItem> against the given dataItem and applies them to the node style.
The styleProvider<TDataItem> is not evaluated by this method.
final

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used.

See Also

API
updateStyle
Creates a binding for labels that will be added to nodes created by this instance.
Use this method over createLabelsSource<TDataItem> if the created nodes have a predetermined amount of labels. This method returns a LabelCreator<TDataItem> whose LabelCreator.textProvider<TDataItem> is configured with the given text. Note that the item type of the returned LabelCreator<TDataItem> is the same as for this NodeCreator<TDataItem>.
final

Parameters

text?: function(TDataItem): string
conversion
An optional provider for the ILabel.text property. If the provider returns null or undefined no label will be created. To force creation of an empty label the provider must return an empty string instead.
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
string

Return Value

LabelCreator<TDataItem>
A new LabelCreator<TData> instance that can be further configured.

Examples

Without further configuration createLabelBinding<TDataItem> creates labels with the text provided by the given text and the graph's default label style.

nodesSource.nodeCreator.createLabelBinding(
  (employee) => employee.name,
)

The returned LabelCreator<TDataItem> can be used to configure the label appearance further. Note that the data item it uses is the data item for the owning node:

const labelCreator = nodesSource.nodeCreator.createLabelBinding(
  (employee) => employee.name,
)
labelCreator.styleProvider = (employee) =>
  employee.department === 'IT' ? itLabelStyle : logisticsLabelStyle

It is also possible to ignore the provided text and create an icon instead, using the IconLabelStyle:

const labelCreator = nodesSource.nodeCreator.createLabelBinding()
// to avoid empty labels to be created return null for missing image URL
labelCreator.textProvider = (employee) =>
  typeof employee.imageUrl !== 'undefined' ? ' ' : null
labelCreator.styleProvider = (employee) =>
  new IconLabelStyle({
    href: employee.imageUrl,
    iconSize: new Size(16, 16),
    iconPlacement: ExteriorNodeLabelModel.LEFT,
  })
Creates a new source of label data items that will be added to nodes created by this instance.
Use this method over createLabelBinding<TDataItem> if the created nodes have a varying number of labels. Note that the data introduces a new item type for the returned LabelsSource<TDataItem>. Also note that the data is expected to return an IEnumerable<T> of that type.
final

Type Parameters

TLabelDataItem

The type of the data items in the source.

Parameters

data: function(TDataItem): any
conversion
A function that provides a collection of label data items from the node data item.
Function Details
A callback that provides an instance of type TValue for the given dataItem.
Parameters
dataItem: TDataItem
The data item for which a value shall be provided.
Return Value
any
id?: function(TLabelDataItem, any): any
conversion
An optional function that yields an id for each label data item that is provided from the label data provider. This id is used to identify the labels during updateLabels<TDataItem>.
Function Details
A callback that provides an unique identifier for the dataItem.
Parameters
dataItem: TLabelDataItem
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.
Return Value
any

Return Value

LabelsSource<TLabelDataItem>
A new LabelsSource<TDataItem> instance whose LabelCreator<TDataItem> can be configured further.

Examples

A simple usage of createLabelsSource<TDataItem> note that a LabelCreator.textProvider<TDataItem> has to be configured on the returned LabelsSource<TDataItem>'s LabelsSource.labelCreator<TDataItem>.

const labelsSource = nodesSource.nodeCreator.createLabelsSource(
  (employee) => employee.labels,
)
labelsSource.labelCreator.textProvider = (labelObject) =>
  labelObject.text

The label objects might encode more information which can be used for individual styling of the labels:

const labelsSource = nodesSource.nodeCreator.createLabelsSource(
  (employee) => employee.labels,
)
labelsSource.labelCreator.styleProvider = (labelObject) =>
  labelObject.style === 'Orange'
    ? orangeLabelStyle
    : defaultLabelStyle
labelsSource.labelCreator.textProvider = (labelObject) =>
  labelObject.text
Creates a node in the graph with the values of the bindings resolved against the dataItem.
This method will use the various helper methods in this class to determine the values for the node's INode.layout, INode.style, and ITagOwner.tag. It will also add the labels to the node.

Parameters

graph: IGraph
The graph to create the node in.
parent: INode | null
The parent node for the newly created node.
dataItem: TDataItem
The data item for which to create the node.

Return Value

INode
The newly created node.

Examples

In the context of a GraphBuilder, this method is called during GraphBuilder.buildGraph and GraphBuilder.updateGraph. Without a GraphBuilder, this method can be called directly to create a styled and labeled node:
const nodeCreator = new NodeCreator<Employee>()
nodeCreator.defaults.shareStyleInstance = true
nodeCreator.defaults.style = new ShapeNodeStyle({
  stroke: 'darkorange',
  fill: 'lightyellow',
  shape: 'round-rectangle',
})
nodeCreator.styleBindings.addBinding('stroke', (employee) =>
  employee.position.includes('Chief') ? 'darkred' : 'darkorange',
)
nodeCreator.styleBindings.addBinding('shape', (employee) =>
  employee.freelancer ? 'hexagon' : 'round-rectangle',
)

const node = nodeCreator.createNode(graph, null, dataItem)
Called from createNode<TDataItem> and performs the actual node creation in the graph.
protected

Parameters

graph: IGraph
The graph to create the node in.
groupNode: boolean
Whether to create a group node.
parent: INode | null
Optional parent of the node.
layout: Rect
The layout of the node.
style: INodeStyle
The style of the node.
tag: INode['tag'] | null
The tag of the node.

Return Value

INode
The newly created node.
Obtains a Rect to use as INode.layout or null by resolving the layoutProvider<TDataItem> and afterwards applying the layoutBindings<TDataItem>.
In case of null, the defaults<TDataItem> are used.
protected

Parameters

dataItem: TDataItem
The data item on which the provider is resolved.

Return Value

Rect | null
The value to use or null in case the defaults should be used.
Obtains an INodeStyle instance or null by resolving the styleProvider<TDataItem>.
In case of null, the defaults<TDataItem> are used.
protected

Parameters

dataItem: TDataItem
The data item on which the provider is resolved.

Return Value

INodeStyle
The value to use or null in case the defaults should be used.
Obtains an object to use as ITagOwner.tag by resolving the tagProvider<TDataItem> on the data item.
The tag can be any value and does not need to be related to the original source data item or its ID.
protected

Parameters

dataItem: TDataItem
The data item on which the provider is resolved.

Return Value

any | null
The value to use or null in case the defaults should be used.

See Also

API
tagProvider
Resolves the layoutProvider<TDataItem> and then applies the layout bindings.
This method is called by updateLayout<TDataItem>.
protected

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used.

Return Value

Rect
An updated Rect based on the given data item.
Resolves the styleProvider<TDataItem> and then applies the style bindings.
This method is called by updateStyle<TDataItem>.
protected

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used.

Return Value

INodeStyle
The updated style instance. Either the value returned by getStyle<TDataItem>, or the default style, if getStyle<TDataItem> returns null.
Resolves the tagProvider<TDataItem> on the given data item.

By default, it only delegates to getTag<TDataItem>.

This method is called by updateTag<TDataItem>.

protected

Parameters

graph: IGraph
The managed graph.
node: INode
The node for which the new tag should be obtained.
dataItem: TDataItem
The data item that is used.

Return Value

any | null
Returns the updated tag object.
Triggers the node-created<TDataItem> event.
protected

Parameters

graph: IGraph
The graph in which the node has been created.
node: INode
The node that has been created.
dataItem: TDataItem
The data item from which the node has been created.
Triggers the node-updated<TDataItem> event.
protected

Parameters

graph: IGraph
The graph in which the node has been updated.
node: INode
The node that has been updated.
dataItem: TDataItem
The data item with which the node has been updated.
Adds an event handler for a given event type.
final

Parameters

type: string
A string which represents the type of the event to register as listed in events.
listener: function(EventArgs, this): void
The function which will be invoked when the event is raised.
options?: ListenerOptions
Options which specify how the listener will be invoked.
Can be used to update the labels of the node that have been added with this NodeCreator<TDataItem>.
This only adds / removes labels based on the bound data. To update bound properties for the labels, call the respective update methods on the LabelCreator<TDataItem>:
// configure the LabelCreators to update non-structural aspects
const labelCreator1 = nodeCreator.createLabelBinding((item) => item.name)
labelCreator1.addEventListener('label-updated', (evt) => {
  labelCreator1.updateText(evt.graph, evt.item, evt.dataItem)
  labelCreator1.updateStyle(evt.graph, evt.item, evt.dataItem)
  labelCreator1.updatePreferredSize(evt.graph, evt.item, evt.dataItem)
  labelCreator1.updateLayoutParameter(evt.graph, evt.item, evt.dataItem)
  labelCreator1.updateTag(evt.graph, evt.item, evt.dataItem)
})
// each LabelCreator has to be configured
const labelCreator2 = nodeCreator.createLabelBinding(
  (item) => item.description,
)
labelCreator2.addEventListener('label-updated', (evt) => {
  // only update properties which may change
  labelCreator2.updateText(evt.graph, evt.item, evt.dataItem)
})

Parameters

graph: IGraph
The managed graph.
node: INode
The node whose labels to update.
dataItem: TDataItem
The node data item with which the node's labels should be updated.
Updates the layout of the node in the graph with the given layout information of the given dataItem by calling getUpdatedLayout<TDataItem> and applying it to the node.
final

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used for the update.
Updates the node.

By default, this method only performs a structural update of the node, i.e. it sets a new parent and updates its group node state.

To update other aspects of the node instance, call the respective update methods when the node is updated or override this method.

// 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()

Note that existing labels whose properties (e.g. text) may change have to be configured similarly.

Parameters

graph: IGraph
The graph that contains the node.
node: INode
The node to update.
parent: INode | null
The new parent of the node.
dataItem: TDataItem
The data item with which the node should be updated.
Updates the style of the node with the given dataItem by calling getUpdatedStyle<TDataItem> and applying the style to the node in the graph.
final

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used for the update.
Updates the ITagOwner.tag of the node with the given dataItem by calling getUpdatedTag<TDataItem> and setting the new tag on the node.
final

Parameters

graph: IGraph
The managed graph.
node: INode
The node to update.
dataItem: TDataItem
The data item that is used for the update.

Events

Occurs when a node has been created.

Properties of GraphBuilderItemEventArgs<INode, TDataItem>

dataItem: TDataItem
final
Gets the object the ItemEventArgs.item<T> has been created from.
graph: IGraph
final
Gets the graph that can be used to modify the ItemEventArgs.item<T>.
item: TItem
final
Gets the item that is the subject of the event.

See Also

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

Properties of GraphBuilderItemEventArgs<INode, TDataItem>

dataItem: TDataItem
final
Gets the object the ItemEventArgs.item<T> has been created from.
graph: IGraph
final
Gets the graph that can be used to modify the ItemEventArgs.item<T>.
item: TItem
final
Gets the item that is the subject of the event.