Class: RubyGraphviz

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyfca/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

Constructor Details

#initialize(name, graph_hash = nil) ⇒ RubyGraphviz

Example:

g = RubyGraphviz.new("newgraph", {:rankdir => "LR", :nodesep => "0.4", :ranksep => "0.2"})


15
16
17
18
19
20
21
22
# File 'lib/rubyfca/ruby_graphviz.rb', line 15

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")


151
152
153
154
# File 'lib/rubyfca/ruby_graphviz.rb', line 151

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")


141
142
143
144
# File 'lib/rubyfca/ruby_graphviz.rb', line 141

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")


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubyfca/ruby_graphviz.rb', line 99

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")


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rubyfca/ruby_graphviz.rb', line 118

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")


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubyfca/ruby_graphviz.rb', line 80

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



158
159
160
161
# File 'lib/rubyfca/ruby_graphviz.rb', line 158

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)


70
71
72
# File 'lib/rubyfca/ruby_graphviz.rb', line 70

def subgraph(graph)
  @dot << graph.to_dot.sub(/\Agraph/, "subgraph")
end

#to_dotObject

Convert graph into dot formatted data



165
166
167
168
169
# File 'lib/rubyfca/ruby_graphviz.rb', line 165

def to_dot
  finish_graph
  @dot = @dot.gsub(/\"\</m, "<").gsub(/\>\"/m, ">")
  return @dot
end