Skip to content

Commit

Permalink
Add namespace for graph objects
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpiegza committed Feb 9, 2018
1 parent 978199f commit debb978
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ This is implemented in graph-visualization/src/graph.js.

Usage:

var graph = new Graph({limit: 100}); // set maximum number of nodes, optional
var node1 = new Node(1); // create nodes with id
var node2 = new Node(2);
var graph = new GRAPHVIS.Graph({limit: 100}); // set maximum number of nodes, optional
var node1 = new GRAPHVIS.Node(1); // create nodes with id
var node2 = new GRAPHVIS.Node(2);
graph.addNode( node1 ); // add nodes
graph.addNode( node2 );
graph.addEdge( node1, node2 ); // create edge between nodes
Expand Down
6 changes: 3 additions & 3 deletions examples/simple_graph/simple_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Drawing.SimpleGraph = function(options) {
var camera, controls, scene, renderer, interaction, geometry, object_selection;
var stats;
var info_text = {};
var graph = new Graph({limit: options.limit});
var graph = new GRAPHVIS.Graph({limit: options.limit});

var geometries = [];

Expand Down Expand Up @@ -154,7 +154,7 @@ Drawing.SimpleGraph = function(options) {
*/
function createGraph() {

var node = new Node(0);
var node = new GRAPHVIS.Node(0);
node.data.title = "This is node " + node.id;
graph.addNode(node);
drawNode(node);
Expand All @@ -168,7 +168,7 @@ Drawing.SimpleGraph = function(options) {

var numEdges = randomFromTo(1, that.edges_count);
for(var i=1; i <= numEdges; i++) {
var target_node = new Node(i*steps);
var target_node = new GRAPHVIS.Node(i*steps);
if(graph.addNode(target_node)) {
target_node.data.title = "This is node " + target_node.id;

Expand Down
6 changes: 3 additions & 3 deletions examples/sphere_graph/sphere_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Drawing.SphereGraph = function(options) {
var camera, controls, scene, renderer, interaction, geometry, object_selection;
var stats;
var info_text = {};
var graph = new Graph({limit: options.limit});
var graph = new GRAPHVIS.Graph({limit: options.limit});

var geometries = [];

Expand Down Expand Up @@ -156,7 +156,7 @@ Drawing.SphereGraph = function(options) {
* numNodes and numEdges.
*/
function createGraph() {
var node = new Node(0);
var node = new GRAPHVIS.Node(0);
graph.addNode(node);
drawNode(node);

Expand All @@ -169,7 +169,7 @@ Drawing.SphereGraph = function(options) {

var numEdges = randomFromTo(1, that.edges_count);
for(var i=1; i <= numEdges; i++) {
var target_node = new Node(i*steps);
var target_node = new GRAPHVIS.Node(i*steps);
if(graph.addNode(target_node)) {
drawNode(target_node);
nodes.push(target_node);
Expand Down
32 changes: 17 additions & 15 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
id, position and data.
Example:
node = new Node(1);
node = new GRAPHVIS.Node(1);
node.position.x = 100;
node.position.y = 100;
node.data.title = "Title of the node";
Expand All @@ -23,7 +23,7 @@
Connects to nodes together.
Example:
edge = new Edge(node1, node2);
edge = new GRAPHVIS.Edge(node1, node2);
An edge can also be extended with the data attribute. E.g. set a
type like "friends", different types can then be draw in differnt ways.
Expand All @@ -48,15 +48,17 @@
*/

function Graph(options) {
var GRAPHVIS = GRAPHVIS || {};

GRAPHVIS.Graph = function(options) {
this.options = options || {};
this.nodeSet = {};
this.nodes = [];
this.edges = [];
this.layout = undefined;
}
};

Graph.prototype.addNode = function(node) {
GRAPHVIS.Graph.prototype.addNode = function(node) {
if(this.nodeSet[node.id] === undefined && !this.reached_limit()) {
this.nodeSet[node.id] = node;
this.nodes.push(node);
Expand All @@ -65,44 +67,44 @@ Graph.prototype.addNode = function(node) {
return false;
};

Graph.prototype.getNode = function(node_id) {
GRAPHVIS.Graph.prototype.getNode = function(node_id) {
return this.nodeSet[node_id];
};

Graph.prototype.addEdge = function(source, target) {
GRAPHVIS.Graph.prototype.addEdge = function(source, target) {
if(source.addConnectedTo(target) === true) {
var edge = new Edge(source, target);
var edge = new GRAPHVIS.Edge(source, target);
this.edges.push(edge);
return true;
}
return false;
};

Graph.prototype.reached_limit = function() {
GRAPHVIS.Graph.prototype.reached_limit = function() {
if(this.options.limit !== undefined)
return this.options.limit <= this.nodes.length;
else
return false;
};


function Node(node_id) {
GRAPHVIS.Node = function(node_id) {
this.id = node_id;
this.nodesTo = [];
this.nodesFrom = [];
this.position = {};
this.data = {};
}
};

Node.prototype.addConnectedTo = function(node) {
GRAPHVIS.Node.prototype.addConnectedTo = function(node) {
if(this.connectedTo(node) === false) {
this.nodesTo.push(node);
return true;
}
return false;
};

Node.prototype.connectedTo = function(node) {
GRAPHVIS.Node.prototype.connectedTo = function(node) {
for(var i=0; i < this.nodesTo.length; i++) {
var connectedNode = this.nodesTo[i];
if(connectedNode.id == node.id) {
Expand All @@ -113,8 +115,8 @@ Node.prototype.connectedTo = function(node) {
};


function Edge(source, target) {
GRAPHVIS.Edge = function(source, target) {
this.source = source;
this.target = target;
this.data = {};
}
};

0 comments on commit debb978

Please sign in to comment.