Graph.js

Summary
Graph.js
GraphA Graph Class that provides useful manipulation functions.
Functions
getNodeReturns a Graph.Node by id.
getAn alias for Graph.Util.getNode.
getByNameReturns a Graph.Node by name.
getAdjacenceReturns a Graph.Adjacence object 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 indicating if the node belongs to the Graph or not.
emptyEmpties the Graph
AccessorsDefines a set of methods for data, canvas and label styles manipulation implemented by Graph.Node and Graph.Adjacence instances.
Functions
getDataReturns the specified data value property.
setDataSets the current data property with some specific value.
setDatasetConvenience method to set multiple data values at once.
removeDataRemove data properties.
getCanvasStyleReturns the specified canvas style data value property.
setCanvasStyleSets the canvas style data property with some specific value.
setCanvasStylesConvenience method to set multiple styles at once.
removeCanvasStyleRemove canvas style properties from data.
getLabelDataReturns the specified label data value property.
setLabelDataSets the current label data with some specific value.
setLabelDatasetConvenience function to set multiple label data at once.
removeLabelDataRemove label properties from data.
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.
getPosReturns the position of the node.
setPosSets the node’s position.
Graph.AdjacenceA Graph adjacence (or edge) connecting two Graph.Nodes.
Graph.UtilGraph traversal and processing utility object.
Functions
getNodeReturns a Graph.Node by id.
eachNodeIterates over Graph nodes performing an action.
eachIterates 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 which are parents of the given node.
isDescendantOfReturns a boolean indicating if some node is descendant of the node with the given id.
cleanCleans flags from nodes.
getClosestNodeToOriginReturns the closest node to the center of canvas.
getClosestNodeToPosReturns the closest node to the given position.

Graph

A Graph Class that provides useful manipulation functions.  You can find more manipulation methods in the Graph.Util object.

An instance of this class can be accessed by using the graph parameter of any tree or graph visualization.

Example

//create new visualization
var viz = new $jit.Viz(options);
//load JSON data
viz.loadJSON(json);
//access model
viz.graph; //<Graph> instance

Implements

The following Graph.Util methods are implemented in Graph

Summary
Functions
getNodeReturns a Graph.Node by id.
getAn alias for Graph.Util.getNode.
getByNameReturns a Graph.Node by name.
getAdjacenceReturns a Graph.Adjacence object 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 indicating if the node belongs to the Graph or not.
emptyEmpties the Graph

Functions

getNode

getNode: function(id)

Returns a Graph.Node by id.

Parameters

id(string) A Graph.Node id.

Example

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

get

get: function(id)

An alias for Graph.Util.getNode.  Returns a node by id.

Parameters

id(string) A Graph.Node id.

Example

var node = graph.get('nodeId');

getByName

getByName: function(name)

Returns a Graph.Node by name.

Parameters

name(string) A Graph.Node name.

Example

var node = graph.getByName('someName');

getAdjacence

getAdjacence: function (id,
id2)

Returns a Graph.Adjacence object connecting nodes with ids id and id2.

Parameters

id(string) A Graph.Node id.
id2(string) A Graph.Node id.

addNode

addNode: function(obj)

Adds a node.

Parameters

objAn object with the properties described below
id(string) A node id
name(string) A node’s name
data(object) A 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

obj(object) A Graph.Node object.
obj2(object) Another Graph.Node object.
data(object) A data 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

id(string) A node’s id.

removeAdjacence

removeAdjacence: function(id1,
id2)

Removes a Graph.Adjacence matching id1 and id2.

Parameters

id1(string) A Graph.Node id.
id2(string) A Graph.Node id.

hasNode

hasNode: function(id)

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

Parameters

id(string) Node id.

empty

empty: function()

Empties the Graph

Accessors

Defines a set of methods for data, canvas and label styles manipulation implemented by Graph.Node and Graph.Adjacence instances.

Summary
Functions
getDataReturns the specified data value property.
setDataSets the current data property with some specific value.
setDatasetConvenience method to set multiple data values at once.
removeDataRemove data properties.
getCanvasStyleReturns the specified canvas style data value property.
setCanvasStyleSets the canvas style data property with some specific value.
setCanvasStylesConvenience method to set multiple styles at once.
removeCanvasStyleRemove canvas style properties from data.
getLabelDataReturns the specified label data value property.
setLabelDataSets the current label data with some specific value.
setLabelDatasetConvenience function to set multiple label data at once.
removeLabelDataRemove label properties from data.

Functions

getData

getData: function(prop,
type,
force)

Returns the specified data value property.  This is useful for querying special/reserved Graph.Node data properties (i.e dollar prefixed properties).

Parameters

