Class: RubyGraphviz
- Inherits:
-
Object
- Object
- RubyGraphviz
- Defined in:
- lib/ruby_graphviz.rb
Overview
lib/ruby_graphviz.rb – graphviz dot generator library
- Author
-
Yoichiro Hasebe (mailto: [email protected])
- Copyright
-
Copyright 2009 Yoichiro Hasebe
- License
-
GNU GPL version 3
Instance Method Summary collapse
-
#arrow_edge(nid1, nid2, edge_hash = nil) ⇒ Object
Create a directional edge (arrow from node to node) with its options .
-
#edge(nid1, nid2, edge_hash = nil) ⇒ Object
Create a non-directional edge (connection line between nodes) with its options .
-
#edge_default(edge_hash = nil) ⇒ Object
Set default options for edges.
-
#initialize(name, graph_hash = nil) ⇒ RubyGraphviz
constructor
Example: .
-
#node(node_id, node_hash = nil) ⇒ Object
Create a node with its options.
-
#node_default(node_hash = nil) ⇒ Object
Set default options for nodes.
-
#rank(nid1, nid2, edge_hash = nil) ⇒ Object
Align nodes on the same rank connecting them with non-directional edges.
-
#subgraph(graph) ⇒ Object
Add a subgraph to a graph (recursively).
-
#to_dot ⇒ Object
Convert graph into dot formatted data.
Constructor Details
#initialize(name, graph_hash = nil) ⇒ RubyGraphviz
Example:
g = RubyGraphviz.new("newgraph", {:rankdir => "LR", :nodesep => "0.4", :ranksep => "0.2"})
12 13 14 15 16 17 18 19 |
# File 'lib/ruby_graphviz.rb', line 12 def initialize(name, graph_hash = nil) @name = name @graph_data = graph_hash @nodes = [] @edges = [] @dot = "" create_graph end |
Instance Method Details
#arrow_edge(nid1, nid2, edge_hash = nil) ⇒ Object
Create a directional edge (arrow from node to node) with its options
Example:
graph.arrow_edge("node-01", "node-02", :label => "from 1 to 2", :color => "lightblue")
148 149 150 151 |
# File 'lib/ruby_graphviz.rb', line 148 def arrow_edge(nid1, nid2, edge_hash = nil) @dot << create_edge("->", nid1, nid2, edge_hash) + ";\n" self end |
#edge(nid1, nid2, edge_hash = nil) ⇒ Object
Create a non-directional edge (connection line between nodes) with its options
Example:
graph.edge("node-01", "node-02", :label => "connecting 1 and 2", :color => "lightblue")
138 139 140 141 |
# File 'lib/ruby_graphviz.rb', line 138 def edge(nid1, nid2, edge_hash = nil) @dot << create_edge("--", nid1, nid2, edge_hash) + ";\n" self end |
#edge_default(edge_hash = nil) ⇒ Object
Set default options for edges
Example:
graph.edge_default(:color => "gray60")
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ruby_graphviz.rb', line 96 def edge_default(edge_hash = nil) @dot << " edge[" index = 0 edge_hash.each do |k, v| k = k.to_s @dot << "#{k} = \"#{v}\"" index += 1 @dot << ", " unless index == edge_hash.size end @dot << "];\n" self end |
#node(node_id, node_hash = nil) ⇒ Object
Create a node with its options
Example:
graph.node("node-01", :label => "Node 01", :fillcolor => "pink")
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby_graphviz.rb', line 115 def node(node_id, node_hash = nil) @dot << " #{node_id.to_s}" index = 0 if node_hash @dot << " [" node_hash.each do |k, v| k = k.to_s @dot << "#{k} = \"#{v}\"" index += 1 @dot << ", " unless index == node_hash.size end @dot << "]" end @dot << ";\n" self end |
#node_default(node_hash = nil) ⇒ Object
Set default options for nodes
Example:
graph.node_default(:shape => "record", :color => "gray60")
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ruby_graphviz.rb', line 77 def node_default(node_hash = nil) @dot << " node[" index = 0 node_hash.each do |k, v| k = k.to_s @dot << "#{k} = \"#{v}\"" index += 1 @dot << ", " unless index == node_hash.size end @dot << "];\n" self end |
#rank(nid1, nid2, edge_hash = nil) ⇒ Object
Align nodes on the same rank connecting them with non-directional edges
155 156 157 158 |
# File 'lib/ruby_graphviz.rb', line 155 def rank(nid1, nid2, edge_hash = nil) @dot << "{rank=same " + create_edge("--", nid1, nid2, edge_hash) + "}\n" self end |
#subgraph(graph) ⇒ Object
Add a subgraph to a graph (recursively)
Example:
graph1.subgraph(graph2)
67 68 69 |
# File 'lib/ruby_graphviz.rb', line 67 def subgraph(graph) @dot << graph.to_dot.sub(/\Agraph/, "subgraph") end |
#to_dot ⇒ Object
Convert graph into dot formatted data
162 163 164 165 166 |
# File 'lib/ruby_graphviz.rb', line 162 def to_dot finish_graph @dot = @dot.gsub(/\"\</m, "<").gsub(/\>\"/m, ">") return @dot end |