Class: Cgraph::Graph
- Inherits:
-
Object
- Object
- Cgraph::Graph
- Defined in:
- lib/cgraph.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
Instance Method Summary collapse
- #add_connections_to_graph(graph) ⇒ Object
- #add_entities(graph) ⇒ Object
- #calc_penwidth(count) ⇒ Object
- #find_entity(name) ⇒ Object
- #generate_connections(data) ⇒ Object
- #generate_entities(data, infos = nil) ⇒ Object
- #generate_graph ⇒ Object
-
#initialize(data, info, options = {}) ⇒ Graph
constructor
A new instance of Graph.
- #to_png ⇒ Object
Constructor Details
#initialize(data, info, options = {}) ⇒ Graph
Returns a new instance of Graph.
36 37 38 39 40 41 42 43 44 |
# File 'lib/cgraph.rb', line 36 def initialize data, info, ={} Entity.clear_namelist @connections = [] @entities = [] @root = [:root] || "" @no_penwidth = [:penwidth] || false generate_entities data, info generate_connections data end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
35 36 37 |
# File 'lib/cgraph.rb', line 35 def connections @connections end |
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
35 36 37 |
# File 'lib/cgraph.rb', line 35 def entities @entities end |
Instance Method Details
#add_connections_to_graph(graph) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cgraph.rb', line 92 def add_connections_to_graph graph @connections.each do |conn| tupel = conn.first = {} if @no_penwidth = {} else = {style: calc_penwidth(conn[1][:count])} end graph.add_edges tupel[0],tupel[1], end graph end |
#add_entities(graph) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cgraph.rb', line 69 def add_entities graph @entities.each do |e| if e.picture graph.add_nodes e.name,label: "",image: e.picture else graph.add_nodes e.name, label: e.label end end graph end |
#calc_penwidth(count) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/cgraph.rb', line 107 def calc_penwidth(count) case count when 4..10 then 'dashed' when 11..1000 then 'solid' else 'dotted' end end |
#find_entity(name) ⇒ Object
64 65 66 67 68 |
# File 'lib/cgraph.rb', line 64 def find_entity name @entities.each do |e| return e if e.name == name end end |
#generate_connections(data) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/cgraph.rb', line 57 def generate_connections data @connections = data.group_by do |a,b| a+b; [a,b] end.map do |a,b| [a ,{count: b.count}] end end |
#generate_entities(data, infos = nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cgraph.rb', line 45 def generate_entities data, infos = nil data.flatten.uniq.each do |name| tmp = Entity.new name if !infos.nil? && !infos[name].nil? info = infos[name] tmp.label = info[:label] || "" tmp.picture = info[:picture] || "" tmp.group = info[:group] || "" end @entities.push tmp end end |
#generate_graph ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cgraph.rb', line 79 def generate_graph if @root graph = GraphViz.new(:G, :type => :digraph, :root => @root) else graph = GraphViz.new(:G, :type => :digraph) end graph = add_connections_to_graph graph graph = add_entities graph graph end |
#to_png ⇒ Object
89 90 91 |
# File 'lib/cgraph.rb', line 89 def to_png graph.output(:png => "test.png") end |