Class: Grada::Graph
- Inherits:
-
Object
- Object
- Grada::Graph
- Defined in:
- lib/grada/graph.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}
- LEFT =
Graph offsets
0.05
- RIGHT =
0.05
- TOP =
0.05
- BOTTOM =
0.05
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
-
.hi ⇒ Object
Hello GraDA.
Instance Method Summary collapse
-
#display(opts = {}) ⇒ Object
Displays a graph in a X11 window.
-
#initialize(x, y = nil) ⇒ Graph
constructor
Initialize object with the data you want to plot.
-
#save(opts = {}) ⇒ Object
Save the graph in a png file.
Constructor Details
#initialize(x, y = nil) ⇒ Graph
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*
59 60 61 62 |
# File 'lib/grada/graph.rb', line 59 def initialize(x, y = nil) @x = validate(x) @y = y.nil? ? y : validate(y) end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
21 22 23 |
# File 'lib/grada/graph.rb', line 21 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
22 23 24 |
# File 'lib/grada/graph.rb', line 22 def y @y end |
Class Method Details
.hi ⇒ Object
Hello GraDA
42 43 44 |
# File 'lib/grada/graph.rb', line 42 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*
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 |
# File 'lib/grada/graph.rb', line 97 def display(opts = {}) @opts = DEFAULT_OPTIONS.merge(opts) if @opts[:graph_type] == :histogram population_data?(@x) return nil if @x.empty? Histogram.plot(@x, @opts) 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' HeatMap.plot(@x, @opts) 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? Default.plot(@x, @y, @opts) 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*
*html* (not valid for heatmaps)
*svg* => default (not valid for heatmaps)
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*
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 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/grada/graph.rb', line 141 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) return nil if @x.empty? return Histogram.plot_html(@x, @opts) if ext == 'html' Histogram.plot(@x, @opts) 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' ext = 'png' if ext == 'html' || ext == 'svg' HeatMap.plot(@x, @opts) do |plot| plot.output "#{@opts[:filename]}.#{ext}" plot.set "terminal #{ext} size #{@opts[:width]}, #{@opts[:height]} crop" end else raise NoPlotDataError if @y.nil? return Default.plot_html(@x, @y, @opts) if ext == 'html' Default.plot(@x, @y, @opts) 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 |