Graph.js

Generic Graph, Graph.Node and Graph.Adjacence classes.

Used by

Hypertree, RGraph and ST.

Summary
Graph.jsGeneric Graph, Graph.Node and Graph.Adjacence classes.
GraphA generic Graph class.
Functions
getNodeReturns a Graph.Node by id.
getAdjacenceReturns an array of Graph.Adjacence objects connecting nodes with ids id and id2.
addNodeAdds a node.
addAdjacenceConnects nodes specified by obj and obj2.
removeNodeRemoves a Graph.Node matching the specified id.
removeAdjacenceRemoves a Graph.Adjacence matching id1 and id2.
hasNodeReturns a Boolean instance indicating if the node belongs to the Graph or not.
Graph.NodeA Graph node.
Functions
adjacentToIndicates if the node is adjacent to the node specified by id
getAdjacencyReturns a Graph.Adjacence object connecting the current Graph.Node and the node having id as id.
addAdjacencyConnects the current node and the given node.
removeAdjacencyRemoves a Graph.Adjacence by id.
Graph.AdjacenceA Graph adjacence (or edge).
Graph.UtilGraph traversal and processing utility object.
Functions
getNodeReturns a Graph.Node by id.
eachNodeIterates over Graph nodes performing an action.
eachAdjacencyIterates over Graph.Node adjacencies applying the action function.
computeLevelsPerforms a BFS traversal setting the correct depth for each node.
eachBFSPerforms a BFS traversal applying action to each Graph.Node.
eachLevelIterates over a node’s subgraph applying action to the nodes of relative depth between levelBegin and levelEnd.
eachSubgraphIterates over a node’s children recursively.
eachSubnodeIterates over a node’s children (without deeper recursion).
anySubnodeReturns true if any subnode matches the given condition.
getSubnodesCollects all subnodes for a specified node.
getParentsReturns an Array of Graph.Nodes wich are parents of the given node.
isDescendantOfReturns a Boolean instance indicating if some node is descendant of the node with the given id.
cleanCleans flags from nodes (by setting the flag property to false).

Graph

A generic Graph class.

Description

When a json graph/tree structure is loaded by Loader.loadJSON, an internal Graph representation is created.

In most cases you’ll be dealing with an already created Graph structure, so methods like Graph.addNode or Graph.addAdjacence won’t be of many use.  However methods like Graph.getNode and Graph.hasNode are pretty useful.

Graph.Util provides also iterators for Graphs and advanced and useful graph operations and methods.

Used by

Loader.loadJSON, Hypertree, RGraph and ST.

Access

An instance of this class can be accessed by using the graph parameter of a Hypertree, RGraph or ST instance

Example

var st = new ST(canvas, config);
st.graph.getNode //or any other <Graph> method.

var ht = new Hypertree(canvas, config);
ht.graph.getNode //or any other <Graph> method.

var rg = new RGraph(canvas, config);
rg.graph.getNode //or any other <Graph> method.
Summary
Functions
getNodeReturns a Graph.Node by id.
getAdjacenceReturns an array of Graph.Adjacence objects connecting nodes with ids id and id2.
addNodeAdds a node.
addAdjacenceConnects nodes specified by obj and obj2.
removeNodeRemoves a Graph.Node matching the specified id.
removeAdjacenceRemoves a Graph.Adjacence matching id1 and id2.
hasNodeReturns a Boolean instance indicating if the node belongs to the Graph or not.

Functions

getNode

getNode: function(id)

Returns a Graph.Node by id.

Parameters

idA Graph.Node id.

Returns

A Graph.Node having id as id.  Returns false otherwise.

Example

var node = graph.getNode('someid');

getAdjacence

getAdjacence: function (id,
id2)

Returns an array of Graph.Adjacence objects connecting nodes with ids id and id2.

Parameters

idA Graph.Node id.
id2A Graph.Node id.

Returns

An Array of Graph.Adjacence objects.  Returns false if there’s not a Graph.Adjacence connecting those two nodes.

addNode

addNode: function(obj)

Adds a node.

Parameters

objAn object containing as properties
  • id node’s id
  • name node’s name
  • data node’s data hash

See also

Graph.Node

addAdjacence

addAdjacence: function (obj,
obj2,
data)

Connects nodes specified by obj and obj2.  If not found, nodes are created.

Parameters

obja Graph.Node object.
obj2Another Graph.Node object.
dataA DataSet object.  Used to store some extra information in the Graph.Adjacence object created.

See also

Graph.Node, Graph.Adjacence

removeNode

removeNode: function(id)

Removes a Graph.Node matching the specified id.

Parameters

idA node’s id.

removeAdjacence

removeAdjacence: function(id1,
id2)

Removes a Graph.Adjacence matching id1 and id2.

Parameters

id1A Graph.Node id.
id2A Graph.Node id.

hasNode

hasNode: function(id)

Returns a Boolean instance indicating if the node belongs to the Graph or not.

Parameters

idNode id.

Returns

A Boolean instance indicating if the node belongs to the graph or not.

Graph.Node

A Graph node.

Parameters

