Scaling numerical data to fit within a boundary.

Discussion in 'Ruby' started by CBlair1986, Apr 23, 2006.

  1. CBlair1986

    CBlair1986 Guest

    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>
    CBlair1986, Apr 23, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Epson Barnett
    Replies:
    1
    Views:
    321
    Epson Barnett
    Apr 21, 2004
  2. Pascal Steiss
    Replies:
    1
    Views:
    3,379
    Toby Inkster
    Mar 19, 2005
  3. jjouett
    Replies:
    2
    Views:
    1,902
    jjouett
    May 1, 2006
  4. Piet
    Replies:
    0
    Views:
    510
  5. Thierry Lam
    Replies:
    0
    Views:
    335
    Thierry Lam
    Jun 13, 2007
Loading...

Share This Page