Created
July 3, 2011 06:32
-
-
Save micktaiwan/1062005 to your computer and use it in GitHub Desktop.
Ruby code using GraphViz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a Ruby comment | |
def graph_node(n, parent=nil, depth=0) | |
#print n, " " | |
gn = @g.add_node(n.object_id.to_s, :label=>n.to_graphviz, :shape=>"Mrecord") | |
if parent | |
e = @g.add_edge(parent, gn) | |
end | |
if n == @current_pos_node | |
gn[:color] = "brown3" | |
gn[:style] = "filled" | |
elsif @s.tree.pv(@current_pos_node).include?(n) | |
gn[:color] = "cadetblue" | |
gn[:style] = "filled" | |
elsif @s.tree.pv(@root).include?(n) | |
gn[:color] = "yellow" | |
gn[:style] = "filled" | |
end | |
return if !n.children # or depth == 2 | |
i = 0 | |
for c in n.children | |
graph_node(c, gn, depth+1) | |
i += 1 | |
#break if i > 2 | |
end | |
end | |
def graph(name="tree", root_node=@current_pos_node) | |
@g = GraphViz::new("G") | |
#@g['sep'] = "10,100" | |
#@g["overlap"] = "compress" | |
#@g["rankdir"] = "BT" | |
#@g["ratio"] = "0.9" | |
@g["size"] = "350,500" | |
graph_node(root_node) | |
@g.output(:svg => "#{name}.svg") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment