DEMOS
DOCS
FEATURES
DOWNLOAD
PURCHASE
CONTACT
BLOG
Show:

Graph Class

A Graph. Contains a list of Vertex objects, each of which has 0..N Ports, and a list of Edge objects. Every instance of the jsPlumb Toolkit is backed by an instance of this class.

A good discussion on Graphs can be found on Wikipedia: http://en.wikipedia.org/wiki/Graph_(mathematics).

You will not typically need to construct a Graph directly (at least not when using the Toolkit; there is nothing stopping you from using this class directly for other purposes).

Constructor

Graph

(
  • [params]
)

Parameters:

  • [params] Object optional

    Constructor parameters. Optional. The Graph class has sensible defaults.

    • [defaultDirected=true] Boolean optional

      Whether edges are directed by default.

    • [idFunction] Function optional

      Function to use to extract an appropriate ID from the JSON for a give node. Defaults to returning the 'id' property of the JSON.

    • [enableSubgraphs=false] Boolean optional

      If true, Nodes are Graphs themselves, and can have child Nodes. If you enable this then you cannot use slashes (/) in your Node ids, as they will be treated as components of a path to a Node in a nested Graph.

    • [defaultCost=1] Number optional

      Default cost for Edges.

    • [portSeparator="."] String optional

      The character(s) used to separate ports from nodes in port ids.

Methods

addEdge

(
  • params
)
Edge

Adds an Edge to the Graph.

Parameters:

  • params Object

    Parameters for new Edge.

    • source String | Node | Port

      Source for the Edge - a Node, Port or Node/Port id.

    • target String | Node | Port

      Target for the Edge - a Node, Port or Node/Port id.

    • [cost=1] Integer optional

      Edge cost. This is used when computing shortest paths through the graph. If an Edge is not directed, then the same cost is applied regardless of the direction of traversal.

    • [directed=true] Boolean optional

      Whether or not the Edge is directed.

    • [data] Object optional

      Optional data to associate with the Edge. The default edgeIdFunction

Returns:

Edge:

The Edge that was added.

addNode

(
  • data
  • [idFunction]
  • The
)

Adds a Node to the Graph

Parameters:

  • data Object

    Backing data for the Node

  • [idFunction] Function optional

    Optional function to use to retrieve ID from backing data. Defaults to retrieving id from data object.

  • The Node

    Node that was added.

addNodes

(
  • data
  • [idFunction]
)

Adds a list of Nodes to the Graph

Parameters:

  • data Object

    List of data objects, one for each Node to be added.

  • [idFunction] Function optional

    Optional function to use to retrieve ID from backing data. Defaults to retrieving id from data object.

clear

()

Clears the Graph of all its Nodes, Ports and Edges.

deleteEdge

(
  • edge
)

Deletes an Edge.

Parameters:

  • edge Edge | String

    Edge, or ID of Edge, to delete.

deleteGroup

(
  • g
  • [removeMembers=false]
)

Deletes a Group

Parameters:

  • g Node | String

    Either a Group, or a Group id.

  • [removeMembers=false] Boolean optional

    If true, also remove all the Nodes that are members of the Group.

deleteNode

(
  • node
)

Deletes a Node

Parameters:

  • node Node | String

    Either a Node, or a Node id.

findPath

(
  • source
  • target
  • [strict=true]
  • [nodeFilter]
  • [edgeFilter]
)
Vertex, cost, edge \

Finds the shortest path from source to target, using the Djikstra algorithm.

Parameters:

  • source Node | String

    Source Node or Node ID.

  • target Node | String

    Target Node or Node ID.

  • [strict=true] Boolean optional

    Sets whether or not paths are searched strictly by the given source/target. If, for instance, you supply a node as the source, but there are only edges connected to ports on that node, by default these edges will be ignored. Switching strict to false will mean these edges are considered.

  • [nodeFilter] Function optional

    Optional function that is given each Node's backing data and asked to return true or false - true means include the Node, false means exclude it.

  • [edgeFilter] Function optional

    Optional function that is given each Edge's backing data and asked to return true or false - true means include the Edge, false means exclude it.

Returns:

Vertex, cost, edge \:

An array like [ \, \{ vertex,cost,edge \} ... ] when successful; when unsuccessful the three compiled tables are returned - distances to nodes, each node's previous node, and the associated edge. so you can call this method with no target set and get the entire table populated.

getBetweenness

(
  • node
)
Float

Returns the betweenness centrality of the given node.

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve betweenness centrality for.

Returns:

Float:

Node's "betweenness" centrality.

getCentrality

(
  • node
)
Integer

Returns the degree centrality of the given node. This is an alias to getDegreeCentrality, as centrality most commonly refers to degree centrality. Note that this returns incoming and outgoing connections; use getIndegreeCentrality or getOutdegreeCentrality if you need to be more specific.

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve centrality for.

