Browse Source

Fixed hierarchical layouts with nodes that start at a level > 0 #220

v3_develop
Alex de Mulder 10 years ago
parent
commit
5fd89bd083
5 changed files with 53 additions and 29 deletions
  1. +1
    -0
      HISTORY.md
  2. +0
    -1
      examples/network/27_world_cup_network.html
  3. +26
    -9
      lib/network/Edge.js
  4. +2
    -0
      lib/network/Network.js
  5. +24
    -19
      lib/network/mixins/HierarchicalLayoutMixin.js

+ 1
- 0
HISTORY.md View File

@ -28,6 +28,7 @@ http://visjs.org
- Updated max velocity of nodes to three times it's original value. - Updated max velocity of nodes to three times it's original value.
- Made "stabilized" event fire every time the network stabilizes. - Made "stabilized" event fire every time the network stabilizes.
- Fixed drift in dragging nodes while zooming. - Fixed drift in dragging nodes while zooming.
- Fixed recursively constructing of hierarchical layouts.

+ 0
- 1
examples/network/27_world_cup_network.html View File

@ -24,7 +24,6 @@
This example shows the effect of the different types on a large network. This example shows the effect of the different types on a large network.
<br /> <br /> <br /> <br />
Also shown in this example is the inheritColor option of the edges as well as the roundness factor. <br /> Also shown in this example is the inheritColor option of the edges as well as the roundness factor. <br />
The roundness factor is only functional with "continuous", "discrete" and "diagonalCross".
<br /><br /> <br /><br />
To improve performance, the physics have been disabled with: To improve performance, the physics have been disabled with:
<br/><code>{barnesHut: {gravitationalConstant: 0, centralGravity: 0, springConstant: 0}}</code><br/> and we have enabled <br/><code>{barnesHut: {gravitationalConstant: 0, centralGravity: 0, springConstant: 0}}</code><br/> and we have enabled

+ 26
- 9
lib/network/Edge.js View File

@ -361,9 +361,6 @@ Edge.prototype._getViaCoordinates = function () {
var yVia = null; var yVia = null;
var factor = this.smoothCurves.roundness; var factor = this.smoothCurves.roundness;
var type = this.smoothCurves.type; var type = this.smoothCurves.type;
if (factor == 0) {
return {x:null,y:null};
}
var dx = Math.abs(this.from.x - this.to.x); var dx = Math.abs(this.from.x - this.to.x);
var dy = Math.abs(this.from.y - this.to.y); var dy = Math.abs(this.from.y - this.to.y);
@ -420,22 +417,42 @@ Edge.prototype._getViaCoordinates = function () {
} }
} }
else if (type == "straightCross") { else if (type == "straightCross") {
if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) {
if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) { // up - down
xVia = this.from.x; xVia = this.from.x;
yVia = this.to.y
if (this.from.y < this.to.y) {
yVia = this.to.y - (1-factor) * dy;
}
else {
yVia = this.to.y + (1-factor) * dy;
}
} }
else if (Math.abs(this.from.x - this.to.x) > Math.abs(this.from.y - this.to.y)) {
xVia = this.to.x;
else if (Math.abs(this.from.x - this.to.x) > Math.abs(this.from.y - this.to.y)) { // left - right
if (this.from.x < this.to.x) {
xVia = this.to.x - (1-factor) * dx;
}
else {
xVia = this.to.x + (1-factor) * dx;
}
yVia = this.from.y; yVia = this.from.y;
} }
} }
else if (type == 'horizontal') { else if (type == 'horizontal') {
xVia = this.to.x;
if (this.from.x < this.to.x) {
xVia = this.to.x - (1-factor) * dx;
}
else {
xVia = this.to.x + (1-factor) * dx;
}
yVia = this.from.y; yVia = this.from.y;
} }
else if (type == 'vertical') { else if (type == 'vertical') {
xVia = this.from.x; xVia = this.from.x;
yVia = this.to.y;
if (this.from.y < this.to.y) {
yVia = this.to.y - (1-factor) * dy;
}
else {
yVia = this.to.y + (1-factor) * dy;
}
} }
else { // continuous else { // continuous
if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) { if (Math.abs(this.from.x - this.to.x) < Math.abs(this.from.y - this.to.y)) {

+ 2
- 0
lib/network/Network.js View File

@ -1068,6 +1068,8 @@ Network.prototype._onDragEnd = function () {
s.node.xFixed = s.xFixed; s.node.xFixed = s.xFixed;
s.node.yFixed = s.yFixed; s.node.yFixed = s.yFixed;
}); });
this.moving = true;
this.start();
} }
this._redraw(); this._redraw();
}; };

+ 24
- 19
lib/network/mixins/HierarchicalLayoutMixin.js View File

@ -94,26 +94,31 @@ exports._placeNodesByHierarchy = function(distribution) {
var nodeId, node; var nodeId, node;
// start placing all the level 0 nodes first. Then recursively position their branches. // start placing all the level 0 nodes first. Then recursively position their branches.
for (nodeId in distribution[0].nodes) {
if (distribution[0].nodes.hasOwnProperty(nodeId)) {
node = distribution[0].nodes[nodeId];
if (this.constants.hierarchicalLayout.direction == "UD" || this.constants.hierarchicalLayout.direction == "DU") {
if (node.xFixed) {
node.x = distribution[0].minPos;
node.xFixed = false;
distribution[0].minPos += distribution[0].nodeSpacing;
}
}
else {
if (node.yFixed) {
node.y = distribution[0].minPos;
node.yFixed = false;
for (var level in distribution) {
if (distribution.hasOwnProperty(level)) {
distribution[0].minPos += distribution[0].nodeSpacing;
for (nodeId in distribution[level].nodes) {
if (distribution[level].nodes.hasOwnProperty(nodeId)) {
node = distribution[level].nodes[nodeId];
if (this.constants.hierarchicalLayout.direction == "UD" || this.constants.hierarchicalLayout.direction == "DU") {
if (node.xFixed) {
node.x = distribution[level].minPos;
node.xFixed = false;
distribution[level].minPos += distribution[level].nodeSpacing;
}
}
else {
if (node.yFixed) {
node.y = distribution[level].minPos;
node.yFixed = false;
distribution[level].minPos += distribution[level].nodeSpacing;
}
}
this._placeBranchNodes(node.edges,node.id,distribution,node.level);
} }
} }
this._placeBranchNodes(node.edges,node.id,distribution,node.level);
} }
} }
@ -145,11 +150,11 @@ exports._getDistribution = function() {
else { else {
node.x = this.constants.hierarchicalLayout.levelSeparation*node.level; node.x = this.constants.hierarchicalLayout.levelSeparation*node.level;
} }
if (!distribution.hasOwnProperty(node.level)) {
if (distribution[node.level] === undefined) {
distribution[node.level] = {amount: 0, nodes: {}, minPos:0, nodeSpacing:0}; distribution[node.level] = {amount: 0, nodes: {}, minPos:0, nodeSpacing:0};
} }
distribution[node.level].amount += 1; distribution[node.level].amount += 1;
distribution[node.level].nodes[node.id] = node;
distribution[node.level].nodes[nodeId] = node;
} }
} }

Loading…
Cancel
Save