Class: ScbiPlot::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/scbi_plot/plot.rb

Direct Known Subclasses

Histogram, Lines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, title = nil) ⇒ Plot

Returns a new instance of Plot.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/scbi_plot/plot.rb', line 45

def initialize(file_name,title=nil)

  @x=[]
  @y=[]
  @x_label='x'
  @y_label='y'
  @y2_label='y'
  
  @x_range=''
  @y_range=''
  @y2_range=''

  @title=title
  @file_name=file_name

  if @title.nil?
    @title=file_name
  end

  @x_limit=20
  @line_width=1

end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def file_name
  @file_name
end

#line_widthObject

Returns the value of attribute line_width.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def line_width
  @line_width
end

#titleObject

Returns the value of attribute title.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def title
  @title
end

#xObject

Returns the value of attribute x.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def x
  @x
end

#x_labelObject

Returns the value of attribute x_label.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def x_label
  @x_label
end

#x_limitObject

Returns the value of attribute x_limit.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def x_limit
  @x_limit
end

#x_rangeObject

Returns the value of attribute x_range.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def x_range
  @x_range
end

#yObject

Returns the value of attribute y.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def y
  @y
end

#y2_labelObject

Returns the value of attribute y2_label.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def y2_label
  @y2_label
end

#y2_rangeObject

Returns the value of attribute y2_range.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def y2_range
  @y2_range
end

#y_labelObject

Returns the value of attribute y_label.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def y_label
  @y_label
end

#y_rangeObject

Returns the value of attribute y_range.



25
26
27
# File 'lib/scbi_plot/plot.rb', line 25

def y_range
  @y_range
end

Instance Method Details

#add_x(x) ⇒ Object



69
70
71
# File 'lib/scbi_plot/plot.rb', line 69

def add_x(x)
  @x=x
end

#add_xy(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/scbi_plot/plot.rb', line 78

def add_xy(data)
  @x=[]
  @y=[]

  if data.is_a?(Array)

    data.each do |e|
      @x.push e[0]
      @y.push e[1]
    end

  elsif data.is_a?(Hash)

    data.each do |k,v|
      @x.push k
      @y.push v
    end

  end
end

#add_y(y) ⇒ Object



73
74
75
# File 'lib/scbi_plot/plot.rb', line 73

def add_y(y)
  @y=y
end

#check_data_limitObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/scbi_plot/plot.rb', line 167

def check_data_limit
  # if more than 20 strings, then keep greater ones
  if @x.count>@x_limit
    h = xy_to_hash(@x,@y)

    @x=[]
    @y=[]

    @x_limit.times do
      ma=h.max_by{|k,v| v}
      if ma
        @x.push ma[0]
        @y.push ma[1]
        h.delete(ma[0])
      end
    end

  end

end

#contains_strings?(x) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/scbi_plot/plot.rb', line 226

def contains_strings?(x)
  contains_strings=false

  x.each do |v|
    begin
      r=Integer(v)
    rescue
      contains_strings=true
      break
    end
  end

  return contains_strings
end

#gnu_plot_installed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/scbi_plot/plot.rb', line 41

def gnu_plot_installed?
  return which('gnuplot')
end

#setup_dataObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/scbi_plot/plot.rb', line 188

def setup_data
  if @x.length != @y.length
    raise "Variables x and y have different sizes"

  else

    hash=xy_to_hash(@x,@y)
    # puts hash
    # sort integer data
    if !contains_strings?(@x)

      @x.sort!
      @y=[]

      @x.each do |v|
        @y.push hash[v].to_i
      end


    else # there are strings in X
      @x=[]
      @y=[]

      # save quoted values
      hash.keys.each do |v|
        # @x.push "\"#{v.gsub('\"','').gsub('\'','')}\""
        @x.push "#{v.gsub('\"','').gsub('\'','')}"
        # puts 
        @y.push hash[v.to_s]
      end

      # check if there is a lot of string data
      check_data_limit

    end
  end
end

#which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby


30
31
32
33
34
35
36
37
38
39
# File 'lib/scbi_plot/plot.rb', line 30

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end

#xy_to_hash(x, y) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/scbi_plot/plot.rb', line 156

def xy_to_hash(x,y)
  h={}

  x.each_with_index do |x1,i|
    h[x1]=y[i]
  end

  return h

end