class Rubinius::Grapher::AsciiGraph
Attributes
title[R]
Public Class Methods
new(x, y, title, data)
click to toggle source
# File lib/rubinius/grapher.rb, line 8 def initialize(x, y, title, data) @x = x @y = y @title = title @data = data end
Public Instance Methods
canvas()
click to toggle source
# File lib/rubinius/grapher.rb, line 23 def canvas @canvas ||= (@y).times.to_a.map { Array.new @x, " " } end
draw_axes()
click to toggle source
# File lib/rubinius/grapher.rb, line 27 def draw_axes @x_axis = @y - 2 @y_axis = 4 (1..@y-3).each { |i| canvas[i][@y_axis] = "|" } (2..@x-2).each { |i| canvas[@x_axis][i] = "-" } canvas[@x_axis][@y_axis - 1] = "-" canvas[@x_axis][@y_axis] = "+" canvas[@x_axis + 1][@y_axis] = "|" end
draw_data()
click to toggle source
# File lib/rubinius/grapher.rb, line 43 def draw_data min = @data.min max = @data.max graph_width = @x - @x_axis - 3 width = @data.size if width > graph_width columns = (0..graph_width).to_a else r = width.to_f / graph_width columns = Array.new(graph_width) { |i| (i * r).to_i } end graph_y = @y - 3 graph_height = @y - 5 height = max + 1 - min r = graph_height.to_f / height columns.each do |c| y = graph_y - (@data[c] - min) * r x = c + @y_axis + 2 @canvas[y][x] = "." end end
draw_title()
click to toggle source
# File lib/rubinius/grapher.rb, line 39 def draw_title canvas[@y] = @title.center(@x).split("") end
plot()
click to toggle source
# File lib/rubinius/grapher.rb, line 15 def plot draw_axes draw_title draw_data self end
to_s()
click to toggle source
# File lib/rubinius/grapher.rb, line 70 def to_s canvas.map { |r| r.join << "\n" }.join end