GraphBuilder: Setting the Node Sizes

Tips & Tricks

Summary

GraphBuilder uses the node size from its nodeDefaults. However, sometimes you want to create the nodes with a different size that's stored in the node data (or somewhere else).

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

Description

Iterate over the nodes after you called buildGraph() and adjust the sizes.

This is a obvious solution but the nodes are created with the wrong size. Therefore, you should the iteration directly after the buildGraph() call, so that the nodes are never rendered with the wrong size.

const graphBuilder = new yfiles.binding.GraphBuilder(graph)
// ...GraphBuilder configuration...

graphBuilder.buildGraph()

graph.nodes.forEach(node => {
  // Replace this with the actual property containing the node size
  const size = node.tag.size
  graph.setNodeLayout(
    node,
    new yfiles.geometry.Rect(
      node.layout.topLeft,
      new yfiles.geometry.Size(size.width, size.height)
    )
  )
})

Listen to the GraphBuilder.NodeCreated event

This solution has the advantages that the size is updated directly after the node creation and that it's more obvious that the size change belongs to the graph builder.

const graphBuilder = new yfiles.binding.GraphBuilder(graph)
// ...other GraphBuilder configuration...

graphBuilder.addNodeCreatedListener((sender, args) => {
  const node = args.item
  const size = node.tag.size
  graph.setNodeLayout(
    node,
    new yfiles.geometry.Rect(
      node.layout.topLeft,
      new yfiles.geometry.Size(size.width, size.height)
    )
  )
})

graphBuilder.buildGraph()

Categories this article belongs to:
yFiles for HTML > Other
Applies to:
yFiles for HTML: 2.0, 2.1, 2.2
Keywords:
GraphBuilder - TreeBuilder - AdjacentNodesGraphBuilder - binding