Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Commit

Permalink
added hierarchical layout
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDM0 committed Feb 20, 2014
1 parent 2591b8a commit 8a3c45e
Show file tree
Hide file tree
Showing 19 changed files with 791 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ task('build', {async: true}, function () {
'./src/graph/Groups.js',
'./src/graph/Images.js',
'./src/graph/graphMixins/physics/PhysicsMixin.js',
'./src/graph/graphMixins/physics/HierarchialRepulsion.js',
'./src/graph/graphMixins/physics/BarnesHut.js',
'./src/graph/graphMixins/physics/Repulsion.js',
'./src/graph/graphMixins/HierarchicalLayoutMixin.js',
'./src/graph/graphMixins/ManipulationMixin.js',
'./src/graph/graphMixins/SectorsMixin.js',
'./src/graph/graphMixins/ClusterMixin.js',
Expand Down
84 changes: 74 additions & 10 deletions docs/graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ <h2><a name="Contents"></a>Contents</h2>
<li><a href="#Clustering">Clustering</a></li>
<li><a href="#Navigation_controls">Navigation controls</a></li>
<li><a href="#Keyboard_navigation">Keyboard navigation</a></li>
<li><a href="#Hierarchical_layout">Hierarchical layout</a></li>
</ul>
</li>
<li><a href="#Methods">Methods</a></li>
Expand Down Expand Up @@ -291,10 +292,10 @@ <h3 id="Nodes">Nodes</h3>
</tr>

<tr>
<td>fixed</td>
<td>allowedToMove</td>
<td>Boolean</td>
<td>false</td>
<td>If fixed is true, then the node will not move from its supplied position.
<td>true</td>
<td>If allowedToMove is false, then the node will not move from its supplied position.
If only an x position has been supplied, it is only fixed in the x-direction.
The same holds for y. If both x and y have been defined, the node will not move.</td>
</tr>
Expand Down Expand Up @@ -334,8 +335,15 @@ <h3 id="Nodes">Nodes</h3>
<td>string</td>
<td>no</td>
<td>Url of an image. Only applicable when the shape of the node is
<code>image</code>.</td>
</tr>
<code>image</code>.</td>
</tr>

<tr>
<td>level</td>
<td>number</td>
<td>-1</td>
<td>This level is used in the hierarchical layout. If this is not selected, the level does not do anything.</td>
</tr>

<tr>
<td>radius</td>
Expand Down Expand Up @@ -836,10 +844,10 @@ <h3 id="Nodes_configuration">Nodes configuration</h3>
<td>Default border color of the node when selected.</td>
</tr>
<tr>
<td>fixed</td>
<td>allowedToMove</td>
<td>Boolean</td>
<td>false</td>
<td>If fixed is true, then the node will not move from its supplied position.
<td>If allowedToMove is false, then the node will not move from its supplied position.
If only an x position has been supplied, it is only fixed in the x-direction.
The same holds for y. If both x and y have been defined, the node will not move.</td>
</tr>
Expand Down Expand Up @@ -875,6 +883,12 @@ <h3 id="Nodes_configuration">Nodes configuration</h3>
<td>Default image url for the nodes. only applicable to shape <code>image</code>.</td>
</tr>

<tr>
<td>level</td>
<td>number</td>
<td>-1</td>
<td>This level is used in the hierarchical layout. If this is not selected, the level does not do anything.</td>
</tr>
<tr>
<td>widthMin</td>
<td>Number</td>
Expand Down Expand Up @@ -1329,13 +1343,11 @@ <h4 id="Data_manipulation_custom">Data manipulation: custom functionality</h4>
var options: {
dataManipulation: true,
onAdd: function(data,callback) {
// fixed must be false because we define a set x and y position.
// If fixed is not false, the node cannot move.
/** data = {id: random unique id,
* label: new,
* x: x position of click (canvas space),
* y: y position of click (canvas space),
* fixed: false
* allowedToMove: true
* };
*/
var newData = {..}; // alter the data as you want.
Expand Down Expand Up @@ -1658,6 +1670,58 @@ <h3 id="Keyboard_navigation">Keyboard navigation</h3>
</tr>
</table>


<h3 id="Hierarchical_layout">Hierarchical layout</h3>
<p>
The graph can be used to display nodes in a hierarchical way. This can be determined automatically, based on the amount of edges connected to each node, or defined by the user.
If the user wants to manually determine the hierarchy, each node has to be supplied with a level (from 0 being heighest to n). The automatic method
is shown in <a href="../examples/graph/23_hierarchical_layout.html">example 23</a> and the user-defined method is shown in <a href="../examples/graph/24_hierarchical_layout_userdefined.html">example 24</a>.
</p>

<pre class="prettyprint">
// simple use of the hierarchical layout
var options: {
hierarchicalLayout: true
}

// advanced configuration for keyboard controls
var options: {
hierarchicalLayout: {
enabled:false,
levelSeparation: 150,
nodeSpacing: 100
}
}
</pre>

<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>

<tr>
<td>enabled</td>
<td>Boolean</td>
<td>false</td>
<td>Enable or disable the hierarchical layout.
</td>
</tr>
<tr>
<td>levelSeparation</td>
<td>Number</td>
<td>150</td>
<td>This defines the space between levels (in the Y-direction).</td>
</tr>
<tr>
<td>nodeSpacing</td>
<td>Number</td>
<td>100</td>
<td>This defines the space between nodes in the same level (in the X-direction).</td>
</tr>
</table>
<h2 id="Methods">Methods</h2>
<p>
Graph supports the following methods.
Expand Down
10 changes: 5 additions & 5 deletions examples/graph/17_network_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@
var x = - mygraph.clientWidth / 2 + 50;
var y = - mygraph.clientHeight / 2 + 50;
var step = 70;
nodes.push({id: 1000, x: x, y: y, label: 'Internet', group: 'internet', value: 1, fixed:true});
nodes.push({id: 1001, x: x, y: y + step, label: 'Switch', group: 'switch', value: 1, fixed:true});
nodes.push({id: 1002, x: x, y: y + 2 * step, label: 'Server', group: 'server', value: 1, fixed:true});
nodes.push({id: 1003, x: x, y: y + 3 * step, label: 'Computer', group: 'desktop', value: 1, fixed:true});
nodes.push({id: 1004, x: x, y: y + 4 * step, label: 'Smartphone', group: 'mobile', value: 1, fixed:true});
nodes.push({id: 1000, x: x, y: y, label: 'Internet', group: 'internet', value: 1});
nodes.push({id: 1001, x: x, y: y + step, label: 'Switch', group: 'switch', value: 1});
nodes.push({id: 1002, x: x, y: y + 2 * step, label: 'Server', group: 'server', value: 1});
nodes.push({id: 1003, x: x, y: y + 3 * step, label: 'Computer', group: 'desktop', value: 1});
nodes.push({id: 1004, x: x, y: y + 4 * step, label: 'Smartphone', group: 'mobile', value: 1});

// create a graph
var container = document.getElementById('mygraph');
Expand Down
2 changes: 1 addition & 1 deletion examples/graph/19_scale_free_graph_clustering.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<body onload="draw();">
<h2>Clustering - Scale-Free-Graph</h2>
<div style="width:700px; font-size:14px;">
This example shows therandomly generated <b>scale-free-graph</b> set of nodes and connected edges from example 2.
This example shows the randomly generated <b>scale-free-graph</b> set of nodes and connected edges from example 2.
By clicking the checkbox you can turn clustering on and off. If you increase the number of nodes to
a value higher than 100, automatic clustering is used before the initial draw (assuming the checkbox is checked).
<br />
Expand Down
113 changes: 113 additions & 0 deletions examples/graph/23_hierarchical_layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!doctype html>
<html>
<head>
<title>Graph | Random nodes</title>

<style type="text/css">
body {
font: 10pt sans;
}
#mygraph {
width: 600px;
height: 600px;
border: 1px solid lightgray;
}
</style>

<script type="text/javascript" src="../../dist/vis.js"></script>

<script type="text/javascript">
var nodes = null;
var edges = null;
var graph = null;

function draw() {
nodes = [];
edges = [];
var connectionCount = [];

// randomly create some nodes and edges
var nodeCount = document.getElementById('nodeCount').value;
for (var i = 0; i < nodeCount; i++) {
nodes.push({
id: i,
label: String(i)
});

connectionCount[i] = 0;

// create edges in a scale-free-graph way
if (i == 1) {
var from = i;
var to = 0;
edges.push({
from: from,
to: to
});
connectionCount[from]++;
connectionCount[to]++;
}
else if (i > 1) {
var conn = edges.length * 2;
var rand = Math.floor(Math.random() * conn);
var cum = 0;
var j = 0;
while (j < connectionCount.length && cum < rand) {
cum += connectionCount[j];
j++;
}


var from = i;
var to = j;
edges.push({
from: from,
to: to
});
connectionCount[from]++;
connectionCount[to]++;
}
}

// create a graph
var container = document.getElementById('mygraph');
var data = {
nodes: nodes,
edges: edges
};

var options = {
edges: {
},
stabilize: false,
hierarchicalLayout:true
};
graph = new vis.Graph(container, data, options);

// add event listeners
graph.on('select', function(params) {
document.getElementById('selection').innerHTML = 'Selection: ' + params.nodes;
});
}
</script>
</head>

<body onload="draw();">
<h2>Hierarchical Layout - Scale-Free-Graph</h2>
<div style="width:700px; font-size:14px;">
This example shows the randomly generated <b>scale-free-graph</b> set of nodes and connected edges from example 2.
In this example, hierarchical layout has been enabled and the vertical levels are determine automatically.
</div>
<br />
<form onsubmit="draw(); return false;">
<label for="nodeCount">Number of nodes:</label>
<input id="nodeCount" type="text" value="25" style="width: 50px;">
<input type="submit" value="Go">
</form>
<br>

<div id="mygraph"></div>

<p id="selection"></p>
</body>
</html>
Loading

0 comments on commit 8a3c45e

Please sign in to comment.