Class: Nodepile::GraphVisualizer
- Inherits:
-
Object
- Object
- Nodepile::GraphVisualizer
- Defined in:
- lib/nodepile/gviz.rb
Overview
This class converts a set of nodes and edges into a visualization rendered by the graphviz tool.
Note: At present this is a wrapper around the ruby-graphviz gem as interface
to the graphviz software tool. In order for this class to work
the graphviz software package must be installed on the system. At some
point I'm probably going to want to pull out that dependency because
I'm a classic NIH bigot. But I always think about Luke and his lightsaber.
Constant Summary collapse
- NODE_ATTR_MAPS =
{ '_shape' => :shape, '_color' => :color, '_fillcolor' => Proc.new{|obj,s| obj[:fillcolor] = s; obj[:style] ||= 'filled'}, '_fontcolor' => :fontcolor, '_label' => :label, }
- EDGE_ATTR_MAPS =
{ '_color' => :color, '_fontcolor' => :fontcolor, '_label' => :label, }
Instance Method Summary collapse
-
#emit_file(fpath, configs: nil, file_format: nil) ⇒ String
Generate a file based on the load specified.
-
#initialize ⇒ GraphVisualizer
constructor
A new instance of GraphVisualizer.
- #inspect ⇒ Object
-
#load(node_record_enum, edge_record_enum, configs: Hash.new) ⇒ void
Given edge and node packets,.
Constructor Details
#initialize ⇒ GraphVisualizer
Returns a new instance of GraphVisualizer.
18 19 20 |
# File 'lib/nodepile/gviz.rb', line 18 def initialize @graph = nil end |
Instance Method Details
#emit_file(fpath, configs: nil, file_format: nil) ⇒ String
Generate a file based on the load specified
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nodepile/gviz.rb', line 71 def emit_file(fpath,configs: nil ,file_format: nil) configs ||= Hash.new extension = /.*\.(png|svg|dot)$/.match(fpath)&.[](1) fmt = file_format || extension&.to_sym raise "Output file format is unspecified and can't be deduced from file extension" unless fmt oargs = Hash.new oargs[fmt] = fpath oargs[:use] = configs[:layout_engine] if configs[:layout_engine] @graph.output(oargs) return fpath end |
#inspect ⇒ Object
22 |
# File 'lib/nodepile/gviz.rb', line 22 def inspect = "#<#{self.class}:0x#{object_id}" |
#load(node_record_enum, edge_record_enum, configs: Hash.new) ⇒ void
This method returns an undefined value.
Given edge and node packets,
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/nodepile/gviz.rb', line 45 def load(node_record_enum, edge_record_enum,configs: Hash.new) @graph = GraphViz.new(:G) @graph.type = configs[:directionality] if configs[:directionality] nodes = Hash.new # temporary cache node_record_enum.each{|nr| n = nodes[nr['@key']] = @graph.add_nodes(nr['@key']) self.class._apply_attrs(n,nr,NODE_ATTR_MAPS) } edge_record_enum.each{|er| node_pair = er['@key'].map{|k| nodes[k] } e = @graph.add_edges(*node_pair) self.class._apply_attrs(e,er,EDGE_ATTR_MAPS) } end |