Simplified API for adding edges.
This commit is contained in:
parent
9ac764d634
commit
e38e2202db
11
README.mkdn
11
README.mkdn
|
@ -42,11 +42,16 @@ Basic Usage
|
||||||
See [demo.html](http://dhotson.github.com/springy/demo.html) for the way to
|
See [demo.html](http://dhotson.github.com/springy/demo.html) for the way to
|
||||||
add nodes and edges to graph and springyui.js for the rendering example.
|
add nodes and edges to graph and springyui.js for the rendering example.
|
||||||
|
|
||||||
Starting from Springy 1.1 it is possible to add nodes with simple array
|
Springy 1.1+ supports simplified API for adding nodes and edges, see
|
||||||
of node names [demo2.html](http://techtonik.github.com/springy/demo2.html):
|
[demo2.html](http://techtonik.github.com/springy/demo2.html):
|
||||||
|
|
||||||
var graph = new Graph();
|
var graph = new Graph();
|
||||||
graph.addNodes(['mark', 'higgs', 'other', 'etc']);
|
graph.addNodes('mark', 'higgs', 'other', 'etc');
|
||||||
|
graph.addEdges(
|
||||||
|
['mark', 'higgs'],
|
||||||
|
['mark', 'etc'],
|
||||||
|
['mark', 'other']
|
||||||
|
);
|
||||||
|
|
||||||
Advanced Drawing
|
Advanced Drawing
|
||||||
----
|
----
|
||||||
|
|
32
demo2.html
32
demo2.html
|
@ -8,27 +8,17 @@ var graph = new Graph();
|
||||||
graph.addNodes('Dennis', 'Michael', 'Jessica', 'Timothy', 'Barbara', 'Franklin')
|
graph.addNodes('Dennis', 'Michael', 'Jessica', 'Timothy', 'Barbara', 'Franklin')
|
||||||
graph.addNodes('Monty', 'James');
|
graph.addNodes('Monty', 'James');
|
||||||
|
|
||||||
var dennis = graph.newNode({label: 'Dennis'});
|
graph.addEdges(
|
||||||
var michael = graph.newNode({label: 'Michael'});
|
['Dennis', 'Michael', {color: '#00A0B0', label: 'Foo bar'}],
|
||||||
var jessica = graph.newNode({label: 'Jessica'});
|
['Michael', 'Dennis', {color: '#6A4A3C'}],
|
||||||
var timothy = graph.newNode({label: 'Timothy'});
|
['Michael', 'Jessica', {color: '#CC333F'}],
|
||||||
var barbara = graph.newNode({label: 'Barbara'});
|
['Jessica', 'Barbara', {color: '#EB6841'}],
|
||||||
var franklin = graph.newNode({label: 'Franklin'});
|
['Michael', 'Timothy', {color: '#EDC951'}],
|
||||||
var monty = graph.newNode({label: 'Monty'});
|
['Franklin', 'Monty', {color: '#7DBE3C'}],
|
||||||
var james = graph.newNode({label: 'James'});
|
['Dennis', 'Monty', {color: '#000000'}],
|
||||||
var bianca = graph.newNode({label: 'Bianca'});
|
['Monty', 'James'],
|
||||||
|
['Barbara', 'Timothy', {color: '#6A4A3C'}]
|
||||||
graph.newEdge(dennis, michael, {color: '#00A0B0', label: 'Foo bar'});
|
);
|
||||||
graph.newEdge(michael, dennis, {color: '#6A4A3C'});
|
|
||||||
graph.newEdge(michael, jessica, {color: '#CC333F'});
|
|
||||||
graph.newEdge(jessica, barbara, {color: '#EB6841'});
|
|
||||||
graph.newEdge(michael, timothy, {color: '#EDC951'});
|
|
||||||
graph.newEdge(franklin, monty, {color: '#7DBE3C'});
|
|
||||||
graph.newEdge(dennis, monty, {color: '#000000'});
|
|
||||||
graph.newEdge(monty, james, {color: '#00A0B0'});
|
|
||||||
graph.newEdge(barbara, timothy, {color: '#6A4A3C'});
|
|
||||||
graph.newEdge(dennis, bianca, {color: '#CC333F'});
|
|
||||||
graph.newEdge(bianca, monty, {color: '#EB6841'});
|
|
||||||
|
|
||||||
jQuery(function(){
|
jQuery(function(){
|
||||||
var springy = jQuery('#springydemo').springy({
|
var springy = jQuery('#springydemo').springy({
|
||||||
|
|
19
springy.js
19
springy.js
|
@ -109,6 +109,25 @@ Graph.prototype.addEdge = function(edge) {
|
||||||
return edge;
|
return edge;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Graph.prototype.addEdges = function() {
|
||||||
|
// accepts variable number of arguments, where each argument
|
||||||
|
// is a triple [nodeid1, nodeid2, attributes]
|
||||||
|
for (var i = 0; i < arguments.length; i++) {
|
||||||
|
var e = arguments[i];
|
||||||
|
var node1 = this.nodeSet[e[0]];
|
||||||
|
if (node1 == undefined) {
|
||||||
|
throw new TypeError("invalid node name: " + e[0]);
|
||||||
|
}
|
||||||
|
var node2 = this.nodeSet[e[1]];
|
||||||
|
if (node2 == undefined) {
|
||||||
|
throw new TypeError("invalid node name: " + e[1]);
|
||||||
|
}
|
||||||
|
var attr = e[2];
|
||||||
|
|
||||||
|
this.newEdge(node1, node2, attr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Graph.prototype.newNode = function(data) {
|
Graph.prototype.newNode = function(data) {
|
||||||
var node = new Node(this.nextNodeId++, data);
|
var node = new Node(this.nextNodeId++, data);
|
||||||
this.addNode(node);
|
this.addNode(node);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user