Class: PlotData

Inherits:
Object
  • Object
show all
Defined in:
lib/shunkuntype/plot_data.rb

Overview

gnuplotが扱えるデータフォーマットへの変換

Usage

data0 = PlotData.new(name0,0,3,name)
data0.add_general_data(name1, 0, 2)
start=Time.parse(Time.now.to_s)
x_func = proc{|x| ((Time.parse(x)-start)/3600/24) }
y_func = proc{|y| y.to_f/60.0 }
data0.mk_plot_data(x_func,y_func)
data0.sort
data0.sum_data

Instance Attribute Summary collapse

data read collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = "", x_col = nil, y_col = nil, title = "") ⇒ PlotData

Returns a new instance of PlotData.



20
21
22
23
24
# File 'lib/shunkuntype/plot_data.rb', line 20

def initialize(file_name="",x_col=nil,y_col=nil,title="")
  @data=[]
  read_general_data(file_name,x_col,y_col) if ""!=file_name
  @title=title if ""!=title
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



18
19
20
# File 'lib/shunkuntype/plot_data.rb', line 18

def data
  @data
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/shunkuntype/plot_data.rb', line 18

def title
  @title
end

Instance Method Details

#add_general_data(file, x_col, y_col) ⇒ Object



27
28
29
# File 'lib/shunkuntype/plot_data.rb', line 27

def add_general_data(file, x_col, y_col)
  read_general_data(file, x_col, y_col)
end

#mk_plot_data(x_func, y_func) ⇒ Object

x_func,y_funcに変換関数をyieldで入れる



57
58
59
60
61
62
63
64
65
# File 'lib/shunkuntype/plot_data.rb', line 57

def mk_plot_data(x_func,y_func)
  tmp_data=[]
  @data.each{|ele|
    x_data = x_func.call(ele[0])
    y_data = y_func.call(ele[1])
    tmp_data << [x_data,y_data]
  }
  @data = tmp_data
end

#read_general_data(file, x_col, y_col) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shunkuntype/plot_data.rb', line 31

def read_general_data(file, x_col, y_col)
  begin
    File.open(file,'r'){|file|
      while line=file.gets do
        tmp=line.chomp.split(',')
        @data << [tmp[x_col],tmp[y_col]]
      end
    }
  rescue => e
    p e
  end
end

#sortObject

x軸の値によってsortをかける.



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

def sort
  @data.sort!{|x,y| x[0] <=> y[0]}
end

#sum_dataObject

y軸の値の積分を取る



68
69
70
71
72
73
# File 'lib/shunkuntype/plot_data.rb', line 68

def sum_data
  tmp_data=[]
  y_data=0
  @data.each{|ele| tmp_data << [ele[0], y_data+=ele[1]]}
  @data = tmp_data
end

#to_gnuplotObject

gnuplot libraryに沿った形のdataを出力.列データを行データに



47
48
49
50
51
52
53
54
# File 'lib/shunkuntype/plot_data.rb', line 47

def to_gnuplot()
  x,y=[],[]
  @data.each{|ele|
    x << ele[0]
    y << ele[1]
  }
  return [x,y]
end