Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Scaling numerical data to fit within a boundary.

Thread Tools

Scaling numerical data to fit within a boundary.

Posts: n/a
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.

So, yeah, any suggestions and whatnot are greatly appreciated. Thank

class DataView
def initialize( an_array )
@values = an_array # I will safely assume that it'll be an array
of Numerics...

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))
(('*' * -n).rjust(pad_right) + '|')

puts result.join("\n")

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Centering and scaling divs to fit any browser tcarone07 General Computer Support 0 02-06-2009 07:36 AM
Scaling data FPGA VHDL 2 02-03-2008 06:19 PM
Textbox width scaling to width of data not width of page? AndrewF ASP .Net 1 10-10-2005 04:38 PM
Make wxListCtrl fit around contents and parent frame fit around listctrl Piet Python 0 07-18-2004 08:27 AM
How to manage solutions that don't fit within the ASP.NET page lifecycle Epson Barnett ASP .Net 1 04-21-2004 02:27 PM