Class: RubyPost::SimplePlot

Inherits:
Picture show all
Defined in:
lib/simpleplot.rb

Instance Attribute Summary collapse

Attributes inherited from Picture

#name

Instance Method Summary collapse

Methods inherited from Picture

#add_drawable, #compile

Methods inherited from Object

#compile

Constructor Details

#initialize(width, height) ⇒ SimplePlot

initialise with height and width in centemeters



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simpleplot.rb', line 10

def initialize(width, height)
  super()
  @plot_array = Array.new
  @height = height
  @width = width
  #axes set by default
  setaxes(-1,1,-1,1)
  @xtickstr = "\n"
  @ytickstr = "\n"
  @xlabel = "\n"
  @ylabel = "\n"
  @drawxaxis = true
  @drawyaxis = true
end

Instance Attribute Details

#drawxaxis=(value) ⇒ Object (writeonly)

Sets the attribute drawxaxis

Parameters:

  • value

    the value to set the attribute drawxaxis to.



7
8
9
# File 'lib/simpleplot.rb', line 7

def drawxaxis=(value)
  @drawxaxis = value
end

#drawyaxis=(value) ⇒ Object (writeonly)

Sets the attribute drawyaxis

Parameters:

  • value

    the value to set the attribute drawyaxis to.



7
8
9
# File 'lib/simpleplot.rb', line 7

def drawyaxis=(value)
  @drawyaxis = value
end

Instance Method Details

#add_plot(plot) ⇒ Object

the scale factors are decided by the very last plot you add you can adjust it with the axes command



27
28
29
30
# File 'lib/simpleplot.rb', line 27

def add_plot(plot)
  @plot_array.push(plot)
  setaxesfor(plot)
end

#precompileObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simpleplot.rb', line 80

def precompile
  str = "picture " + @name + ";\n"

  #draw the axes
  str = str + "\n%draw the axes\n"
  str = str + 'draw ((' + (@xscale*@xmax + 0.6).to_s + 'cm,0)--('+ (@xscale*@xmin - 0.3).to_s + "cm,0));\n" if (@drawxaxis)
  str = str + 'draw ((0, ' + (@yscale*@ymin).to_s + 'cm)--(0, ' + (@yscale*@ymax + 0.6).to_s + "cm));\n" if (@drawyaxis)

  str = str + "\n%draw xticks\n"
  str = str + @xtickstr
  str = str + "\n%draw yticks\n"
  str = str + @ytickstr
  str = str + "\n%draw xticks\n"
  str = str + @xlabel
  str = str + "\n%draw yticks\n"
  str = str + @ylabel


  #draw each plot componenet.
  @plot_array.each{|p| 
    str = str + p.compilewithscale(@xscale, @yscale) }

  str = str + @name + " := currentpicture; currentpicture := " + @@org_picture + ";\n"
end

#setaxes(xmin, xmax, ymin, ymax) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/simpleplot.rb', line 38

def setaxes(xmin, xmax, ymin, ymax)
  @xmax = xmax
  @xmin = xmin
  @ymax = ymax
  @ymin = ymin

  @xscale = @width.to_f/(xmax - xmin)
  @yscale = @height.to_f/(ymax - ymin)
end

#setaxesfor(plot) ⇒ Object



32
33
34
35
36
# File 'lib/simpleplot.rb', line 32

def setaxesfor(plot)
  puts plot.xmin
  puts plot.ymax
  setaxes(plot.xmin, plot.xmax, plot.ymin, plot.ymax)
end

#xlabel(t) ⇒ Object



72
73
74
# File 'lib/simpleplot.rb', line 72

def xlabel(t)
  @xlabel = 'label.top(' + t + ', (' + (@xscale*@xmax + 0.6).to_s + "cm,0));\n"
end

#xticks(x, label = nil, func = lambda {|v| 1}) ⇒ Object

set xtick marks, a is an array of tick position and label is the text for each tick. func describes whether a tick should be put on the top or the bottom. func(x) should return positive for on the bottom and negative for on the top.



52
53
54
55
56
57
58
59
60
# File 'lib/simpleplot.rb', line 52

def xticks(x, label=nil, func = lambda {|v| 1} )
  if(label==nil) then label = x.map{|v| latex(v.to_s) } end
  lplace = x.map{ |v| if(func.call(v) < 0) then 'top' else 'bot' end }
  @xtickstr = ''
  x.each_index{ |i|
    @xtickstr = @xtickstr + 'draw ((0,-0.05cm)--(0,0.05cm)) shifted (' + (x[i]*@xscale).to_s + "cm, 0);\n"
    @xtickstr = @xtickstr + 'label.' + lplace[i].to_s + '(' + label[i] + ' ,(' + (x[i]*@xscale).to_s + "cm, 0));\n"
  }
end

#ylabel(t) ⇒ Object



76
77
78
# File 'lib/simpleplot.rb', line 76

def ylabel(t)
  @ylabel = 'label.rt(' + t + ', (0, ' + (@yscale*@ymax + 0.6).to_s + "cm));\n"
end

#yticks(y, label = nil) ⇒ Object

set ytick marks, a is an array of tick position and laebl is the text



63
64
65
66
67
68
69
70
# File 'lib/simpleplot.rb', line 63

def yticks(y, label=nil )
  if(label==nil) then label = y.map{|v| latex(v.to_s) } end
  @ytickstr = ''
  y.each_index{ |i|
    @ytickstr = @ytickstr + 'draw ((-0.05cm,0)--(0.05cm,0)) shifted (0,' + (y[i]*@yscale).to_s + "cm);\n"
    @ytickstr = @ytickstr + 'label.rt(' + label[i] + ' ,(0,' + (y[i]*@yscale).to_s + "cm));\n"
  }
end