Using Creators Without a Builder
You can use instances of NodeCreator<TDataItem>, EdgeCreator<TDataItem>, and LabelCreator<TDataItem> outside the context of graph builders. This allows you to use the various bindings and providers to create and style nodes, edges, and labels using data items, even without providing the data in consistent lists. It also allows you to use the creators in cases where the graph builders cannot be configured sufficiently.
// Set up a node Creator
const nodeCreator = new NodeCreator<Employee>()
nodeCreator.defaults.shareStyleInstance = false
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'
)
nodeCreator.createLabelBinding((item) => item.name)
// Set up an EdgeCreator
const edgeCreator = new EdgeCreator<Relationship>({
style: (relationship) =>
relationship.Type == 'Assistant'
? new PolylineEdgeStyle({ stroke: 'lightgray' })
: new PolylineEdgeStyle({ stroke: 'black' })
})You can use these creators to create nodes and edges with styles, positions, and labels according to the provided node and edge data.
// Create the elements
const node1 = nodeCreator.createNode(graph, null, data1)
const node2 = nodeCreator.createNode(graph, null, data2)
const edge = edgeCreator.createEdge(graph, node1, node2, relation)You can use the creators' different update methods to apply corresponding changes to existing nodes and edges. The updated items do not need to have been created with the creators.
// Use the configured node and edge creators
// to update structural information like parent-child relationships
nodeCreator.updateNode(graph, node1, null, data1)
nodeCreator.updateNode(graph, node2, null, data2)
// or to apply styles, layout information, labels, or tags to existing items
nodeCreator.updateStyle(graph, node1, data1)
nodeCreator.updateLayout(graph, node1, data1)
edgeCreator.updateBends(graph, edge, relation)
edgeCreator.updateLabels(graph, edge, relation)
edgeCreator.updateTag(graph, edge, relation)See also the chapter about creating and updating items with a GraphBuilder.