Class: RubyPost::Graph

Inherits:
Drawable show all
Defined in:
lib/graph.rb

Overview

wrapper for the metapost graphing macro multiple coordinate systems in a single graph is not yet supported May require pre and post options for graph data which doesn’t have a neat solution. You can ofcourse, still do this with a CustomDrawable.

Instance Method Summary collapse

Constructor Details

#initializeGraph

automattically sets the size of the graph to 10cm x 10cm



11
12
13
14
15
16
17
18
# File 'lib/graph.rb', line 11

def initialize
  super()
  @options = Array.new
  @dati  = Array.new
  @@Inputs.add_input('graph')
  @@Inputs.add_input('sarith')
  self.set_size(10,10)
end

Instance Method Details

#add_data(d) ⇒ Object



20
21
22
# File 'lib/graph.rb', line 20

def add_data(d)
  @dati.push(d)
end

#add_graph_option(o) ⇒ Object

add a graph option such as it Title, Xlabel etc.



31
32
33
# File 'lib/graph.rb', line 31

def add_graph_option(o)
  @options.push(o)
end

#compileObject



42
43
44
45
46
47
48
49
50
# File 'lib/graph.rb', line 42

def compile
  str = "begingraph(" + @width.to_s + "cm, " + @height.to_s + "cm);\n"
  str = str + compile_options + "\n"
  @dati.each do |d| 
    str = str + d.compile_pre_commands + "gdraw " + d.compile + d.compile_post_commands + "\n"
  end
  str = str + "endgraph"
  str
end

#compile_optionsObject

utility function to compile all the graph options



36
37
38
39
40
# File 'lib/graph.rb', line 36

def compile_options
  str = String.new
  @options.each { |o| str = str + ' ' + o.compile }
  return str
end

#set_size(w, h) ⇒ Object

set the size of the graph in cm



25
26
27
28
# File 'lib/graph.rb', line 25

def set_size(w,h)
  @width = w
  @height = h
end