prop(string) The name of the property.  The dollar sign is not needed.  For example getData(width) will return data.$width.
type(string) The type of the data property queried.  Default’s “current”.  You can access start and end data properties also.  These properties are used when making animations.
force(boolean) Whether to obtain the true value of the property (equivalent to data.$prop) or to check for node.overridable = true first.

Returns

The value of the dollar prefixed property or the global Node/Edge property value if overridable=false

Example

node.getData('width'); //will return node.data.$width if Node.overridable=true;

setData

setData: function(prop,
value,
type)

Sets the current data property with some specific value.  This method is only useful for reserved (dollar prefixed) properties.

Parameters

prop(string) The name of the property.  The dollar sign is not necessary.  For example setData(width) will set data.$width.
value(mixed) The value to store.
type(string) The type of the data property to store.  Default’s “current” but can also be “start” or “end”.

Example

node.setData('width', 30);

If we were to make an animation of a node/edge width then we could do

var node = viz.getNode('nodeId');
//set start and end values
node.setData('width', 10, 'start');
node.setData('width', 30, 'end');
//will animate nodes width property
viz.fx.animate({
  modes: ['node-property:width'],
  duration: 1000
});

setDataset

setDataset: function(types,
obj)

Convenience method to set multiple data values at once.

Parameters

types(array|string) A set of ‘current’, ‘end’ or ‘start’ values.
obj(object) A hash containing the names and values of the properties to be altered.

Example

node.setDataset(['current', 'end'], {
  'width': [100, 5],
  'color': ['#fff', '#ccc']
});
//...or also
node.setDataset('end', {
  'width': 5,
  'color': '#ccc'
});

See also

Accessors.setData

removeData

removeData: function()

Remove data properties.

Parameters

One or more property names as arguments.  The dollar sign is not needed.

Example

node.removeData('width'); //now the default width value is returned

getCanvasStyle

getCanvasStyle: function(prop,
type,
force)

Returns the specified canvas style data value property.  This is useful for querying special/reserved Graph.Node canvas style data properties (i.e. dollar prefixed properties that match with $canvas-<name of canvas style>).

Parameters

prop(string) The name of the property.  The dollar sign is not needed.  For example getCanvasStyle(shadowBlur) will return data[$canvas-shadowBlur].
type(string) The type of the data property queried.  Default’s current.  You can access start and end data properties also.

Example

node.getCanvasStyle('shadowBlur');

See also

Accessors.getData

setCanvasStyle

setCanvasStyle: function(prop,
value,
type)

Sets the canvas style data property with some specific value.  This method is only useful for reserved (dollar prefixed) properties.

Parameters

prop(string) Name of the property.  Can be any canvas property like ‘shadowBlur’, ‘shadowColor’, ‘strokeStyle’, etc.
value(mixed) The value to set to the property.
type(string) Default’s current.  Whether to set start, current or end type properties.

Example

node.setCanvasStyle('shadowBlur', 30);

If we were to make an animation of a node/edge shadowBlur canvas style then we could do

var node = viz.getNode('nodeId');
//set start and end values
node.setCanvasStyle('shadowBlur', 10, 'start');
node.setCanvasStyle('shadowBlur', 30, 'end');
//will animate nodes canvas style property for nodes
viz.fx.animate({
  modes: ['node-style:shadowBlur'],
  duration: 1000
});

See also

Accessors.setData.

setCanvasStyles

setCanvasStyles: function(types,
obj)

Convenience method to set multiple styles at once.

Parameters

types(array|string) A set of ‘current’, ‘end’ or ‘start’ values.
obj(object) A hash containing the names and values of the properties to be altered.

See also

Accessors.setDataset.

removeCanvasStyle

removeCanvasStyle: function()

Remove canvas style properties from data.

Parameters

A variable number of canvas style strings.

See also

Accessors.removeData.

getLabelData

getLabelData: function(prop,
type,
force)

Returns the specified label data value property.  This is useful for querying special/reserved Graph.Node label options (i.e. dollar prefixed properties that match with $label-<name of label style>).

Parameters

prop(string) The name of the property.  The dollar sign prefix is not needed.  For example getLabelData(size) will return data[$label-size].
type(string) The type of the data property queried.  Default’s current.  You can access start and end data properties also.

See also

Accessors.getData.

setLabelData

setLabelData: function(prop,
value,
type)

Sets the current label data with some specific value.  This method is only useful for reserved (dollar prefixed) properties.

Parameters

prop(string) Name of the property.  Can be any canvas property like ‘shadowBlur’, ‘shadowColor’, ‘strokeStyle’, etc.
value(mixed) The value to set to the property.
type(string) Default’s current.  Whether to set start, current or end type properties.

Example

