Skip to content

Instantly share code, notes, and snippets.

@DGaffney
Created February 6, 2013 20:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DGaffney/1c2f1c3c6a9501320e94 to your computer and use it in GitHub Desktop.
Save DGaffney/1c2f1c3c6a9501320e94 to your computer and use it in GitHub Desktop.
sigma.publicPrototype.parseJSON = function(jsonPath,callback) {
var sigmaInstance = this;
jQuery.getJSON(jsonPath, function(data) {
for (i=0; i<data.nodes.length; i++){
var nodeNode = data.nodes[i];
var id = nodeNode.id;
var label = nodeNode.label;
var color = '#'+sigma.tools.rgbToHex(parseFloat(nodeNode.color.r), parseFloat(nodeNode.color.g), parseFloat(nodeNode.color.b))
var x = parseFloat(nodeNode.position.x);
var y = parseFloat(nodeNode.position.y);
var size = parseFloat(nodeNode.size);
var clean_node = {id:id, label:label, color:color, x:x, y:y, attributes:[], size:size};
for (j=0; j < nodeNode.attributes.length; j++){
var raw_attribute = nodeNode.attributes[j];
var attr = raw_attribute.for;
var val = raw_attribute.value;
clean_node.attributes.push({attr:attr, val:val});
}
sigmaInstance.addNode(id, clean_node);
};
for(i=0; i<data.edges.length; i++){
var edgeNode = data.edges[i];
var edge = {
id: i,
sourceID: edgeNode.source,
targetID: edgeNode.target,
label: edgeNode.label,
weight: parseFloat(edgeNode.weight),
displaySize: parseFloat(edgeNode.weight),
attributes: []
};
for (j=0; j < edgeNode.attributes.length; j++){
var raw_attribute = edgeNode.attributes[j];
var attr = raw_attribute.for;
var val = raw_attribute.value;
edge.attributes.push({attr:attr, val:val});
}
sigmaInstance.addEdge(i, edgeNode.source, edgeNode.target, edge);
};
if (callback) callback.call(this);
});
};
@psychemedia
Copy link

To what extent does the JSON format you supporting here resemble the JSON format used to pass network data into d3js (which is also the same format that can be exported from py/networkx http://networkx.github.com/documentation/latest/reference/readwrite.json_graph.html )

@audvin
Copy link

audvin commented May 10, 2013

Is there a working example located somewhere that we can explore? Thanks! :)

@DGaffney
Copy link
Author

Jeez! Didn't see these messages. Expect a response in a few days from me.

@DGaffney
Copy link
Author

@pducrot
Copy link

pducrot commented Jan 4, 2014

Hi,
I'm trying to display a graph with sigma.js from data in JSON format. I started from the "parse GEXF" example, converted the "les_miserables.gexf" file with the provided python script, changed the html code to load the sigma.parseJson.js. The graph doesn't show up, I have the following error message in the browser console: "TypeError: 'undefined' is not an object (evaluating 'data.nodes.length') in sigma.parseJson.js". Any idea? Would you have a working example that I could start from instead?
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment