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


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

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


149
150
151
152
# File 'lib/rubyfca/ruby_graphviz.rb', line 149

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


139
140
141
142
# File 'lib/rubyfca/ruby_graphviz.rb', line 139

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


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

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


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

def node(node_id, node_hash = nil)
  @dot << "  #{node_id}"
  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")


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

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



156
157
158
159
# File 'lib/rubyfca/ruby_graphviz.rb', line 156

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)


68
69
70
# File 'lib/rubyfca/ruby_graphviz.rb', line 68

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

#to_dotObject

Convert graph into dot formatted data



163
164
165
166
# File 'lib/rubyfca/ruby_graphviz.rb', line 163

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