glejeune / Ruby-Graphviz

[MIRROR] Ruby interface to the GraphViz graphing tool
https://gitlab.com/glejeune/ruby-graphviz
Other
609 stars 116 forks source link

How can I add named edges? #31

Closed jergason closed 12 years ago

jergason commented 12 years ago

I want to add a to an edge, so the string will appear next to the edge, but the documentation does not explain how to do this. The code shows that you can pass in a hash of edge attributes, but I am not sure of how to name those attributes.

glejeune commented 12 years ago

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

Here is 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

Greg