node.setLabelData('size', 30);

If we were to make an animation of a node label size then we could do

var node = viz.getNode('nodeId');
//set start and end values
node.setLabelData('size', 10, 'start');
node.setLabelData('size', 30, 'end');
//will animate nodes label size
viz.fx.animate({
  modes: ['label-property:size'],
  duration: 1000
});

See also

Accessors.setData.

setLabelDataset

setLabelDataset: function(types,
obj)

Convenience function to set multiple label data at once.

Parameters

types(array|string) A set of ‘current’, ‘end’ or ‘start’ values.
obj(object) A hash containing the names and values of the properties to be altered.

See also

Accessors.setDataset.

removeLabelData

removeLabelData: function()

Remove label properties from data.

Parameters

A variable number of label property strings.

See also

Accessors.removeData.

Graph.Node

A Graph node.

Implements

Accessors methods.

The following Graph.Util methods are implemented by Graph.Node

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.
getPosReturns the position of the node.
setPosSets the node’s position.

Functions

adjacentTo

adjacentTo: function(node)

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

Parameters

id(string) A node id.

Example

node.adjacentTo('nodeId') == true;

getAdjacency

getAdjacency: function(id)

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

Parameters

id(string) A node id.

getPos

getPos: function(type)

Returns the position of the node.

Parameters

type(string) Default’s current.  Possible values are “start”, “end” or “current”.

Returns

A Complex or Polar instance.

Example

var pos = node.getPos('end');

setPos

setPos: function(value,
type)

Sets the node’s position.

Parameters

value(object) A Complex or Polar instance.
type(string) Default’s current.  Possible values are “start”, “end” or “current”.

Example

node.setPos(new $jit.Complex(0, 0), 'end');

Graph.Adjacence

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

Implements

Accessors methods.

See also

Graph, Graph.Node

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.

Graph.Util

Graph traversal and processing utility object.

Note

For your convenience some of these methods have also been appended to Graph and Graph.Node classes.

Summary
Functions
getNodeReturns a Graph.Node by id.
eachNodeIterates over Graph nodes performing an action.
eachIterates 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 which are parents of the given node.
isDescendantOfReturns a boolean indicating if some node is descendant of the node with the given id.
cleanCleans flags from nodes.
getClosestNodeToOriginReturns the closest node to the center of canvas.
getClosestNodeToPosReturns the closest node to the given position.

Functions

getNode

getNode: function(graph,
id)

Returns a Graph.Node by id.

Also implemented by

Graph

Parameters

graph(object) A Graph instance.
id(string) A Graph.Node id.

Example

$jit.Graph.Util.getNode(graph, 'nodeid');
//or...
graph.getNode('nodeid');

eachNode

eachNode: function(graph,
action,
flags)

Iterates over Graph nodes performing an action.

Also implemented by

Graph.

Parameters

graph(object) A Graph instance.
action(function) A callback function having a Graph.Node as first formal parameter.

Example

$jit.Graph.Util.eachNode(graph, function(node) {
 alert(node.name);
});
//or...
graph.eachNode(function(node) {
  alert(node.name);
});

each

each: function(graph,
action,
flags)

Iterates over Graph nodes performing an action.  It’s an alias for Graph.Util.eachNode.

Also implemented by

Graph.

Parameters

graph(object) A Graph instance.
action(function) A callback function having a Graph.Node as first formal parameter.

Example

$jit.Graph.Util.each(graph, function(node) {
 alert(node.name);
});
//or...
graph.each(function(node) {
  alert(node.name);
});

eachAdjacency

eachAdjacency: function(node,
action,
flags)

Iterates over Graph.Node adjacencies applying the action function.

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
action(function) A callback function having Graph.Adjacence as first formal parameter.

Example

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

computeLevels

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

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

Also implemented by

Graph.

Note

The depth of each node can then be accessed by

node._depth

Parameters

graph(object) A Graph.
id(string) A starting node id for the BFS traversal.
startDepth(optional|number) A minimum depth value.  Default’s 0.

eachBFS

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

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

Also implemented by

Graph.

Parameters

graph(object) A Graph.
id(string) A starting node id for the BFS traversal.
action(function) A callback function having a Graph.Node as first formal parameter.

Example

