Scaling numerical data to fit within a boundary.

C

CBlair1986

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>
 

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top