Class: ScbiPlot::Lines

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

Instance Attribute Summary

Attributes inherited from Plot

#file_name, #line_width, #title, #x, #x_label, #x_limit, #x_range, #y, #y2_label, #y2_range, #y_label, #y_range

Instance Method Summary collapse

Methods inherited from Plot

#add_x, #add_xy, #add_y, #check_data_limit, #contains_strings?, #gnu_plot_installed?, #which, #xy_to_hash

Constructor Details

#initialize(file_name, title = nil) ⇒ Lines

Returns a new instance of Lines.



26
27
28
29
30
31
32
33
34
35
# File 'lib/scbi_plot/lines.rb', line 26

def initialize(file_name,title=nil)
  super
  @series=[]
  @line_width=2
  @show_leyend=true
  @vertical_lines=[]



end

Instance Method Details

#add_series(title, y_data, graph_type = 'lines', line_width = nil, axes = "x1y1") ⇒ Object



37
38
39
# File 'lib/scbi_plot/lines.rb', line 37

def add_series(title, y_data, graph_type ='lines',line_width=nil, axes="x1y1")
  @series << {:title => title, :data=> y_data, :graph_type=>graph_type, :line_width=>(line_width || @line_width), :axes=> axes}
end

#add_vertical_line(title, x_value) ⇒ Object



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

def add_vertical_line(title,x_value)
  @vertical_lines << {:title => title, :x_value=> x_value}
end

#do_graphObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/scbi_plot/lines.rb', line 45

def do_graph
  setup_data
  # $VERBOSE=true
  if !gnu_plot_installed?
    puts "GNUPLOT executable not installed"
    return
  end

  Gnuplot.open do |gp|
    # histogram
    Gnuplot::Plot.new( gp ) do |plot|

      if !title
        title=file_name
      end

      plot.title "#{@title}"
      plot.xlabel @x_label
      plot.ylabel @y_label
      
      if !@y2_label.empty?
        plot.y2label @y2_label
      end
      
      if @x_range.empty?
        plot.xrange "[#{@x.min}:#{@x.max}]"
        plot.x2range "[#{@x.min}:#{@x.max}]"
      else
        plot.xrange @x_range
        plot.x2range @x_range
      end
      
      if !@y_range.empty?
        plot.yrange @y_range
      end

      if !@y2_range.empty?
        
        plot.y2range @y2_range
        plot.set 'y2tics border'
      end

      
      # plot.x2range "auto"

      if !@show_leyend
        plot.set "key off" #leyend
      end

      # x values are integers
      if contains_strings?(@x)
        raise "Line plots cannot have strings in x axis"
      end

      # plot.style "fill pattern 22 border -1"
      # plot.set "boxwidth 0.2" # Probably 3-5.
      if !@vertical_lines.empty?
        x2ticks =[]
        @vertical_lines.each do |v_line|

          x2ticks << "'#{v_line[:title]} (#{v_line[:x_value]})' #{v_line[:x_value]}"
        end

        plot.set "x2tics (#{x2ticks.join(', ')})"
        plot.set 'grid noxtics x2tics lt rgb "green"'
      end

      @series.each do |series|
        plot.data << Gnuplot::DataSet.new( [@x, series[:data]] ) do |ds|

          ds.with =  series[:graph_type]
          ds.linewidth = series[:line_width]
          ds.title = series[:title]
          ds.axes=series[:axes]
        end

      end
      
      if !file_name.nil?
        plot.terminal "png size 800,600"
        plot.output "#{@file_name}"
      end
      
      
      
    end
  end
end

#setup_dataObject



136
137
138
139
140
141
142
# File 'lib/scbi_plot/lines.rb', line 136

def setup_data
  @series.each do |series|
    if @x.length != series[:data].length
      raise "Variables x and y have different sizes in serie #{series[:name]}"
    end
  end
end