$jit.Graph.Util.eachBFS(graph, 'mynodeid', function(node) {
 alert(node.name);
});
//or...
graph.eachBFS('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.

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
levelBegin(number) A relative level value.
levelEnd(number) A relative level value.
action(function) A callback function having a Graph.Node as first formal parameter.

eachSubgraph

eachSubgraph: function(node,
action,
flags)

Iterates over a node’s children recursively.

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
action(function) A callback function having a Graph.Node as first formal parameter.

Example

$jit.Graph.Util.eachSubgraph(node, function(node) {
  alert(node.name);
});
//or...
node.eachSubgraph(function(node) {
  alert(node.name);
});

eachSubnode

eachSubnode: function(node,
action,
flags)

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

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
action(function) A callback function having a Graph.Node as first formal parameter.

Example

$jit.Graph.Util.eachSubnode(node, function(node) {
 alert(node.name);
});
//or...
node.eachSubnode(function(node) {
  alert(node.name);
});

anySubnode

anySubnode: function(node,
cond,
flags)

Returns true if any subnode matches the given condition.

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
cond(function) A callback function returning a Boolean instance.  This function has as first formal parameter a Graph.Node.

Example

$jit.Graph.Util.anySubnode(node, function(node) { return node.name == "mynodename"; });
//or...
node.anySubnode(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.

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
level(optional|number) Default’s 0.  A starting relative depth for collecting nodes.

Returns

An array of nodes.

getParents

getParents: function(node)

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

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.

Returns

An Array of Graph.Nodes.

Example

var pars = $jit.Graph.Util.getParents(node);
//or...
var pars = node.getParents();

if(pars.length > 0) {
  //do stuff with parents
}

isDescendantOf

isDescendantOf: function(node,
id)

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

Also implemented by

Graph.Node.

Parameters

node(object) A Graph.Node.
id(string) A Graph.Node id.

Example

$jit.Graph.Util.isDescendantOf(node, "nodeid"); //true|false
//or...
node.isDescendantOf('nodeid');//true|false

clean

clean: function(graph)

Cleans flags from nodes.

Also implemented by

Graph.

Parameters

graphA Graph instance.

getClosestNodeToOrigin

getClosestNodeToOrigin: function(graph,
prop,
flags)

Returns the closest node to the center of canvas.

Also implemented by

Graph.

Parameters

graph(object) A Graph instance.
prop(optional|string) Default’s ‘current’.  A Graph.Node position property.  Possible properties are ‘start’, ‘current’ or ‘end’.

getClosestNodeToPos

getClosestNodeToPos: function(graph,
pos,
prop,
flags)

Returns the closest node to the given position.

Also implemented by

Graph.

Parameters

graph(object) A Graph instance.
pos(object) A Complex or Polar instance.
prop(optional|string) Default’s current.  A Graph.Node position property.  Possible properties are ‘start’, ‘current’ or ‘end’.
getNode: function(id)
Returns a Graph.Node by id.
A Graph node.
get: function(id)
An alias for Graph.Util.getNode.
getNode: function(graph,
id)
Returns a Graph.Node by id.
getByName: function(name)
Returns a Graph.Node by name.
getAdjacence: function (id,
id2)
Returns a Graph.Adjacence object connecting nodes with ids id and id2.
A Graph adjacence (or edge) connecting two Graph.Nodes.
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 indicating if the node belongs to the Graph or not.
A Graph Class that provides useful manipulation functions.
empty: function()
Empties the Graph
getData: function(prop,
type,
force)
Returns the specified data value property.
setData: function(prop,
value,
type)
Sets the current data property with some specific value.
setDataset: function(types,
obj)
Convenience method to set multiple data values at once.
removeData: function()
Remove data properties.
getCanvasStyle: function(prop,
type,
force)
Returns the specified canvas style data value property.
setCanvasStyle: function(prop,
value,
type)
Sets the canvas style data property with some specific value.
setCanvasStyles: function(types,
obj)
Convenience method to set multiple styles at once.
removeCanvasStyle: function()
Remove canvas style properties from data.
getLabelData: function(prop,
type,
force)
Returns the specified label data value property.
setLabelData: function(prop,
value,
type)
Sets the current label data with some specific value.
setLabelDataset: function(types,
obj)
Convenience function to set multiple label data at once.
removeLabelData: function()
Remove label properties from data.
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.
getPos: function(type)
Returns the position of the node.
setPos: function(value,
type)
Sets the node’s position.
eachNode: function(graph,
action,
flags)
Iterates over Graph nodes performing an action.
each: 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 which are parents of the given node.
isDescendantOf: function(node,
id)
Returns a boolean indicating if some node is descendant of the node with the given id.
clean: function(graph)
Cleans flags from nodes.
getClosestNodeToOrigin: function(graph,
prop,
flags)
Returns the closest node to the center of canvas.
getClosestNodeToPos: function(graph,
pos,
prop,
flags)
Returns the closest node to the given position.
Graph traversal and processing utility object.
Defines a set of methods for data, canvas and label styles manipulation implemented by Graph.Node and Graph.Adjacence instances.
A multi-purpose Complex Class with common methods.
A multi purpose polar representation.
Close