Returns:

Integer:

Node's centrality.

getCloseness

(
  • node
)
Float

Returns the Closeness centrality of the given node. This is the inverse of the node's farness.

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve closeness for.

Returns:

Float:

Node's "closeness".

getDiameter

(
  • [dontUseMax=false]
)
Number

Returns the diameter of the Graph.

Parameters:

  • [dontUseMax=false] Boolean optional

    Whether or not to return Infinity if there is at least one pair of nodes for which there is no available path.

Returns:

Number:

Diameter of the Graph.

getDistance

(
  • source
  • target
  • [strict=true]
)
Number

Finds the distance between source and target.

Parameters:

  • source Node | String

    Source Node or Node ID.

  • target Node | String

    Target Node or Node ID.

  • [strict=true] Boolean optional

    Sets whether or not paths are searched strictly by the given source/target. If, for instance, you supply a node as the source, but there are only edges connected to ports on that node, by default these edges will be ignored. Switching strict to false will mean these edges are considered.

Returns:

Number:

Distance from the source to the target.

getEdge

(
  • e
)
Edge

Gets an Edge by id, or if the given object is already an Edge, hands that back.

Parameters:

  • e String | Edge | Object

    ID of the Edge to retrieve, or an actual Edge, or some data from which an ID could be derived.

Returns:

Edge:

The requested Edge, if found, otherwise null.

getEdgeCount

() Integer

Returns the total number of Edges in the graph.

Returns:

Integer:

The total number of Edges.

getFarness

(
  • node
)
Number

Returns the farness centrality of the given node, ie. the sum of its distance from all other nodes, where the distance from one Node to another is given by the associated cost of the Edge joining the two Nodes.

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve farness for.

Returns:

Number:

Node's "farness".

getGroupAt

(
  • idx
)
Group

Returns the Group at the given index.

Parameters:

  • idx Number

    Index into group list

Returns:

Group:

a Group, or null if not found.

getGroups

() Group[]

Gets the Groups in the graph.

Returns:

Group[]:

All the groups in the Graph.

getIdFunction

() Function

Gets the current function to use to extract an appropriate ID from the JSON for any given object.

Returns:

Function:

Function in use for extracting IDs.

getIndegreeCentrality

(
  • node
)
Integer

Returns the indegree centrality of the given node (number of connections entering the vertex)

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve indegree centrality for.

Returns:

Integer:

Node's indegree centrality.

getNode

(
  • id
)
Node

Gets the Node or Port with the given id, null if not found.

Parameters:

  • id String

    Node or Port id.

Returns:

Node:

Node/Port if found, null otherwise.

getNodeAt

(
  • index
)
Node

Returns the Node at the given index (used for bulk init type purposes)

Parameters:

  • index Integer

    Index of the Node to retrieve

Returns:

Node:

Node at the given index.

getNodeCount

() Integer

Gets the count of Nodes in the Graph.

Returns:

Integer:

The total number of Nodes in the graph.

getNodes

() Node[]

Gets all the Nodes in the Graph.

Returns:

Node[]:

All the Nodes in the Graph.

getOutdegreeCentrality

(
  • node
)
Integer

Returns the outdegree centrality of the given node (number of connections exiting the vertex)

Parameters:

  • node Node | String

    Node, or Node ID, to retrieve outdegree centrality for.

Returns:

Integer:

Node's indegree centrality.

getVertexByPortId Looks up a vertex identified by the port id, which is in full port id (vertexId.portId) format, returning null if nothing found. (Note that we show full port id format as vertexId.portId, but the '.' is just the default portSeparator; it is possible to change that to some other character)

(
  • portId
)

Parameters:

  • portId Object

Returns:

:

printPath

() String

Returns the path from source to target as a String.

Returns:

String:

Printed path. Mostly useful for debugging.

setIdFunction

(
  • f
)

Sets the default function to use to extract an appropriate type from the JSON for any given object.

Parameters:

  • f Function

    Function to use to extract IDs.

setIdFunction

(
  • idFunction
)

Sets the default function to use to extract an appropriate ID from the JSON for any given object.

Parameters:

  • idFunction Function

    Function to use to extract IDs.

setPortSeparator

(
  • separator
)

Sets the character(s) used to separate ports from nodes in port ids. By default this is '.', ie a port is addressed as nodeId.portId. This may need to be changed depending on the data in your model.

Parameters:

  • separator String

    Separator to use.

setSource

(
  • o
)

Sets the source Node/Port for some Edge.

Parameters:

  • o Node | Port | String

    Node/Port/id for new Edge source

setTarget

(
  • o
)

Sets the target Node/Port for some Edge.

Parameters:

  • o Node | Port | String

    Node/Port/id for new Edge target