提交 2a36705f 编写于 作者: R Rajkumar Sriramulu

Updated layout stratergy to draw nodes

上级 21374adc
...@@ -73,10 +73,17 @@ class DirectedGraphConstructTracer extends Tracer { ...@@ -73,10 +73,17 @@ class DirectedGraphConstructTracer extends Tracer {
return this; return this;
} }
_clearTraversal() {
this.manager.pushStep(this.capsule, {
type: 'clear'
});
return this;
}
processStep(step, options) { processStep(step, options) {
switch (step.type) { switch (step.type) {
case 'setTreeData': case 'clear':
this.setTreeData.apply(this, step.arguments); this.clear.apply(this);
break; break;
case 'setNodePositions': case 'setNodePositions':
$.each(this.graph.nodes(), (i, node) => { $.each(this.graph.nodes(), (i, node) => {
...@@ -159,7 +166,6 @@ class DirectedGraphConstructTracer extends Tracer { ...@@ -159,7 +166,6 @@ class DirectedGraphConstructTracer extends Tracer {
} }
} }
} }
nodeObject.updateBreadth();
this.nodeCollection.push(nodeObject); this.nodeCollection.push(nodeObject);
return nodeObject; return nodeObject;
} }
...@@ -169,23 +175,10 @@ class DirectedGraphConstructTracer extends Tracer { ...@@ -169,23 +175,10 @@ class DirectedGraphConstructTracer extends Tracer {
id: this.n(val), id: this.n(val),
originalVal: val, originalVal: val,
isNew: true, isNew: true,
visited: false,
children: [], children: [],
breadth: 0,
level: 1, level: 1,
parent: null, parent: null
visited: false,
updateBreadth: function() {
var oldBreadth = nodeObject.breadth;
if ( nodeObject.children.length > 0 ) {
nodeObject.breadth = nodeObject.children.length % 2 ? 0 : 1;
for (let j = 0; j < nodeObject.children.length; j++) {
nodeObject.breadth += nodeObject.children[j].breadth;
}
} else { nodeObject.breadth = 1; }
if ( oldBreadth !== nodeObject.breadth && nodeObject.parent ) {
nodeObject.parent.updateBreadth();
}
}
} }
return nodeObject; return nodeObject;
} }
...@@ -194,43 +187,60 @@ class DirectedGraphConstructTracer extends Tracer { ...@@ -194,43 +187,60 @@ class DirectedGraphConstructTracer extends Tracer {
const nodes = []; const nodes = [];
const edges = []; const edges = [];
var tracer = this; var tracer = this;
var drawNode = function (node, parentNode, occupiedBreadth) {
var calculatedX = node.breadth; var arrangeChildNodes = function(node, offsetWidth) {
if (parentNode) { if(node.children.length > 1){
calculatedX = parentNode.breadth + occupiedBreadth - node.breadth; var midPoint = Math.floor(node.children.length / 2);
} else if (node.children.length > 0) { for (let i = 0; i < node.children.length; i++) {
calculatedX = Math.ceil(calculatedX/node.children.length); if (i===midPoint) {
offsetWidth += (node.children.length % 2 === 0 ? 1 : 0);
addGraphNode(node, offsetWidth);
}
offsetWidth = arrangeChildNodes(node.children[i], offsetWidth);
addEdge(node, node.children[i]);
} }
} else {
if (node.children.length === 0) {
offsetWidth += 1;
} else {
offsetWidth = arrangeChildNodes(node.children[0], offsetWidth);
addEdge(node, node.children[0]);
}
addGraphNode(node, offsetWidth);
}
return offsetWidth;
};
var addGraphNode = function (node, calculatedX) {
var color = getColor(node.isNew, node.visited, tracer.color);
nodes.push({ nodes.push({
id: node.id, id: node.id,
label: '' + node.originalVal, label: '' + node.originalVal,
x: calculatedX, x: calculatedX,
y: node.level - 1, y: node.level - 1,
size: 1, size: 1,
color: node.isNew ? tracer.color.selected : (node.visited ? tracer.color.visited : tracer.color.default), color: color,
weight: 0 weight: 0
}); });
};
if ( node.children.length > 0 ) { var addEdge = function (node, childNode) {
var midPoint = node.children.length / 2; var color = getColor(node.visited && childNode.isNew, node.visited && childNode.visited, tracer.color);
var occupiedBreadth = 0;
for (let j = 0; j < node.children.length; j++) {
var childNode = node.children[j];
edges.push({ edges.push({
id: tracer.e(node.originalVal, childNode.originalVal), id: tracer.e(node.originalVal, childNode.originalVal),
source: node.id, source: node.id,
target: childNode.id, target: childNode.id,
color: node.visited && childNode.visited ? tracer.color.visited : tracer.color.default, color: color,
size: 1, size: 1,
weight: refineByType(childNode.originalVal) weight: refineByType(childNode.originalVal)
}); });
drawNode(childNode, node, occupiedBreadth);
occupiedBreadth += node.breadth;
}
}
}; };
drawNode(this.rootObject);
var getColor = function (isNew, isVisited, colorPalete) {
return isNew ? colorPalete.selected :
(isVisited ? colorPalete.visited : colorPalete.default);
};
arrangeChildNodes(this.rootObject, 0);
this.graph.clear(); this.graph.clear();
this.graph.read({ this.graph.read({
...@@ -269,8 +279,9 @@ class DirectedGraphConstructTracer extends Tracer { ...@@ -269,8 +279,9 @@ class DirectedGraphConstructTracer extends Tracer {
} }
clearGraphColor() { clearGraphColor() {
var tracer = this;
this.nodeCollection.forEach(function(node){ this.nodeCollection.forEach(function(node){
node.isNew = false; node.visited = node.isNew = false;
}); });
this.graph.nodes().forEach(function (node) { this.graph.nodes().forEach(function (node) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册