Class: Grada

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

Defined Under Namespace

Classes: NoPlotDataError, NotValidArrayError, NotValidDataError

Constant Summary collapse

DEFAULT_OPTIONS =
{width: 1920,
height: 1080,
title: "Graph",
x_label: "X",
y_label: "Y",
with: 'lines',
graph_type: :default}
STYLES =

All styles you can specify for the plots

[:linestyle, :linetype, :linewidth, :linecolor, :pointtype, :pointsize, :fill]
LEFT =

Graph offsets

0.05
RIGHT =
0.05
TOP =
0.05
BOTTOM =
0.05

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y = nil) ⇒ Grada

Initialize object with the data you want to plot. It can vary depending on the type of graph. The second argument is optional.

Example:

>> radiation_levels_median_per_day = [0.001,0.01,1,10,1000]
>> radiation_days = [0,1,2,3,4]
>> grada = Grada.new(radiation_days, radiation_levels_median_per_day)
=> #<Grada:0x007f962a8dc9b8 @x=[0, 1, 2, 3, 4], @y=[0.001, 0.01, 1, 10, 1000]>

Arguments:

x: (Array)
y: (Array) *optional*


58
59
60
61
# File 'lib/grada.rb', line 58

def initialize(x, y = nil)
  @x = validate(x)
  @y = y.nil? ? y : validate(y)  
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



16
17
18
# File 'lib/grada.rb', line 16

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



17
18
19
# File 'lib/grada.rb', line 17

def y
  @y
end

Class Method Details

.hiObject

Hello GraDA



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

def self.hi
  puts "Hello GraDA"
end

Instance Method Details

#display(opts = {}) ⇒ Object

Displays a graph in a X11 window. You can specify all the options that you need: width (Integer) height (Integer) title (Integer) x_label (String) y_label (String) graph_type (:histogram, :heatmap) default: :default with (‘points’, ‘linespoints’) default: ‘lines’

Also is important to know that you can interact with the graph:

  • Zoom in => right click and drag the mouse to cover the area you want

    or 
    use the scroll wheel
    
  • Zoom out => press key ‘a’

    or
    if you want to go back to a previous state of zoom press key 'p'
    
  • Exit interactive mode => press key ‘q’

    or
    just close the window
    
  • Save image => working on it

Example:

>> grada.display
=> ""
>> grada.display({ title: 'Atomic Device X', x_label: 'Day', y_label: 'smSv', with: 'points' })
=> ""

Arguments:

opts: (Hash) *optional*


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
# File 'lib/grada.rb', line 96

def display(opts = {})
  @opts = DEFAULT_OPTIONS.merge(opts)
  
  if @opts[:graph_type] == :histogram
    population_data?(@x)
    
    plot_histogram do |plot|
      plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
      plot.set "offset graph #{LEFT},#{RIGHT},#{TOP},#{BOTTOM}"
    end
  elsif @opts[:graph_type] == :heatmap
    Matrix.columns(@x) rescue raise NoPlotDataError
    @opts[:with] = 'image'
    
    plot_heat_map do |plot|
      plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
      plot.set "offset graph #{LEFT},#{RIGHT},#{TOP},#{BOTTOM}"
    end
  else
    raise NoPlotDataError if @y.nil?
    
    plot_and do |plot|
      plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
      plot.set "offset graph #{LEFT},#{RIGHT},#{TOP},#{BOTTOM}"
    end
  end
end

#save(opts = {}) ⇒ Object

Save the graph in a png file. You can specify all the options that you need as display but also need to specify the file root-name and extension. The possible extensions you can use for saving a file are:

*png*
*gif*
*jpeg*
*svg* => default

Example:

>> grada.save({ filename: 'secret/radiation_levels/ffa/zonex/devicex/radiation_level_malaga', ext: 'png' ,title: 'Atomic Device X', x_label: 'Day', y_label: 'smSv', with: 'points' }) 
=> ""

Arguments:

opts: (Hash) *optional*


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/grada.rb', line 138

def save(opts = {})
  @opts = DEFAULT_OPTIONS.merge(opts)
  
  return nil if @opts[:filename].nil?

  ext = @opts[:ext] || 'svg'
  
  if @opts[:graph_type] == :histogram
    population_data?(@x)
    
    plot_histogram do |plot|
      plot.output "#{@opts[:filename]}.#{ext}" 
      plot.set "terminal #{ext} size #{@opts[:width]}, #{@opts[:height]} crop"
      plot.set "offset graph #{LEFT},#{RIGHT},#{TOP},#{BOTTOM}"
    end
  elsif @opts[:graph_type] == :heatmap
    Matrix.columns(@x) rescue raise NoPlotDataError
    @opts[:with] = 'image'
    
    plot_heat_map do |plot|
      plot.output "#{@opts[:filename]}.#{ext}" 
      plot.set "terminal #{ext} size #{@opts[:width]}, #{@opts[:height]} crop"
    end
  else
    raise NoPlotDataError if @y.nil?
    
    plot_and do |plot|
      plot.output "#{@opts[:filename]}.#{ext}" 
      plot.set "terminal #{ext} size #{@opts[:width]}, #{@opts[:height]} crop"
      plot.set "offset graph #{LEFT},#{RIGHT},#{TOP},#{BOTTOM}"
    end
  end
end