CBlair1986
4/23/2006 9:02:00 AM
I've coded the following to meet a need for a text-console based graph,
supporting negative numbers and such. I know it probably needs a lot of
improvement, but hey, cut me some slack, it's 5:00am, and I need to
head off to bed. :D
So, yeah, any suggestions and whatnot are greatly appreciated. Thank
you!
<code>
class DataView
def initialize( an_array )
@values = an_array # I will safely assume that it'll be an array
of Numerics...
end
def show( screen_width = 80 )
min_val = @values.min
max_val = @values.max
range = -min_val + max_val
ratio = screen_width / range.to_f
dataset = @values.collect{ |n| n * ratio }
pad_right = -min_val * ratio
result = dataset.collect do |n|
if n > 0
(' ' * pad_right)+( '|' + ('*' * n))
else
(('*' * -n).rjust(pad_right) + '|')
end
end
puts result.join("\n")
end
end
</code>