1 // Mathieu Jacomy @ Sciences Po Médialab & WebAtlas
\r
2 // (requires sigma.js to be loaded)
\r
3 sigma.publicPrototype.parseGexf = function(gexfPath) {
\r
6 var sigmaInstance = this;
\r
7 gexfhttp = window.XMLHttpRequest ?
\r
8 new XMLHttpRequest() :
\r
9 new ActiveXObject('Microsoft.XMLHTTP');
\r
11 gexfhttp.overrideMimeType('text/xml');
\r
12 gexfhttp.open('GET', gexfPath, false);
\r
14 gexf = gexfhttp.responseXML;
\r
16 var viz='http://www.gexf.net/1.2draft/viz'; // Vis namespace
\r
20 // This is confusing, so I'll comment heavily
\r
21 var nodesAttributes = []; // The list of attributes of the nodes of the graph that we build in json
\r
22 var edgesAttributes = []; // The list of attributes of the edges of the graph that we build in json
\r
23 var attributesNodes = gexf.getElementsByTagName('attributes'); // In the gexf (that is an xml), the list of xml nodes 'attributes' (note the plural 's')
\r
26 for(i = 0; i<attributesNodes.length; i++){
\r
27 var attributesNode = attributesNodes[i]; // attributesNode is each xml node 'attributes' (plural)
\r
29 if(attributesNode.getAttribute('class') == 'node'){
\r
30 var attributeNodes = attributesNode.getElementsByTagName('attribute'); // The list of xml nodes 'attribute' (no 's')
\r
31 console.log(attributeNodes)
\r
32 for(j = 0; j<attributeNodes.length; j++){
\r
33 var attributeNode = attributeNodes[j]; // Each xml node 'attribute'
\r
35 var id = attributeNode.getAttribute('id'),
\r
36 title = attributeNode.getAttribute('title'),
\r
37 type = attributeNode.getAttribute('type');
\r
39 var attribute = {id:id, title:title, type:type};
\r
40 nodesAttributes.push(attribute);
\r
41 console.log(nodesAttributes)
\r
44 } else if(attributesNode.getAttribute('class') == 'edge'){
\r
45 var attributeNodes = attributesNode.getElementsByTagName('attribute'); // The list of xml nodes 'attribute' (no 's')
\r
46 for(j = 0; j<attributeNodes.length; j++){
\r
47 var attributeNode = attributeNodes[j]; // Each xml node 'attribute'
\r
49 var id = attributeNode.getAttribute('id'),
\r
50 title = attributeNode.getAttribute('title'),
\r
51 type = attributeNode.getAttribute('type');
\r
53 var attribute = {id:id, title:title, type:type};
\r
54 edgesAttributes.push(attribute);
\r
60 var nodes = []; // The nodes of the graph
\r
61 var nodesNodes = gexf.getElementsByTagName('nodes') // The list of xml nodes 'nodes' (plural)
\r
63 for(i=0; i<nodesNodes.length; i++){
\r
64 var nodesNode = nodesNodes[i]; // Each xml node 'nodes' (plural)
\r
65 var nodeNodes = nodesNode.getElementsByTagName('node'); // The list of xml nodes 'node' (no 's')
\r
67 for(j=0; j<nodeNodes.length; j++){
\r
68 var nodeNode = nodeNodes[j]; // Each xml node 'node' (no 's')
\r
70 window.NODE = nodeNode;
\r
72 var id = nodeNode.getAttribute('id');
\r
73 var label = nodeNode.getAttribute('label') || id;
\r
77 var x = 100 - 200*Math.random();
\r
78 var y = 100 - 200*Math.random();
\r
81 var sizeNodes = nodeNode.getElementsByTagName('size');
\r
82 sizeNodes = sizeNodes.length ?
\r
84 nodeNode.getElementsByTagNameNS('*','size');
\r
85 if(sizeNodes.length>0){
\r
86 sizeNode = sizeNodes[0];
\r
87 size = parseFloat(sizeNode.getAttribute('value'));
\r
90 var positionNodes = nodeNode.getElementsByTagName('position');
\r
91 positionNodes = positionNodes.length ?
\r
93 nodeNode.getElementsByTagNameNS('*','position');
\r
94 if(positionNodes.length>0){
\r
95 var positionNode = positionNodes[0];
\r
96 x = parseFloat(positionNode.getAttribute('x'));
\r
97 y = parseFloat(positionNode.getAttribute('y'));
\r
100 var colorNodes = nodeNode.getElementsByTagName('color');
\r
101 colorNodes = colorNodes.length ?
\r
103 nodeNode.getElementsByTagNameNS('*','color');
\r
104 if(colorNodes.length>0){
\r
105 colorNode = colorNodes[0];
\r
106 color = '#'+sigma.tools.rgbToHex(parseFloat(colorNode.getAttribute('r')),
\r
107 parseFloat(colorNode.getAttribute('g')),
\r
108 parseFloat(colorNode.getAttribute('b')));
\r
112 var node = {label:label, size:size, x:x, y:y, attributes:[], color:color}; // The graph node
\r
114 // Attribute values
\r
115 var attvalueNodes = nodeNode.getElementsByTagName('attvalue');
\r
116 for(k=0; k<attvalueNodes.length; k++){
\r
117 var attvalueNode = attvalueNodes[k];
\r
118 var attr = attvalueNode.getAttribute('for');
\r
119 var title = nodesAttributes[k]['title']
\r
120 var val = attvalueNode.getAttribute('value');
\r
121 node.attributes.push({attr:title, val:val});
\r
124 sigmaInstance.addNode(id,node);
\r
130 var edgesNodes = gexf.getElementsByTagName('edges');
\r
131 for(i=0; i<edgesNodes.length; i++){
\r
132 var edgesNode = edgesNodes[i];
\r
133 var edgeNodes = edgesNode.getElementsByTagName('edge');
\r
134 for(j=0; j<edgeNodes.length; j++){
\r
135 var edgeNode = edgeNodes[j];
\r
136 var source = edgeNode.getAttribute('source');
\r
137 var target = edgeNode.getAttribute('target');
\r
138 var label = edgeNode.getAttribute('label');
\r
147 var weight = edgeNode.getAttribute('weight');
\r
148 if(weight!=undefined){
\r
149 edge['weight'] = weight;
\r
152 var attvalueNodes = edgeNode.getElementsByTagName('attvalue');
\r
153 for(k=0; k<attvalueNodes.length; k++){
\r
154 var attvalueNode = attvalueNodes[k];
\r
155 var attr = attvalueNode.getAttribute('for');
\r
156 var val = attvalueNode.getAttribute('value');
\r
157 edge.attributes.push({attr:attr, val:val});
\r
160 sigmaInstance.addEdge(edgeId++,source,target,edge);
\r