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.



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

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

Class Method Details

.parse(x, y, commands) ⇒ Object



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

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

Instance Method Details

#generate_plot(path = "plot.gp") ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/plotty/graph.rb', line 124

def generate_plot(path = "plot.gp")
	File.open(path, "w") do |file|
		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 "'data.txt' using 1:#{index+2} with lines title #{function.title.dump}"
		end
		
		file.puts
		
		# file.puts "pause 1"
		# file.puts "reread"
	end
	
	return path
end

#generate_valuesObject



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

def generate_values
	File.open("data.txt", "w") do |file|
		file.sync = true
		
		@x.each do |x|
			values = @y.collect do |function|
				function.call(x)
			end
			
			# puts "#{x}: #{values.inspect}"
			file.puts "#{x} #{values.join(' ')}"
		end
	end
end

#plot!(script = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/plotty/graph.rb', line 149

def plot!(script = nil)
	generate_values
	
	path = generate_plot do |file|
		file.puts script if script
	end
	
	system("gnuplot", path)
end

#sizeObject



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

def size
	TTY::Screen.size.reverse
end