Browse files

Update render

  • Loading branch information...
1 parent 6a6aa04 commit 39d75d131c54313b5b5249aee6d10decaacd840b @glejeune committed Oct 10, 2010
Showing with 91 additions and 4 deletions.
  1. +1 −0 README.rdoc
  2. +4 −0 lib/deps/attributs.js
  3. +55 −4 lib/deps/graph.js
  4. +31 −0 tests/hello.js
View
1 README.rdoc
@@ -20,6 +20,7 @@ Interface to the GraphViz graphing tool
* Major bugs corrections
* Update Attributs class
* parse now accept a file or a script
+* Update render (see tests/hello.js)
=== 0.0.3 :
View
4 lib/deps/attributs.js
@@ -200,6 +200,10 @@ function validateAttribut(name, type) {
}
}
+exports.isValid = function(name, type) {
+ return validateAttribut(name, type);
+}
+
var Attributs = exports.Attributs = function(t) {
this._type = t;
this.attributs = new Hash();
View
59 lib/deps/graph.js
@@ -4,7 +4,8 @@
var Hash = require( './core_ext/hash' ).Hash,
Node = require( './node' ).Node,
Edge = require( './edge' ).Edge,
- Attributs = require( './attributs' ).Attributs,
+ gvattrs = require( './attributs' ),
+ Attributs = gvattrs.Attributs,
sys = require('sys'),
path = require('path'),
spawn = require('child_process').spawn;
@@ -274,12 +275,63 @@ Graph.prototype.to_dot = function() {
/**
* Generate an output in file or memory
*
- * @param {String} type The output file type (png, jpeg, ps, ...)
+ * @param {String|Object} type The output file type (png, jpeg, ps, ...) or options
* @param {String|Function} name_or_callback The output file name or callback
* @param {Function} errback Error callback
* @api public
+ *
+ * Options :
+ * - type : output file type (png, jpeg, ps, ...)
+ * - use : Graphviz command to use (dot, neato, ...)
+ * - path : GraphViz path
+ * - G :
+ * - N :
+ * - E :
*/
-Graph.prototype.render = function(type, name_or_callback, errback) {
+Graph.prototype.render = function(type_or_options, name_or_callback, errback) {
+ var parameters = [];
+
+ // Get output type
+ var type = type_or_options;
+ if( typeof(type_or_options) == 'object' ) {
+ type = type_or_options.type;
+
+ // Get use
+ if( type_or_options.use != undefined ) { this.use = type_or_options.use; }
+
+ // Get path
+ if( type_or_options.path != undefined ) { this.gvPath = type_or_options.path; }
+
+ // Get extra Graph Options
+ if( type_or_options.G != undefined ) {
+ for( attr in type_or_options.G ) {
+ if( gvattrs.isValid( attr, "G" ) == false ) {
+ sys.debug( "Warning : Invalid attribut `"+attr+"' for a graph" );
+ }
+ parameters.push( "-G"+attr+"="+type_or_options.G[attr] )
+ }
+ }
+ // Get extra Node Options
+ if( type_or_options.N != undefined ) {
+ for( attr in type_or_options.N ) {
+ if( gvattrs.isValid( attr, "N" ) == false ) {
+ sys.debug( "Warning : Invalid attribut `"+attr+"' for a node" );
+ }
+ parameters.push( "-N"+attr+"="+type_or_options.N[attr] )
+ }
+ }
+ // Get extra Edge Options
+ if( type_or_options.E != undefined ) {
+ for( attr in type_or_options.E ) {
+ if( gvattrs.isValid( attr, "E" ) == false ) {
+ sys.debug( "Warning : Invalid attribut `"+attr+"' for an edge" );
+ }
+ parameters.push( "-E"+attr+"="+type_or_options.E[attr] )
+ }
+ }
+ }
+ parameters.push( '-T' + type );
+
var dotScript = this.to_dot();
var cmd = this.use;
@@ -290,7 +342,6 @@ Graph.prototype.render = function(type, name_or_callback, errback) {
var rendered = null;
var out = ''
var err = ''
- var parameters = ['-T' + type];
var outcallback = function(data) {
if( rendered == null ) {
rendered = data;
View
31 tests/hello.js
@@ -0,0 +1,31 @@
+var sys = require('sys'),
+ graphviz = require('../lib/graphviz');
+
+// Create digraph G
+var g = graphviz.digraph("G");
+
+// Add node (ID: Hello)
+var n1 = g.addNode( "Hello" );
+
+// Add node (ID: World)
+g.addNode( "World" );
+
+// Add edge between the two nodes
+var e = g.addEdge( n1, "World" );
+
+// Generate a PNG output
+g.output( {
+ "type":"png",
+ "use":"dot",
+ "N" : {
+ "color":"blue",
+ "shape":"Mdiamond"
+ },
+ "E" : {
+ "color" : "red",
+ "label" : "Say"
+ },
+ "G" : {
+ "label" : "Example"
+ }
+}, "hello.png" );

0 comments on commit 39d75d1

Please sign in to comment.