Class: GV::Graph

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

Overview

Represents a toplevel graph

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

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

Parameters:

  • io (IO, String)

    the resource to load from

Returns:

  • the newly loaded graph



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

Parameters:

  • type (:directed, :undirected) (defaults to: :directed)

    the graphs type

  • strictness (:strict, :normal) (defaults to: :normal)

    the graphs strict type

Yield Parameters:

  • graph (Graph)

    the newly created graph

Returns:

  • (Graph)

    the newly created graph

See Also:



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

#graphObject



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

Parameters:

  • format (String) (defaults to: 'png')

    the image format to use, e.g. ‘svg’, ‘pdf’ etc.

  • layout (String) (defaults to: 'dot')

    the layout to use, e.g. ‘dot’ or ‘neato’ etc.

Returns:

  • (String)

    the rendered graph in the given format



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

Parameters:

  • filename (String)

    the filename

  • format (String) (defaults to: 'png')

    the image format to use, e.g. ‘svg’, ‘pdf’ etc.

  • layout (String) (defaults to: 'dot')

    the layout to use, e.g. ‘dot’ or ‘neato’ etc.

Returns:

  • (nil)


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