Class: MOSAIK::Graph::Visualizer
- Inherits:
-
Object
- Object
- MOSAIK::Graph::Visualizer
- Defined in:
- lib/mosaik/graph/visualizer.rb
Overview
Visualize the graph
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options, graph) ⇒ Visualizer
constructor
A new instance of Visualizer.
- #to_dot ⇒ Object
- #to_png(file) ⇒ Object
- #to_svg(file) ⇒ Object
Constructor Details
#initialize(options, graph) ⇒ Visualizer
Returns a new instance of Visualizer.
11 12 13 14 |
# File 'lib/mosaik/graph/visualizer.rb', line 11 def initialize(, graph) @options = @graph = graph end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
9 10 11 |
# File 'lib/mosaik/graph/visualizer.rb', line 9 def graph @graph end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/mosaik/graph/visualizer.rb', line 9 def @options end |
Instance Method Details
#to_dot ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mosaik/graph/visualizer.rb', line 16 def to_dot # Set of visited edges (to avoid duplicates in undirected graphs) visited = Set.new # List of vertices with incoming or outgoing edges coupled_vertices = graph .vertices .values .select { |v| v.edges.any? } + graph .vertices .values .map { |v| v.edges.keys } .flatten(2) .uniq .map { |id| graph.find_vertex(id) } [ graph.directed ? "digraph {" : "graph {", (if graph.clusters.any? graph .clusters .values .filter_map do |cluster| next unless [:show_uncoupled] || (cluster.vertices.any? { |vertex| vertex.edges.any? }) [ "subgraph \"#{cluster.id}\" {", " cluster = true", " label = \"#{cluster.id}\"", ' color = "gray"', *cluster.vertices.map { |vertex| " \"#{vertex.id}\"" }, "}", ] end.join("\n ").prepend(" ") end), graph .vertices .values .map do |vertex| [ ("\"#{vertex.id}\" [shape=circle, width=1, fixedsize=true, fontsize=12, style=filled, fillcolor=lightblue]" if [:show_uncoupled] || vertex.in?(coupled_vertices)), *vertex .edges .flat_map do |key, edges| edges.map do |edge| next if edge.in? visited visited << edge [ "\"#{vertex.id}\" ", graph.directed? ? "->" : "--", " \"#{key}\"", [:show_labels] && edge.attributes.any? ? " [label=\"#{edge.attributes.map { |ek, ev| "#{ek}: #{ev}" }.join(', ')}\"]" : nil, ].compact.join end end, ].compact_blank.join("\n ") end.compact_blank.join("\n ").prepend(" "), "}\n", ].compact.join("\n") end |
#to_png(file) ⇒ Object
79 80 81 82 |
# File 'lib/mosaik/graph/visualizer.rb', line 79 def to_png(file) File.write("#{file}.gv", to_dot) system("#{.fetch(:renderer, 'dot')} -x -Goverlap=scale -Tpng #{file}.gv -o #{file}.png#{' 2> /dev/null' unless [:debug]}") end |
#to_svg(file) ⇒ Object
84 85 86 87 |
# File 'lib/mosaik/graph/visualizer.rb', line 84 def to_svg(file) File.write("#{file}.gv", to_dot) system("#{.fetch(:renderer, 'dot')} -x -Goverlap=scale -Tsvg #{file}.gv -o #{file}.svg#{' 2> /dev/null' unless [:debug]}") end |