Context
I want to use cytoscape.js
for visualizing graphs. While I am capable with a myriad of languages (C++
, Mathematica
, R
, etc), I am new to Javascript
, JSON
, HTML
, and CSS
. Thus it would be favorable to learn these languages through this use case (implementing graphs with cytoscape.js
). Please keep this in mind in your answer.
I have previously asked how to [remotely load cytoscape.js
and how to get graphs display (requires a div
). Since then I have created a script that turns a graph as represented in one of the other languages I use, into the JSON
format indicated here. While I can just copy-paste all of this directly into my program, for large networks that is clearly a poor way to implement it. An example of my script's output is at the bottom of this.
Question
Given a JSON
object/file(?) how can I do the following:
- load it into
cytoscape.js
without copy-pasting the code. - referencing it once loaded. (e.g. basic explanation of how JSON syntax for use in
cytoscape.js
)
Script Output
cytoscape({
container: document.getElementById('cy'),
elements: [
{// node Node 1
group: 'nodes',
data: {
id: 'Node 1'
},
selected: false,
selectable: true,
locked: false,
grabbable: true,
selectable: true,
},
{// node Node 2
group: 'nodes',
data: {
id: 'Node 2'
},
selected: false,
selectable: true,
locked: false,
grabbable: true,
selectable: true,
},
{// node Node 3
group: 'nodes',
data: {
id: 'Node 3'
},
selected: false,
selectable: true,
locked: false,
grabbable: true,
selectable: true,
},
{// edge 1_2
group: 'edges',
data: {
id: '1_2',
source: '1',
target: '2'
}
},
{// edge 2_3
group: 'edges',
data: {
id: '2_3',
source: '2',
target: '3'
}
},
{// edge 3_1
group: 'edges',
data: {
id: '3_1',
source: '3',
target: '1'
}
}
],
style: [
{
selector: 'node',
style: {
'content': 'data(id)'
}
}
]
});
http-server
) to avoid CORS issue when running on local host. – Giacinta