NxTIC/js/includes/sociogram-min.js

11 lines
4.0 KiB
JavaScript
Raw Normal View History

function sociogramClass(a){this.container=a;this.log("sociogram created")}
sociogramClass.prototype={container:this.container,sigma:null,request:{path:"charts/network_data"},response:null,nodes:[],edges:[],rad:500,nodeDistance:100,overloadGraph:function(){},load:function(){},nodeAt:function(a,b){},arrange:function(a,b,d){},extractNodesFromResponse:function(){},extractEdgesFromResponse:function(){},renderNodes:function(){},renderEdges:function(){},overload:{nodeNeighbors:function(a){},nodeDirectNeighbors:function(a){}},bindings:{clickNode:function(a,b){},clickStage:function(a,
b){}},log:function(a){console.log("--[SOCIOGRAM]--");console.warn(a);console.log("--[/SOCIOGRAM]--")}};sociogramClass.prototype.overloadGraph=function(){sigma.classes.graph.addMethod("nodeNeighbors",this.overload.nodeNeighbors);sigma.classes.graph.addMethod("nodeDirectNeighbors",this.overload.nodeDirectNeighbors);this.log("graph overloaded")};
sociogramClass.prototype.load=function(){this.sigma=new sigma({renderer:{container:this.container,type:"canvas"}});var a=this;api.send(this.request,function(b){a.log(b);0==b.ModuleError&&(a.response=b,a.sigma.settings({defaultNodeColor:"#348ed8",defaultLabelSize:14,defaultLabelBGColor:"#ddd",defaultHoverLabelBGColor:"#002147",defaultLabelHoverColor:"#fff",labelThreshold:10,defaultEdgeType:"line"}),a.extractNodesFromResponse(),a.extractEdgesFromResponse(),a.addNodes(),a.addEdges(),a.sigma.bind("clickNode",
function(b){a.bindings.clickNode(a,b)}),a.sigma.bind("clickStage",function(b){a.bindings.clickStage(a,b)}),a.sigma.graph.nodes().forEach(function(b){a.arrange(b.id,null,!0)}),a.sigma.camera.ratio=1.2,a.sigma.refresh())})};sociogramClass.prototype.nodeAt=function(a,b){var d=this.sigma.graph.nodes(),c=null,e;for(e in d){var f=Math.sqrt(Math.pow(a-d[e].x,2)+Math.pow(b-d[e].y,2));if(null==c||f<c)c=f}return c};
sociogramClass.prototype.arrange=function(a,b,d){var c=this.sigma.graph.nodes(a);if(0==c.x&&0==c.y){b=null==b?{x:c.x,y:c.y}:b;if(d)for(;this.nodeAt(b.x,b.y)<2*this.nodeDistance;)b={x:b.x+2*this.nodeDistance*Math.cos(2*Math.random()*Math.PI),y:b.y+2*this.nodeDistance*Math.sin(2*Math.random()*Math.PI)};d=this.sigma.graph.nodeDirectNeighbors(a);a={};var e=0,f;for(f in d)a[f]=this.sigma.graph.nodes(f),e++;c.x=b.x;c.y=b.y;c=[];for(f in a)if(a=this.sigma.graph.nodes(f),0==a.x&&0==a.y){e=!1;do{d=2*Math.random()*
Math.PI;for(var g=0;g<c.length;g++)if(Math.abs(d-c[g])>Math.PI/10){e=!0;break}}while(e);a.x=b.x+this.nodeDistance*Math.cos(d);a.y=b.y+this.nodeDistance*Math.sin(d);this.arrange(a.id)}this.sigma.refresh()}};sociogramClass.prototype.overload.nodeNeighbors=function(a){a=this.allNeighborsIndex[a];var b=[],d;for(d in a)b.push(d);for(;0<b.length;){d=this.allNeighborsIndex[b[0]];for(var c in d)null==a[c]&&(b.push(c),a[c]=d[c]);b.shift()}return a};sociogramClass.prototype.overload.nodeDirectNeighbors=function(a){return this.allNeighborsIndex[a]};
sociogramClass.prototype.extractNodesFromResponse=function(){this.nodes=[];for(var a=0;a<this.response.data.alter.length;a++)this.nodes.push({id:"n-"+this.response.data.alter[a][0],label:this.response.data.alter[a][1],x:0,y:0,size:this.response.data.alter[a][2]})};
sociogramClass.prototype.extractEdgesFromResponse=function(){this.edges=[];for(var a=0;a<this.response.data.inter.length;a++)this.edges.push({id:"e-"+this.response.data.inter[a][0]+"-"+this.response.data.inter[a][1],source:"n-"+this.response.data.inter[a][0],target:"n-"+this.response.data.inter[a][1]})};sociogramClass.prototype.addNodes=function(){for(var a=0;a<this.nodes.length;a++)this.sigma.graph.addNode(this.nodes[a])};sociogramClass.prototype.addEdges=function(){for(var a=0;a<this.edges.length;a++)this.sigma.graph.addEdge(this.edges[a])};
sociogramClass.prototype.bindings.clickNode=function(a,b){console.log(a);var d=b.data.node.id,c=a.sigma.graph.nodeNeighbors(d);c[d]=b.data.node;a.sigma.graph.nodes().forEach(function(a){a.color=null!=c[a.id]?a.originalColor:"#888"});a.sigma.refresh()};sociogramClass.prototype.bindings.clickStage=function(a,b){a.sigma.graph.nodes().forEach(function(a){a.color=a.originalColor});a.sigma.refresh()};