Skip to content
guiman edited this page May 21, 2012 · 3 revisions

How can I add named edges?

You just need to use the attribute label (for more informations about attributs, see http://www.graphviz.org/content/attrs)

Here are differents examples on how to do this :

Solution #1 - With the "object" API

require 'graphviz'

g = GraphViz.new(:G)

nodeA = g.add_node("nodeA")
nodeB = g.add_node("nodeB")

edge = g.add_edge(nodeA, nodeB)

edge[:label] = "edge label"

Solution #2 - With a block

require 'graphviz'

GraphViz.new(:G) { |g|
   edge = g.nodeA << g.nodeB
   edge[:label => "edge label"]
}

Solution #3 - With a block (short)

require 'graphviz'

GraphViz.new(:G) { |g|
   (g.nodeA << g.nodeB)[:label => "edge label"]
}

Solution #4 - With the DSL

require 'graphviz/dsl'

digraph :G do
   nA = n("nodeA")
   nB = n("nodeB")
   edge = e(nA, nB)
   edge[:label => "edge label"]
end

Solution #5 - With the DSL (short)

require 'graphviz/dsl'

digraph :G do
   (nodeA << nodeB)[:label => "edge label"]
end

How do I change the output format?

Well, it's pretty straight forward, here its an example:

require 'graphviz'

g = GraphViz.new(:g, type: :digraph)
#create the graph....
g.output :svg => "filename.svg"

To change the output format just change svg for the desired format, like dot or png. The complete list could be found here(the FORMAT constant): Available Graphviz Formats

Clone this wiki locally