class Graph

Public Class Methods

new(x, y) click to toggle source
# File lib/plotty/graph.rb, line 97
def initialize(x, y)
        @x = x
        @y = y
end
parse(x, y, commands) click to toggle source
# File lib/plotty/graph.rb, line 102
def self.parse(x, y, commands)
        self.new(
                Sequence.parse(x),
                commands.collect{|command| Function.parse(y, command)},
        )
end

Public Instance Methods

generate_plot(name, offsets, force = false) { |file| ... } click to toggle source
# File lib/plotty/graph.rb, line 132
def generate_plot(name, offsets, force = false)
        path = name + ".gp"
        
        if !File.exist?(path) or force
                File.open(path, "w") do |file|
                        file.puts('set datafile separator ","')
                        
                        yield file if block_given?
                        
                        file.write("plot ")
                        first = true
                        @y.collect.with_index do |function, index|
                                if first
                                        first = false
                                else
                                        file.write ','
                                end
                                
                                file.write "'#{name}.txt' using 1:#{offsets[index]} with lines title #{function.title.dump}"
                        end
                        
                        file.puts
                end
        end
        
        return path
end
generate_values(name) click to toggle source
# File lib/plotty/graph.rb, line 113
def generate_values(name)
        path = name + ".csv"
        values = nil
        
        File.open(path, "w") do |file|
                file.sync = true
                
                @x.each do |x|
                        values = @y.collect do |function|
                                function.call(x)
                        end
                        
                        file.puts "#{x}, #{values.flatten.join(', ')}"
                end
        end
        
        return values.map(&:count)
end
plot!(script = nil, name = "plot") click to toggle source
# File lib/plotty/graph.rb, line 160
def plot!(script = nil, name = "plot")
        counts = generate_values(name)
        offsets = [2]
        
        counts.each do |count|
                offsets << offsets.last + count
        end
        
        path = generate_plot(name, offsets) do |file|
                file.puts script if script
        end
        
        system("gnuplot", path)
end
size() click to toggle source
# File lib/plotty/graph.rb, line 109
def size
        TTY::Screen.size.reverse
end