Articles in this section
Category / Section

How to automatically arrange nodes without overlapping in Vue 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 Vue 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 -->
<ejs-button v-on:click="btnClick">Add Node</ejs-button>

 btnClick: function (event) {
    let node = { };
  // add a node at runtime
   diagram.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 -->
<ejs-button v-on:click="btnClick">Add Node</ejs-button>

btnClick: function (event) {
  let node = { id:”node1”,offsetX:100,offsetY:100};
  // add a node at runtime
   diagram.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: number = 100;
let offsetY: number = 100;

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

btnClick: function (event) {
     var diagram = document.getElementById('diagram').ej2_instances[0];
     let node: NodeModel = { height: 100, width: 100 };
     // Check node offset lesser than diagram viewportwidth
     if (offsetX < diagram.scrollSettings.viewPortWidth) {
       node.offsetX = offsetX;
       node.offsetY = offsetY;
       // Add a node at runtime
       diagram.add(node);
       offsetX = offsetX + 150;
     } else {
       offsetX = 100;
       offsetY = offsetY + 150;
       node.offsetX = offsetX;
       node.offsetY = offsetY;
       diagram.add(node);
     }
   },

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

<!-- create a button -->
<ejs-button v-on:click="btnClick">Add Node</ejs-button>

let offsetX: number = 100;
let offsetY: number = 100;
btnClick: function (event) {
  var diagram = document.getElementById('diagram').ej2_instances[0];
  let node: NodeModel = { height: 100, width: 100 };
  // check if node offset is lesser than diagram viewport width
  if (offsetX < diagram.scrollSettings.viewPortWidth) {
    node.offsetX = offsetX;
    node.offsetY = offsetY;
    // add a node at runtime
    diagram.add(node);
    offsetX = offsetX + 150;
  } else {
    offsetX = 100;
    offsetY = offsetY + 150;
    node.offsetX = offsetX;
    node.offsetY = offsetY;
    diagram.add(node);
  }
},

Sample

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

You can refer to our Vue Diagram feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Vue Diagram example to understand how to create 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 other controls.

If you have any queries or require clarifications, please let us know in the comments section 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