Class: Plotty::Graph

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Graph

Returns a new instance of Graph.



92
93
94
95
# File 'lib/plotty/graph.rb', line 92

def initialize(x, y)
  @x = x
  @y = y
end

Class Method Details

.parse(x, y, commands) ⇒ Object



97
98
99
100
101
102
# File 'lib/plotty/graph.rb', line 97

def self.parse(x, y, commands)
  self.new(
    Sequence.parse(x),
    commands.collect{|command| Function.new(y, command)},
  )
end

Instance Method Details

#plot!(script = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/plotty/graph.rb', line 108

def plot!(script = nil)
  File.open("data.txt", "w") do |file|
    @x.each do |x|
      values = @y.collect do |function|
        function.call(x)
      end
      
      puts "#{x}: #{values.inspect}"
      file.puts "#{x} #{values.join(' ')}"
    end
  end
  
  plots = @y.collect.with_index{|function, index| "'data.txt' using 1:#{index+2} with lines title #{function.title.dump}"}
  
  system("gnuplot", "-e", "#{script}; plot #{plots.join(', ')}")
end

#sizeObject



104
105
106
# File 'lib/plotty/graph.rb', line 104

def size
  TTY::Screen.size.reverse
end