Skip to content

Instantly share code, notes, and snippets.

@DGaffney DGaffney/sigma.parseJSON.js Secret
Created Feb 6, 2013

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

psychemedia commented Feb 22, 2013

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

This comment has been minimized.

Copy link

audvin commented May 10, 2013

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

@DGaffney

This comment has been minimized.

Copy link
Owner Author

DGaffney commented Sep 12, 2013

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

@DGaffney

This comment has been minimized.

@pducrot

This comment has been minimized.

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
You can’t perform that action at this time.