Class: Plotty::Graph
- Inherits:
-
Object
- Object
- Plotty::Graph
- Defined in:
- lib/plotty/graph.rb
Class Method Summary collapse
Instance Method Summary collapse
- #generate_plot(path = "plot.gp") ⇒ Object
- #generate_values ⇒ Object
-
#initialize(x, y) ⇒ Graph
constructor
A new instance of Graph.
- #plot!(script = nil) ⇒ Object
- #size ⇒ Object
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
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_values ⇒ Object
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 |
#size ⇒ Object
105 106 107 |
# File 'lib/plotty/graph.rb', line 105 def size TTY::Screen.size.reverse end |