objAn object containing an ‘id’, ‘name’ and ‘data’ properties as described in Graph.addNode.
complexWhether node position properties should contain Complex or Polar instances.

See also

Graph

Description

An instance of Graph.Node is usually passed as parameter for most configuration/controller methods in the Hypertree, RGraph and ST classes.

A Graph.Node object has as properties

idNode id.
nameNode name.
dataNode data property containing a hash (i.e {}) with custom options.  For more information see Loader.loadJSON.
selectedWhether the node is selected or not.  Used by ST for selecting nodes that are between the root node and the selected node.
angleSpanFor radial layouts such as the ones performed by the Hypertree and the RGraph.  Contains begin and end properties containing angle values describing the angle span for this subtree.
alphaCurrent opacity value.
startAlphaOpacity begin value.  Used for interpolation.
endAlphaOpacity end value.  Used for interpolation.
posCurrent position.  Can be a Complex or Polar instance.
startPosStarting position.  Used for interpolation.
endPosEnding position.  Used for interpolation.
Summary
Functions
adjacentToIndicates if the node is adjacent to the node specified by id
getAdjacencyReturns a Graph.Adjacence object connecting the current Graph.Node and the node having id as id.
addAdjacencyConnects the current node and the given node.
removeAdjacencyRemoves a Graph.Adjacence by id.

Functions

adjacentTo

adjacentTo: function(node)

Indicates if the node is adjacent to the node specified by id

Parameters

idA node id.

Returns

A Boolean instance indicating whether this node is adjacent to the specified by id or not.

Example

node.adjacentTo('mynodeid');

getAdjacency

getAdjacency: function(id)

Returns a Graph.Adjacence object connecting the current Graph.Node and the node having id as id.

Parameters

idA node id.

Returns

A Graph.Adjacence object or undefined.

addAdjacency

addAdjacency: function(node,
data)

Connects the current node and the given node.

Parameters

nodeA Graph.Node.
dataSome custom hash information.

removeAdjacency

removeAdjacency: function(id)

Removes a Graph.Adjacence by id.

Parameters

idA node id.

Graph.Adjacence

A Graph adjacence (or edge).  Connects two Graph.Nodes.

Parameters

nodeFromA Graph.Node.
nodeToA Graph.Node.
dataSome custom hash data.

See also

Graph

Description

An instance of Graph.Adjacence is usually passed as parameter for some configuration/controller methods in the Hypertree, RGraph and ST classes.

A Graph.Adjacence object has as properties

nodeFromA Graph.Node connected by this edge.
nodeToAnother Graph.Node connected by this edge.
dataNode data property containing a hash (i.e {}) with custom options.  For more information see Loader.loadJSON.
alphaCurrent opacity value.
startAlphaOpacity begin value.  Used for interpolation.
endAlphaOpacity end value.  Used for interpolation.

Graph.Util

Graph traversal and processing utility object.

Summary
Functions
getNodeReturns a Graph.Node by id.
eachNodeIterates over Graph nodes performing an action.
eachAdjacencyIterates over Graph.Node adjacencies applying the action function.
computeLevelsPerforms a BFS traversal setting the correct depth for each node.
eachBFSPerforms a BFS traversal applying action to each Graph.Node.
eachLevelIterates over a node’s subgraph applying action to the nodes of relative depth between levelBegin and levelEnd.
eachSubgraphIterates over a node’s children recursively.
eachSubnodeIterates over a node’s children (without deeper recursion).
anySubnodeReturns true if any subnode matches the given condition.
getSubnodesCollects all subnodes for a specified node.
getParentsReturns an Array of Graph.Nodes wich are parents of the given node.
isDescendantOfReturns a Boolean instance indicating if some node is descendant of the node with the given id.
cleanCleans flags from nodes (by setting the flag property to false).

Functions

getNode

getNode: function(graph,
id)

Returns a Graph.Node by id.

Parameters

graphA Graph instance.
idA Graph.Node id.

Returns

A Graph node.

Example

Graph.Util.getNode(graph, 'nodeid');

eachNode

eachNode: function(graph,
action,
flags)

Iterates over Graph nodes performing an action.

Parameters

graphA Graph instance.
actionA callback function having a Graph.Node as first formal parameter.

Example

Graph.Util.each(graph, function(node) {
 alert(node.name);
});

eachAdjacency

eachAdjacency: function(node,
action,
flags)

Iterates over Graph.Node adjacencies applying the action function.

Parameters

nodeA Graph.Node.
actionA callback function having Graph.Adjacence as first formal parameter.

Example

Graph.Util.eachAdjacency(node, function(adj) {
 alert(adj.nodeTo.name);
});

computeLevels

computeLevels: function(graph,
id,
startDepth,
flags)

Performs a BFS traversal setting the correct depth for each node.

The depth of each node can then be accessed by

node._depth

Parameters

graphA Graph.
idA starting node id for the BFS traversal.
startDepthoptional A minimum depth value.  Default’s 0.

eachBFS

eachBFS: function(graph,
id,
action,
flags)

Performs a BFS traversal applying action to each Graph.Node.

Parameters

graphA Graph.
idA starting node id for the BFS traversal.
actionA callback function having a Graph.Node as first formal parameter.

