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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scbi_plot/plot.rb', line 27

def initialize(file_name,title=nil)

  @x=[]
  @y=[]
  @x_label='x'
  @y_label='y'
  
  @x_range=''
  @y_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

#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



49
50
51
# File 'lib/scbi_plot/plot.rb', line 49

def add_x(x)
  @x=x
end

#add_xy(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/scbi_plot/plot.rb', line 58

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



53
54
55
# File 'lib/scbi_plot/plot.rb', line 53

def add_y(y)
  @y=y
end

#check_data_limitObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/scbi_plot/plot.rb', line 147

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)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/scbi_plot/plot.rb', line 206

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

#setup_dataObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/scbi_plot/plot.rb', line 168

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

#xy_to_hash(x, y) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/scbi_plot/plot.rb', line 136

def xy_to_hash(x,y)
  h={}

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

  return h

end