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
|
||||
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
|
||||
of node names [demo2.html](http://techtonik.github.com/springy/demo2.html):
|
||||
Springy 1.1+ supports simplified API for adding nodes and edges, see
|
||||
[demo2.html](http://techtonik.github.com/springy/demo2.html):
|
||||
|
||||
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
|
||||
----
|
||||
|
|
32
demo2.html
32
demo2.html
|
@ -8,27 +8,17 @@ var graph = new Graph();
|
|||
graph.addNodes('Dennis', 'Michael', 'Jessica', 'Timothy', 'Barbara', 'Franklin')
|
||||
graph.addNodes('Monty', 'James');
|
||||
|
||||
var dennis = graph.newNode({label: 'Dennis'});
|
||||
var michael = graph.newNode({label: 'Michael'});
|
||||
var jessica = graph.newNode({label: 'Jessica'});
|
||||
var timothy = graph.newNode({label: 'Timothy'});
|
||||
var barbara = graph.newNode({label: 'Barbara'});
|
||||
var franklin = graph.newNode({label: 'Franklin'});
|
||||
var monty = graph.newNode({label: 'Monty'});
|
||||
var james = graph.newNode({label: 'James'});
|
||||
var bianca = graph.newNode({label: 'Bianca'});
|
||||
|
||||
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'});
|
||||
graph.addEdges(
|
||||
['Dennis', 'Michael', {color: '#00A0B0', label: 'Foo bar'}],
|
||||
['Michael', 'Dennis', {color: '#6A4A3C'}],
|
||||
['Michael', 'Jessica', {color: '#CC333F'}],
|
||||
['Jessica', 'Barbara', {color: '#EB6841'}],
|
||||
['Michael', 'Timothy', {color: '#EDC951'}],
|
||||
['Franklin', 'Monty', {color: '#7DBE3C'}],
|
||||
['Dennis', 'Monty', {color: '#000000'}],
|
||||
['Monty', 'James'],
|
||||
['Barbara', 'Timothy', {color: '#6A4A3C'}]
|
||||
);
|
||||
|
||||
jQuery(function(){
|
||||
var springy = jQuery('#springydemo').springy({
|
||||
|
|
19
springy.js
19
springy.js
|
@ -109,6 +109,25 @@ Graph.prototype.addEdge = function(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) {
|
||||
var node = new Node(this.nextNodeId++, data);
|
||||
this.addNode(node);
|
||||
|
|
Loading…
Reference in New Issue
Block a user