Skip to content

Commit

Permalink
Update render
Browse files Browse the repository at this point in the history
  • Loading branch information
glejeune committed Oct 10, 2010
1 parent 6a6aa04 commit 39d75d1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 :

Expand Down
4 changes: 4 additions & 0 deletions lib/deps/attributs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
59 changes: 55 additions & 4 deletions lib/deps/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/hello.js
Original file line number Diff line number Diff line change
@@ -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.