Improment to tree view

This commit is contained in:
Ruben van de Ven 2019-02-18 23:13:42 +01:00
parent 8722203704
commit e2a05a18bb

View file

@ -1023,7 +1023,7 @@ class Graph {
.force( "link", d3.forceLink( this.directions ).id( d => d['@id'] ).strength(0) ) .force( "link", d3.forceLink( this.directions ).id( d => d['@id'] ).strength(0) )
// .force( "charge", d3.forceManyBody().strength( 100 ) ) // .force( "charge", d3.forceManyBody().strength( 100 ) )
// .force( "center", d3.forceCenter( this.width / 2, this.height / 2 ) ) // .force( "center", d3.forceCenter( this.width / 2, this.height / 2 ) )
.force( "collide", d3.forceCollide( this.nodeSize * 2.3 ) ) .force( "collide", d3.forceCollide( this.nodeSize * 2.3 ).strength(2) )
.force( "forceX", d3.forceX(function(m){ .force( "forceX", d3.forceX(function(m){
let fx = panopticon.graph.distances[m['@id']] !== null ? panopticon.graph.distances[m['@id']] * panopticon.graph.nodeSize * 4 : 0 let fx = panopticon.graph.distances[m['@id']] !== null ? panopticon.graph.distances[m['@id']] * panopticon.graph.nodeSize * 4 : 0
console.log('fx', m['@id'], panopticon.graph.distances[m['@id']], fx); console.log('fx', m['@id'], panopticon.graph.distances[m['@id']], fx);
@ -1031,7 +1031,7 @@ class Graph {
}).strength(50)) }).strength(50))
.force( "forceY", d3.forceY(m => panopticon.graph.distances[m['@id']] !== null ? 0 : panopticon.graph.nodeSize * 3 ).strength(30)) .force( "forceY", d3.forceY(m => panopticon.graph.distances[m['@id']] !== null ? 0 : panopticon.graph.nodeSize * 3 ).strength(30))
; ;
this.simulation.velocityDecay(.98); this.simulation.velocityDecay(.99);
// Update existing nodes // Update existing nodes
let node = this.nodesG let node = this.nodesG
@ -1190,34 +1190,70 @@ class Graph {
distances[msg['@id']] = msg === startMsg ? 0 : null; distances[msg['@id']] = msg === startMsg ? 0 : null;
} }
let directionsPerMsg = {}; let targetsPerMsg = {};
let sourcesPerMsg = {};
console.log("dir", this.directions); console.log("dir", this.directions);
for(let direction of this.directions) { for(let direction of this.directions) {
let from = typeof direction['source'] == "string" ? direction['source'] : direction['source']['@id']; let from = typeof direction['source'] == "string" ? direction['source'] : direction['source']['@id'];
let to = typeof direction['target'] == "string" ? direction['target'] : direction['target']['@id']; let to = typeof direction['target'] == "string" ? direction['target'] : direction['target']['@id'];
if(!directionsPerMsg.hasOwnProperty(from)) { if(!targetsPerMsg.hasOwnProperty(from)) {
directionsPerMsg[from] = []; targetsPerMsg[from] = [];
} }
directionsPerMsg[from].push(to); targetsPerMsg[from].push(to);
if(!sourcesPerMsg.hasOwnProperty(to)) {
sourcesPerMsg[to] = [];
}
sourcesPerMsg[to].push(from);
} }
let traverseMsg = function(msgId, depth) { let traverseMsg = function(msgId, depth, goingDown) {
if(!directionsPerMsg.hasOwnProperty(msgId)) { let msgsPerMsg = goingDown ? targetsPerMsg : sourcesPerMsg;
console.log(goingDown, msgId, depth);
if(!msgsPerMsg.hasOwnProperty(msgId)) {
// end of trail // end of trail
return; return;
} }
for(let childMsgId of directionsPerMsg[msgId]) { for(let childMsgId of msgsPerMsg[msgId]) {
if(distances[childMsgId] === null || distances[childMsgId] > depth) { if(distances[childMsgId] === null || (goingDown && distances[childMsgId] > depth)) {
distances[childMsgId] = depth; distances[childMsgId] = depth;
traverseMsg(childMsgId, depth+1); // console.log(goingDown, childMsgId, depth);
traverseMsg(childMsgId, goingDown ? (depth+1) : (depth - 1), goingDown);
}
else if(!goingDown && distances[childMsgId] < depth) {
// console.log('a', depth);
traverseMsg(childMsgId, depth - 1, goingDown);
} else { } else {
// apparently, there is a loop. Don't traverse it. // apparently, there is a loop. Don't traverse it.
} }
} }
} }
traverseMsg(startMsg['@id'], 1); traverseMsg(startMsg['@id'], 1 , true);
// now we have the formal tree, lets try to polish the rest:
for(let msgId in distances) {
if(distances[msgId] === null) {
continue;
}
// let's see if there are parent nodes that are not in the distances array
// traverse up and see whether we encounter anything new
traverseMsg(msgId, distances[msgId] -1, false)
}
// let additionalsDepth = 0;
//// now the secondary strands:
// for(let msgId in distances) {
// if(distances[msgId] !== null || sourcesPerMsg.hasOwnProperty(msgId)) {
// // it is already calculated, or it has a parent node (which we should traverse instead)
// continue;
// }
// distances[msgId] = additionalsDepth;
// traverseMsg(msgId, additionalsDepth+1, true);
//
// }
return distances; return distances;
} }