Vis Js Network Save Position – Create Network Graph JavaScript

We will see use of vis js network save position in this blog.
I needed to create a Network graph with lots of nodes and edges and it was becoming very hard, as due to lots of edges, it was becoming chaotic.
I was using vis.js, my favorite for network graphs. But it was not generating the graph I needed. I tried a lots of layouts, nodes, edges, and physics options. I spent a whole day tweaking settings, but due to large number of nodes, I was not getting a satisfactory graph.
Now, vis.js lets us drag and change the positions of nodes. But, I was unaware of the fact that once we have dragged all the nodes and get required positions, we could save that positions and load whenever needed.
I was looking over stackoverflow for maintaining big graphs using vis.js and I came across thisstackoverflow answer bytotymedli.
That helped me to save network position with vis js.
So, we can use storePositions() to load the X and Y coordinates to your dataset. We can save them, then just expand the nodes with the saved coordinates when we initialize the network later.
Suppose we have a vis.js graph.
Easiest way for JavaScript Network Graph Visualization – Vis.js
So, we can drag the nodes and make the layout of graph as we wanted it to be.
Then we can use storePositions.
network.storePositions()
This is available at network.data.nodes.
const nodePositions = data.nodes.map(({ id, x, y }) => ({ id, x, y }))
Its saved in nodePositions variable. So, next time we load the graph, we can update graph layout using this variable.
copy(nodePositions)
This would copy the value in this variable, which is just the array of coordinates of positions. So, we can save it somewhere.
And, next we can load it and use it to update the graph.
nodePositions.forEach(nodePosition => data.nodes.update(nodePosition))
Update: Vis Js Network Save Positions
So, later I also had to use dynamic filter in vis.js. And using DataView for dynamic filter, the storePositions was no longer working. storePositions was giving error.
Uncaught TypeError: Cannot read property ‘x’ of undefined
In adiscussion, I found out storePositions would not work with dataview.
So, the alternative here is to use getPosition and later put them in the dataset.
However the data returned by it (getPosition) is incompatible for data.update(). So, changing a bit of structure.
let theNodePosition = []let theEdgePosition = []function savePosition() { let gpPositions = network.getPositions() let edgePositions = network.body.edges; // Either way for (const [key, value] of Object.entries(gpPositions)) { let tempObj = { id: parseInt(key), x: value.x, y: value.y } theNodePosition.push(tempObj) } for(let edgeId in edgePositions){ theEdgePosition.push({ from:edgePositions[edgeId].from['id'], to: edgePositions[edgeId].to['id'] }); }}
Now for loading and updating the graph, same as above.
theNodePositions.forEach(nodePosition => nodes.update(nodePosition))theEdgePositions.forEach(nodePosition => nodes.update(nodePosition))
Similar Posts

Easiest way for JavaScript Network Graph Visualization – Vis.js
In this blog, we will learn how to create the JavaScript Network graph visualization in one of the easiest way possible, using vis.js Network.

Socket IO Node Js Example – Car Driving Simulation
We will build a simulation of a 2D car driving using Socket.IO. We will have a 2D car from top view which can be controlled by a remote and we will be using Node.js, Express, Socket.IO, HTML, CSS and JavaScript.

How to Connect and Deploy Private GitHub Repositories to cPanel Shared Hosting using GitHub Actions
In this blog we are going to see the step by step process to connect and deploy the private GitHub repository to cPanel share hosting using GitHub actions.

HTML to PDF JavaScript – Example with Code
In this blog we will convert HTML file to PDF using JavaScript. Examples code snippets provided.
![[SOLVED] we couldn’t sign you in. please try again. – Microsoft Teams Login Error](/image.pl?url=https%3a%2f%2fawan.com.np%2fwp-content%2fuploads%2f2021%2f04%2fwe-couldnt-sign-you-in.-please-try-again.-Microsoft-Teams-Login-Error-Solved-min-768x560-1.png&f=jpg&w=240)
[SOLVED] we couldn’t sign you in. please try again. – Microsoft Teams Login Error
Microsoft Teams Login Error – SOLVED. Solution to “we couldn’t sign you in. please try again.” – Microsoft Teams login error.

Real-Time Data Processing using AWS
Real-time data is something that is being updated on a near-to-real-time basis. We will be using different AWS services to create a data pipeline that will be used to handle and integrate this real-time data and finally load it into a Redshift Data Warehouse.