I am trying to use vis.js to draw out network topologies. The required format for entering nodes and edges is as follows:
var nodes = new vis.DataSet([
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}
]);
var edges = new vis.DataSet([
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
]);
I want to know how I can enter this data directly from a text file. I have two separate .txt files for the nodes and edges. I need to read them and populate the nodes and edges variables in my js script.
The nodes .txt file looks like this
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'}
And the edges .txt file looks like this
{from: 1, to: 3},
{from: 1, to: 2},
{from: 2, to: 4},
{from: 2, to: 5}
Please share the code and thanks in advance.
I would recommend to use a JSON file for this. Not only will you get syntax highlighting and formatting in your IDE, but it can also be imported very easily using Node.js: require('./your-data-file.json') and will be parsed to a JS object out of the box.
{
"nodes": [
{ "id": 1, "label": "Node 1"},
{ "id": 2, "label": "Node 2"},
{ "id": 3, "label": "Node 3"},
{ "id": 4, "label": "Node 4"},
{ "id": 5, "label": "Node 5"}
]
}