diff --git a/README.mkdn b/README.mkdn index 22c6358..9d3ea43 100644 --- a/README.mkdn +++ b/README.mkdn @@ -55,13 +55,18 @@ Springy 1.1+ supports simplified API for adding nodes and edges, see ['mark', 'other'] ); -Springy 1.2+ also accepts JSON, see +Springy 1.2+ also accepts JSON with node attributes, see [demo-json.html](http://dhotson.github.com/springy/demo-json.html): graphJSON = { - "nodes": ["mark", "higgs", "other", "etc"], + "nodes": [ + ["mark", {"label":"Mark", "color":"#0000ff"}], + "higgs", + "other", + "etc" + ], "edges": [ - ["mark", "higgs"], + ["mark", "higgs", {"label":"Foo", "color":"#ff0000"}], ["mark", "etc"], ["mark", "other"] ] diff --git a/springy.js b/springy.js index 392f94b..c12aeee 100644 --- a/springy.js +++ b/springy.js @@ -90,12 +90,19 @@ // accepts variable number of arguments, where each argument // is a string that becomes both node identifier and label for (var i = 0; i < arguments.length; i++) { - var name = arguments[i]; - var node = new Node(name, {label:name}); + var arg = arguments[i]; + var node; + if (typeof arg === "string") { + node = new Node(arg, {label:arg}); + } else { + node = new Node(arg[0], arg[1]); + } + this.addNode(node); } }; + Graph.prototype.addEdge = function(edge) { var exists = false; this.edges.forEach(function(e) { @@ -168,14 +175,15 @@ { "nodes": [ - "center", + ["center", {"label":"Center","color":"#ff0000"}], "left", "right", "up", "satellite" ], "edges": [ - ["center", "left"], + ["center", "left", {"label":"Foo"},"color":"#0000ff"], + ["center", "right"], ["center", "up"] ] @@ -193,7 +201,6 @@ } } - // find the edges from node1 to node2 Graph.prototype.getEdges = function(node1, node2) { if (node1.id in this.adjacency