Example

Graph.Util.eachBFS(graph, 'mynodeid', function(node) {
 alert(node.name);
});

eachLevel

eachLevel: function(node,
levelBegin,
levelEnd,
action,
flags)

Iterates over a node’s subgraph applying action to the nodes of relative depth between levelBegin and levelEnd.

Parameters

nodeA Graph.Node.
levelBeginA relative level value.
levelEndA relative level value.
actionA callback function having a Graph.Node as first formal parameter.

eachSubgraph

eachSubgraph: function(node,
action,
flags)

Iterates over a node’s children recursively.

Parameters

nodeA Graph.Node.
actionA callback function having a Graph.Node as first formal parameter.

Example

Graph.Util.eachSubgraph(node, function(node) {
 alert(node.name);
});

eachSubnode

eachSubnode: function(node,
action,
flags)

Iterates over a node’s children (without deeper recursion).

Parameters

nodeA Graph.Node.
actionA callback function having a Graph.Node as first formal parameter.

Example

Graph.Util.eachSubnode(node, function(node) {
 alert(node.name);
});

anySubnode

anySubnode: function(node,
cond,
flags)

Returns true if any subnode matches the given condition.

Parameters

nodeA Graph.Node.
condA callback function returning a Boolean instance.  This function has as first formal parameter a Graph.Node.

Returns

A boolean value.

Example

Graph.Util.anySubnode(node, function(node) { return node.name == "mynodename"; });

getSubnodes

getSubnodes: function(node,
level,
flags)

Collects all subnodes for a specified node.  The level parameter filters nodes having relative depth of level from the root node.

Parameters

nodeA Graph.Node.
leveloptional A starting relative depth for collecting nodes.  Default’s 0.

Returns

An array of nodes.

getParents

getParents: function(node)

Returns an Array of Graph.Nodes wich are parents of the given node.

Parameters

nodeA Graph.Node.

Returns

An Array of Graph.Nodes.

Example

var pars = Graph.Util.getParents(node);
if(pars.length > 0) {
  //do stuff with parents
}

isDescendantOf

isDescendantOf: function(node,
id)

Returns a Boolean instance indicating if some node is descendant of the node with the given id.

Parameters

nodeA Graph.Node.
idA Graph.Node id.

Returns

Ture if node is descendant of the node with the given id.  False otherwise.

Example

var pars = Graph.Util.isDescendantOf(node, "nodeid");

clean

clean: function(graph)

Cleans flags from nodes (by setting the flag property to false).

Parameters

graphA Graph instance.
A generic Graph class.
A Graph node.
A Graph adjacence (or edge).
getNode: function(id)
Returns a Graph.Node by id.
getAdjacence: function (id,
id2)
Returns an array of Graph.Adjacence objects connecting nodes with ids id and id2.
addNode: function(obj)
Adds a node.
addAdjacence: function (obj,
obj2,
data)
Connects nodes specified by obj and obj2.
removeNode: function(id)
Removes a Graph.Node matching the specified id.
removeAdjacence: function(id1,
id2)
Removes a Graph.Adjacence matching id1 and id2.
hasNode: function(id)
Returns a Boolean instance indicating if the node belongs to the Graph or not.
adjacentTo: function(node)
Indicates if the node is adjacent to the node specified by id
getAdjacency: function(id)
Returns a Graph.Adjacence object connecting the current Graph.Node and the node having id as id.
addAdjacency: function(node,
data)
Connects the current node and the given node.
removeAdjacency: function(id)
Removes a Graph.Adjacence by id.
getNode: function(graph,
id)
Returns a Graph.Node by id.
eachNode: function(graph,
action,
flags)
Iterates over Graph nodes performing an action.
eachAdjacency: function(node,
action,
flags)
Iterates over Graph.Node adjacencies applying the action function.
computeLevels: function(graph,
id,
startDepth,
flags)
Performs a BFS traversal setting the correct depth for each node.
eachBFS: function(graph,
id,
action,
flags)
Performs a BFS traversal applying action to each Graph.Node.
eachLevel: function(node,
levelBegin,
levelEnd,
action,
flags)
Iterates over a node’s subgraph applying action to the nodes of relative depth between levelBegin and levelEnd.
eachSubgraph: function(node,
action,
flags)
Iterates over a node’s children recursively.
eachSubnode: function(node,
action,
flags)
Iterates over a node’s children (without deeper recursion).
anySubnode: function(node,
cond,
flags)
Returns true if any subnode matches the given condition.
getSubnodes: function(node,
level,
flags)
Collects all subnodes for a specified node.
getParents: function(node)
Returns an Array of Graph.Nodes wich are parents of the given node.
isDescendantOf: function(node,
id)
Returns a Boolean instance indicating if some node is descendant of the node with the given id.
clean: function(graph)
Cleans flags from nodes (by setting the flag property to false).
The main Hypertree class
The main RGraph class
The main ST class
loadJSON: function(json,
i)
Loads a JSON structure to the visualization.
Graph traversal and processing utility object.
A multi-purpose Complex Class with common methods.
A multi purpose polar representation.
Close