Articles in this section
Category / Section

How to automatically arrange nodes without overlapping in React Diagram?

3 mins read

How to add multiple nodes dynamically without overlapping between them

You should not push the node to the nodes collection at runtime. (Example: nodes.push(node)). Instead, you can use a add method to add a node at runtime in React Diagram.

Add a single node dynamically

You can add a node at runtime by using the add method. Refer to the following code example.

<!-- create a button -->
<input type="button" id="Add Node" value="Add Node">

document.getElementById('Add Node').onclick = function () {
    let node = { };
// add a node at runtime
   diagramInstance.add(node);
};

The above code example will add a node to the diagram at position 0,0, which means offsetX and offsetY of a node is 0.

Position the node

You need to set node offsetX and offsetY to position the node in the diagram. Refer to the following code example in which the nodes are positioned at 100,100.

<!-- create a button -->
<input type="button" id="Add Node" value="Add Node">

document.getElementById('Add Node').onclick = function () {
let node = { id:”node1”,offsetX:100,offsetY:100};
// add a node at runtime
   diagramInstance.add(node);
};

Nodes overlaps with each other by clicking the button several times

When you click a button several times, the nodes will be added to the position 100,100 at each button click. So, multiple nodes will be in the same position.

How to avoid overlapping of multiple nodes

To avoid overlapping of nodes, change the node position based on the viewPortWidth value.Here is the code example to initialize the offsetX and offsetY value as 100,100.

//Define offset values initially

let offsetX = 100;
let offsetY = 100;

Here is a code example to change the node offset value based on the diagram viewPortWidth.

document.getElementById('Add Node').onclick = function () {
   let node = { height: 100, width: 100 };
   //check node offset lesser than diagram viewportwidth
   if (offsetX < diagramInstance.scrollSettings.viewPortWidth) {
     node.offsetX = offsetX;
     node.offsetY = offsetY;
     //add a node at runtime
     diagramInstance.add(node);
     offsetX = offsetX + 150;
   } else {
     offsetX = 100;
     offsetY = offsetY + 150;
     node.offsetX = offsetX;
     node.offsetY = offsetY;
     diagramInstance.add(node);
   }
 };

Here is a complete code example to add multiple nodes dynamically without overlapping with each other.

<!-- create a button -->
<input type="button" id="Add Node" value="Add Node">

let offsetX = 100;
let offsetY = 100;

document.getElementById('Add Node').onclick = function () {
   let node = { height: 100, width: 100 };
   //check node offset lesser than diagram viewportwidth
   if (offsetX < diagramInstance.scrollSettings.viewPortWidth) {
     node.offsetX = offsetX;
     node.offsetY = offsetY;
     //add a node at runtime
     diagramInstance.add(node);
     offsetX = offsetX + 150;
   } else {
     offsetX = 100;
     offsetY = offsetY + 150;
     node.offsetX = offsetX;
     node.offsetY = offsetY;
     diagramInstance.add(node);
   }
 };

Sample

Conclusion
I hope you enjoyed learning about how to automatically arrange nodes without overlapping in React Diagram.

You can refer to our React Diagram feature tour page to know about its other groundbreaking feature representations and documentations. You can also explore our React Diagram example to understand how to present and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our React Spreadsheet and other components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied