Class: GV::Graph

Inherits:
BaseGraph show all
Defined in:
lib/gv.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseGraph

#directed?, #edge, #node, #strict?, #sub_graph

Methods inherited from Component

#==, #[], #[]=, #hash, #html, #name

Constructor Details

#initialize(ptr) ⇒ Graph


255
256
257
# File 'lib/gv.rb', line 255

def initialize(ptr)
  @ptr = ptr
end

Class Method Details

.load(io) ⇒ Object

Loads a graph from a string of file


245
246
247
248
249
250
251
252
# File 'lib/gv.rb', line 245

def load(io)
  data = if io.is_a? String
    io
  else
    io.read
  end
  new FFI.agmemread(data)
end

.open(name, type = :directed, strictness = :normal) {|graph| ... } ⇒ Graph

Creates a new graph

Yield Parameters:

  • graph (Graph)

    the newly created graph

See Also:


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gv.rb', line 223

def open(name, type = :directed, strictness = :normal)
  ag_type = case [type, strictness]
              when [:directed, :normal] then FFI.Agdirected
              when [:undirected, :normal] then FFI.Agundirected
              when [:directed, :strict] then FFI.Agstrictdirected
              when [:undirected, :strict] then FFI.Agstrictundirected
              else
                raise ArgumentError, "invalid graph type #{[type, strictness]}"
              end

  graph = new(FFI.agopen(name, ag_type, ::FFI::Pointer::NULL))

  if block_given?
    yield graph
  end

  graph
end

Instance Method Details

#graphObject


259
260
261
# File 'lib/gv.rb', line 259

def graph
  self
end

#render(format = 'png', layout = 'dot') ⇒ String

Renders the graph to an image and returns the result as a string


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gv.rb', line 280

def render(format = 'png', layout = 'dot')
  FFI.gvLayout(@@gvc, ptr, layout.to_s)

  data_ptr = ::FFI::MemoryPointer.new(:pointer, 1)
  len_ptr = ::FFI::MemoryPointer.new(:int, 1)

  FFI.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_length len

  FFI.gvFreeRenderData(data_ptr)
  FFI.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


268
269
270
271
272
273
274
# File 'lib/gv.rb', line 268

def save(filename, format = 'png', layout = 'dot')
  FFI.gvLayout(@@gvc, ptr, layout.to_s)
  FFI.gvRenderFilename(@@gvc, ptr, format.to_s, filename);
  FFI.gvFreeLayout(@@gvc, ptr)

  nil
end