class GV::Graph

Public Class Methods

load(io) click to toggle source

Loads a graph from a string of file @param io [IO, String] the resource to load from @return the newly loaded graph

# File lib/gv.rb, line 250
def load(io)
  data = if io.is_a? String
    io
  else
    io.read
  end
  new LibCGraph.agmemread(data)
end
new(ptr) click to toggle source
# File lib/gv.rb, line 260
def initialize(ptr)
  @ptr = ptr
end
open(name, type = :directed, strictness = :normal) { |graph| ... } click to toggle source

Creates a new graph @param type [:directed, :undirected] the graphs type @param strictness [:strict, :normal] the graphs strict type @see www.graphviz.org/doc/info/attrs.html Node, Edge and Graph Attributes @yieldparam graph [Graph] the newly created graph @return [Graph] the newly created graph

# File lib/gv.rb, line 228
def open(name, type = :directed, strictness = :normal)
  ag_type = case [type, strictness]
              when [:directed, :normal] then LibCGraph.Agdirected
              when [:undirected, :normal] then LibCGraph.Agundirected
              when [:directed, :strict] then LibCGraph.Agstrictdirected
              when [:undirected, :strict] then LibCGraph.Agstrictundirected
              else
                raise ArgumentError, "invalid graph type #{[type, strictness]}"
              end

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

  if block_given?
    yield graph
  end

  graph
end

Public Instance Methods

graph() click to toggle source
# File lib/gv.rb, line 264
def graph
  self
end
render(format = 'png', layout = 'dot') click to toggle source

Renders the graph to an image and returns the result as a string @param format [String] the image format to use, e.g. 'svg', 'pdf' etc. @param layout [String] the layout to use, e.g. 'dot' or 'neato' etc. @return [String] the rendered graph in the given format

# File lib/gv.rb, line 285
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') click to toggle source

Renders the graph to an images and saves the result to a file @param filename [String] the filename @param format [String] the image format to use, e.g. 'svg', 'pdf' etc. @param layout [String] the layout to use, e.g. 'dot' or 'neato' etc. @return [nil]

# File lib/gv.rb, line 273
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