Spaces:
Running
Running
// // Scott Hale (Oxford Internet Institute) | |
// // Requires sigma.js and jquery to be loaded | |
// // based on parseGexf from Mathieu Jacomy @ Sciences Po M�dialab & WebAtlas | |
sigma.publicPrototype.parseJson = function(gzippedJsonPath, callback) { | |
var sigmaInstance = this; | |
// Use XMLHttpRequest for binary data | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', gzippedJsonPath, true); | |
xhr.responseType = 'arraybuffer'; | |
xhr.onload = function() { | |
if (xhr.status === 200) { | |
try { | |
// Decompress the gzipped data using pako | |
var inflatedData = pako.inflate(new Uint8Array(xhr.response)); | |
// Convert binary data to string | |
var jsonString = new TextDecoder('utf-8').decode(inflatedData); | |
// Parse the JSON | |
var jsonData = JSON.parse(jsonString); | |
// Process nodes | |
for (var i = 0; i < jsonData.nodes.length; i++) { | |
var id = jsonData.nodes[i].id; | |
sigmaInstance.addNode(id, jsonData.nodes[i]); | |
} | |
// Process edges | |
for (var j = 0; j < jsonData.edges.length; j++) { | |
var edgeNode = jsonData.edges[j]; | |
var source = edgeNode.source; | |
var target = edgeNode.target; | |
var label = edgeNode.label; | |
var eid = edgeNode.id; | |
sigmaInstance.addEdge(eid, source, target, edgeNode); | |
} | |
// Call the callback function if provided | |
if (callback) callback.call(sigmaInstance); | |
} catch (error) { | |
console.error("Error processing gzipped JSON:", error); | |
} | |
} else { | |
console.error("Error fetching gzipped JSON. Status:", xhr.status); | |
} | |
}; | |
xhr.onerror = function() { | |
console.error("Network error while fetching gzipped JSON"); | |
}; | |
xhr.send(); | |
}; |