Class: GV::Graph
Overview
Represents a toplevel graph
Class Method Summary collapse
-
.load(io) ⇒ Object
Loads a graph from a string of file.
-
.open(name, type = :directed, strictness = :normal) {|graph| ... } ⇒ Graph
Creates a new graph.
Instance Method Summary collapse
- #graph ⇒ Object
-
#initialize(ptr) ⇒ Graph
constructor
A new instance of Graph.
-
#render(format = 'png', layout = 'dot') ⇒ String
Renders the graph to an image and returns the result as a string.
-
#save(filename, format = 'png', layout = 'dot') ⇒ nil
Renders the graph to an images and saves the result to a file.
Methods inherited from BaseGraph
#directed?, #edge, #node, #strict?, #sub_graph
Methods inherited from Component
#==, #[], #[]=, #hash, #html, #name
Constructor Details
#initialize(ptr) ⇒ Graph
Returns a new instance of Graph.
265 266 267 |
# File 'lib/gv.rb', line 265 def initialize(ptr) @ptr = ptr end |
Class Method Details
.load(io) ⇒ Object
Loads a graph from a string of file
255 256 257 258 259 260 261 262 |
# File 'lib/gv.rb', line 255 def load(io) data = if io.is_a? String io else io.read end new Libcgraph.agmemread(data) end |
.open(name, type = :directed, strictness = :normal) {|graph| ... } ⇒ Graph
Creates a new graph
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/gv.rb', line 231 def open(name, type = :directed, strictness = :normal) ag_type = case [type, strictness] when %i[directed normal] Libcgraph.Agdirected when %i[undirected normal] Libcgraph.Agundirected when %i[directed strict] Libcgraph.Agstrictdirected when %i[undirected strict] Libcgraph.Agstrictundirected else raise ArgumentError, "invalid graph type #{[type, strictness]}" end graph = new(Libcgraph.agopen(name, ag_type, FFI::Pointer::NULL)) yield graph if block_given? graph end |
Instance Method Details
#graph ⇒ Object
269 270 271 |
# File 'lib/gv.rb', line 269 def graph self end |
#render(format = 'png', layout = 'dot') ⇒ String
Renders the graph to an image and returns the result as a string
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/gv.rb', line 290 def render(format = 'png', layout = 'dot') Libgvc.gvLayout(@@gvc, ptr, layout.to_s) data_ptr = FFI::MemoryPointer.new(:pointer, 1) len_ptr = FFI::MemoryPointer.new(:int, 1) Libgvc.gvRenderData(@@gvc, ptr, format.to_s, data_ptr, len_ptr) len = len_ptr.read_uint data_ptr = data_ptr.read_pointer data = data_ptr.read_string len Libgvc.gvFreeRenderData(data_ptr) Libgvc.gvFreeLayout(@@gvc, ptr) data end |
#save(filename, format = 'png', layout = 'dot') ⇒ nil
Renders the graph to an images and saves the result to a file
278 279 280 281 282 283 284 |
# File 'lib/gv.rb', line 278 def save(filename, format = 'png', layout = 'dot') Libgvc.gvLayout(@@gvc, ptr, layout.to_s) Libgvc.gvRenderFilename(@@gvc, ptr, format.to_s, filename) Libgvc.gvFreeLayout(@@gvc, ptr) nil end |