I am currently working on a Java Web Application where I need to generate a graph programmatically and send it to Cytoscape.js (version 2.7.10) for display. In order to transport the data from my application to Cytoscape.js, I am using gson (version 2.8.1) to build a JSON Object. The formatted data that is sent to the JavaScript part looks like this:
{"elements":[{"nodes":[{"group":"nodes","data":{"id":"a","name":"Eins"}},{"group":"nodes","data":{"id":"b","name":"Zwei"}},{"group":"nodes","data":{"id":"c","name":"Drei"}},{"group":"nodes","data":{"id":"d","name":"Vier"}}]},{"edges":[{"group":"edges","data":{"id":"e","name":"1-2","source":"a","target":"b"}},{"group":"edges","data":{"id":"f","name":"2-3","source":"b","target":"c"}},{"group":"edges","data":{"id":"g","name":"2-4","source":"b","target":"d"}}]}]}
When setting up the cytoscape "window," the code structure is as follows:
return function(callbackuri, inputdata, divname, title, colorstr) {
console.log("CytoScape Test ver:0.1.9");
console.log("Data String sent to cyto * "+inputdata+" *");
var cy = cytoscape({
container : $('#cy'),
elements: JSON.parse(inputdata),
style: [
{
selector: 'node',
style: {
'label': 'data(name)',
'width': '60px',
'height': '60px',
'color': 'blue',
'background-fit': 'contain',
'background-clip': 'none'
}
}
, {
selector: 'edge',
style: {
'text-background-color': 'yellow',
'text-background-opacity': 0.4,
'width': '6px',
'target-arrow-shape': 'triangle',
'control-point-step-size': '140px'
}
}]
});
//
cy.layout(concentric_options);
cy.fit();
})
I have tried various methods to skip the setup via elements: JSON.parse(inputdata) but none of them seem to work as expected. Most times, I end up with a single large node that can be zoomed and panned around, which is not the desired outcome.
Interestingly, I am able to manually set up a graph by adding data after the initial setup and before the layout using the following function call:
var eles = cy.add([ {
group : "nodes",
data : {
id : "n0",
name : "A",
weight : 75
}
}, {
group : "nodes",
data : {
id : "n1",
name : "B",
weight : 75
}
},
….
If anyone has any ideas or suggestions, please feel free to share!
Cheers
Janko