Class: Plotty::Graph
- Inherits:
-
Object
- Object
- Plotty::Graph
- Defined in:
- lib/plotty/graph.rb
Class Method Summary collapse
Instance Method Summary collapse
- #generate_plot(name, offsets, force = false) ⇒ Object
- #generate_values(name) ⇒ Object
-
#initialize(x, y) ⇒ Graph
constructor
A new instance of Graph.
- #plot!(script = nil, name = "plot") ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(x, y) ⇒ Graph
Returns a new instance of Graph.
97 98 99 100 |
# File 'lib/plotty/graph.rb', line 97 def initialize(x, y) @x = x @y = y end |
Class Method Details
Instance Method Details
#generate_plot(name, offsets, force = false) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# 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) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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") ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# 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 ⇒ Object
109 110 111 |
# File 'lib/plotty/graph.rb', line 109 def size TTY::Screen.size.reverse end |