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 for(j = 0; j<attributeNodes.length; j++){
\r
32 var attributeNode = attributeNodes[j]; // Each xml node 'attribute'
\r
34 var id = attributeNode.getAttribute('id'),
\r
35 title = attributeNode.getAttribute('title'),
\r
36 type = attributeNode.getAttribute('type');
\r
38 var attribute = {id:id, title:title, type:type};
\r
39 nodesAttributes.push(attribute);
\r
41 } else if(attributesNode.getAttribute('class') == 'edge'){
\r
42 var attributeNodes = attributesNode.getElementsByTagName('attribute'); // The list of xml nodes 'attribute' (no 's')
\r
43 for(j = 0; j<attributeNodes.length; j++){
\r
44 var attributeNode = attributeNodes[j]; // Each xml node 'attribute'
\r
46 var id = attributeNode.getAttribute('id'),
\r
47 title = attributeNode.getAttribute('title'),
\r
48 type = attributeNode.getAttribute('type');
\r
50 var attribute = {id:id, title:title, type:type};
\r
51 edgesAttributes.push(attribute);
\r
57 var nodes = []; // The nodes of the graph
\r
58 var nodesNodes = gexf.getElementsByTagName('nodes') // The list of xml nodes 'nodes' (plural)
\r
60 for(i=0; i<nodesNodes.length; i++){
\r
61 var nodesNode = nodesNodes[i]; // Each xml node 'nodes' (plural)
\r
62 var nodeNodes = nodesNode.getElementsByTagName('node'); // The list of xml nodes 'node' (no 's')
\r
64 for(j=0; j<nodeNodes.length; j++){
\r
65 var nodeNode = nodeNodes[j]; // Each xml node 'node' (no 's')
\r
67 window.NODE = nodeNode;
\r
69 var id = nodeNode.getAttribute('id');
\r
70 var label = nodeNode.getAttribute('label') || id;
\r
74 var x = 100 - 200*Math.random();
\r
75 var y = 100 - 200*Math.random();
\r
78 var sizeNodes = nodeNode.getElementsByTagName('size');
\r
79 sizeNodes = sizeNodes.length ?
\r
81 nodeNode.getElementsByTagNameNS('*','size');
\r
82 if(sizeNodes.length>0){
\r
83 sizeNode = sizeNodes[0];
\r
84 size = parseFloat(sizeNode.getAttribute('value'));
\r
87 var positionNodes = nodeNode.getElementsByTagName('position');
\r
88 positionNodes = positionNodes.length ?
\r
90 nodeNode.getElementsByTagNameNS('*','position');
\r
91 if(positionNodes.length>0){
\r
92 var positionNode = positionNodes[0];
\r
93 x = parseFloat(positionNode.getAttribute('x'));
\r
94 y = parseFloat(positionNode.getAttribute('y'));
\r
97 var colorNodes = nodeNode.getElementsByTagName('color');
\r
98 colorNodes = colorNodes.length ?
\r
100 nodeNode.getElementsByTagNameNS('*','color');
\r
101 if(colorNodes.length>0){
\r
102 colorNode = colorNodes[0];
\r
103 color = '#'+sigma.tools.rgbToHex(parseFloat(colorNode.getAttribute('r')),
\r
104 parseFloat(colorNode.getAttribute('g')),
\r
105 parseFloat(colorNode.getAttribute('b')));
\r
109 var node = {label:label, size:size, x:x, y:y, attributes:[], color:color}; // The graph node
\r
111 // Attribute values
\r
112 var attvalueNodes = nodeNode.getElementsByTagName('attvalue');
\r
113 for(k=0; k<attvalueNodes.length; k++){
\r
114 var attvalueNode = attvalueNodes[k];
\r
115 var attr = attvalueNode.getAttribute('for');
\r
116 var title = nodesAttributes[k]['title']
\r
117 var val = attvalueNode.getAttribute('value');
\r
118 node.attributes.push({attr:title, val:val});
\r
121 sigmaInstance.addNode(id,node);
\r
127 var edgesNodes = gexf.getElementsByTagName('edges');
\r
128 for(i=0; i<edgesNodes.length; i++){
\r
129 var edgesNode = edgesNodes[i];
\r
130 var edgeNodes = edgesNode.getElementsByTagName('edge');
\r
131 for(j=0; j<edgeNodes.length; j++){
\r
132 var edgeNode = edgeNodes[j];
\r
133 var source = edgeNode.getAttribute('source');
\r
134 var target = edgeNode.getAttribute('target');
\r
135 var label = edgeNode.getAttribute('label');
\r
144 var weight = edgeNode.getAttribute('weight');
\r
145 if(weight!=undefined){
\r
146 edge['weight'] = weight;
\r
149 var attvalueNodes = edgeNode.getElementsByTagName('attvalue');
\r
150 for(k=0; k<attvalueNodes.length; k++){
\r
151 var attvalueNode = attvalueNodes[k];
\r
152 var attr = attvalueNode.getAttribute('for');
\r
153 var val = attvalueNode.getAttribute('value');
\r
154 edge.attributes.push({attr:attr, val:val});
\r
157 sigmaInstance.addEdge(edgeId++,source,target,edge);
\r