Class: AsciiChart

Inherits:
Object
  • Object
show all
Defined in:
lib/asciichart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#maxxObject (readonly)

Returns the value of attribute maxx.



4
5
6
# File 'lib/asciichart.rb', line 4

def maxx
  @maxx
end

#maxyObject (readonly)

Returns the value of attribute maxy.



4
5
6
# File 'lib/asciichart.rb', line 4

def maxy
  @maxy
end

#minyObject (readonly)

Returns the value of attribute miny.



4
5
6
# File 'lib/asciichart.rb', line 4

def miny
  @miny
end

#seriesObject (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_seriesObject



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

#renderObject



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