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: 1600,
height: 400,
title: "Graph",
x_label: "X",
y_label: "Y",
with: 'lines',
graph_type: :default}

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*


47
48
49
50
51
# File 'lib/grada.rb', line 47

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



30
31
32
# File 'lib/grada.rb', line 30

def self.hi
  puts "Hello GraDA"
end

Instance Method Details

#display(opts = {}) ⇒ Object

Displays a graph in a 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’

Example:

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

Arguments:

opts: (Hash) *optional*


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/grada.rb', line 72

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]}"
    end
  elsif @opts[:graph_type] == :heatmap
    Matrix.columns(@x) rescue raise NoPlotDataError
    @opts[:with] = 'image'
    
    plot_heat_map
  else
    raise NoPlotDataError if @y.nil?
    
    plot_and do |plot|
      plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
    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

Example:

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

Arguments:

opts: (Hash) *optional*


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

def save(opts = {})
  @opts = DEFAULT_OPTIONS.merge(opts)
  
  return nil if @opts[:filename].nil?
  
  if @opts[:graph_type] == :histogram
    population_data?(@x)
    
    plot_histogram do |plot|
      plot.output @opts[:filename]
      plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
      plot.terminal 'png'
    end
  elsif @opts[:graph_type] == :heatmap
    Matrix.columns(@x) rescue raise NoPlotDataError
    @opts[:with] = 'image'
    
    plot_heat_map do |plot|
      plot.output @opts[:filename]
      plot.terminal 'png'
    end
  else
    raise NoPlotDataError if @y.nil?
    
    plot_and do |plot|
      plot.output @opts[:filename]
      plot.set "terminal x11 size #{@opts[:width]*10},#{@opts[:height]}"
      plot.terminal 'png'
    end
  end
end