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

Returns a new instance of Grada.

Raises:



24
25
26
27
28
29
# File 'lib/grada.rb', line 24

def initialize(x, y = nil)
  raise NoPlotDataError if ! y.nil? && x.size != y.size

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

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



8
9
10
# File 'lib/grada.rb', line 8

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



9
10
11
# File 'lib/grada.rb', line 9

def y
  @y
end

Class Method Details

.hiObject



20
21
22
# File 'lib/grada.rb', line 20

def self.hi
  puts "Hello GraDA"
end

Instance Method Details

#display(opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grada.rb', line 31

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



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

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