Class: AsciiChart
- Inherits:
-
Object
- Object
- AsciiChart
- Defined in:
- lib/asciichart.rb
Instance Attribute Summary collapse
-
#maxx ⇒ Object
readonly
Returns the value of attribute maxx.
-
#maxy ⇒ Object
readonly
Returns the value of attribute maxy.
-
#miny ⇒ Object
readonly
Returns the value of attribute miny.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
Instance Method Summary collapse
- #add_series(series, name = nil) ⇒ Object
- #get_ascii_char(series, time, value_range) ⇒ Object
-
#initialize(rows = 20) ⇒ AsciiChart
constructor
A new instance of AsciiChart.
- #num_series ⇒ Object
- #pick_colour(index) ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(rows = 20) ⇒ AsciiChart
Returns a new instance of AsciiChart.
6 7 8 9 10 11 12 13 |
# File 'lib/asciichart.rb', line 6 def initialize(rows=20) @rows = rows @series = [] @maxx = 0 @miny = 0 @maxy = 0 @@colours = %w{red green blue}.map(&:to_sym) end |
Instance Attribute Details
#maxx ⇒ Object (readonly)
Returns the value of attribute maxx.
4 5 6 |
# File 'lib/asciichart.rb', line 4 def maxx @maxx end |
#maxy ⇒ Object (readonly)
Returns the value of attribute maxy.
4 5 6 |
# File 'lib/asciichart.rb', line 4 def maxy @maxy end |
#miny ⇒ Object (readonly)
Returns the value of attribute miny.
4 5 6 |
# File 'lib/asciichart.rb', line 4 def miny @miny end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
4 5 6 |
# File 'lib/asciichart.rb', line 4 def series @series end |
Instance Method Details
#add_series(series, name = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/asciichart.rb', line 15 def add_series(series, name=nil) @series << { values: series, colour: pick_colour(@series.length), name: name ? name.capitalize : "Series #{@series.length}" } @miny = [series.min, @miny].min @maxy = [series.max, @maxy].max @maxx = [series.length, @maxx].max end |
#get_ascii_char(series, time, value_range) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/asciichart.rb', line 64 def get_ascii_char(series, time, value_range) # returns an ascii character to represent the value-range for the given series at the given time # if the series does not have a high enough value at that time, we return ' ' value = series[:values][time] if value and value >= value_range.max.to_f c = '#' elsif value and value > value_range.min.to_f c = '.' else c = ' ' end c.send(series[:colour]) end |
#num_series ⇒ Object
26 27 28 |
# File 'lib/asciichart.rb', line 26 def num_series @series.length end |
#pick_colour(index) ⇒ Object
78 79 80 |
# File 'lib/asciichart.rb', line 78 def pick_colour(index) @@colours[index % @@colours.length] end |
#render ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/asciichart.rb', line 30 def render # we need one column for each series up the maximum # length of any one series, plus one for padding width = ((num_series + 2) * @maxx) + 2 step = (@maxy - @miny) / @rows.to_f y_top = @maxy.to_f bitmap = '-' * (width + 2) bitmap += "\r\n|" + (' ' * width) + '|' @rows.times do bitmap += "\r\n| " # loop on each distinct time/x value @maxx.times do |x| @series.each do |s| bitmap += get_ascii_char(s, x, (y_top - step)..y_top) end bitmap += ' ' end bitmap += '|' y_top = (y_top - step).round(2) end bitmap += "\r\n" + ('-' * (width + 2)) # the legend bitmap += "\r\n" @series.each do |s| bitmap += "\r\n" + s[:name].send(s[:colour]) end puts bitmap end |