Skip to content

Instantly share code, notes, and snippets.

@micktaiwan
Created July 3, 2011 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micktaiwan/1062005 to your computer and use it in GitHub Desktop.
Save micktaiwan/1062005 to your computer and use it in GitHub Desktop.
Ruby code using GraphViz
# 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