Class: Tplot::BarChart
- Inherits:
-
Object
- Object
- Tplot::BarChart
- Includes:
- Curses
- Defined in:
- lib/tplot/bar_chart.rb
Constant Summary collapse
- COLORS =
[COLOR_BLUE, COLOR_GREEN, COLOR_RED, COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE]
Instance Attribute Summary collapse
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#limit ⇒ Object
Returns the value of attribute limit.
Instance Method Summary collapse
- #add(values) ⇒ Object
- #calc_mapping_value(value) ⇒ Object
- #calc_position(value) ⇒ Object
- #draw ⇒ Object
- #draw_frame_post ⇒ Object
- #draw_frame_pre ⇒ Object
- #draw_line ⇒ Object
- #init_colors ⇒ Object
-
#initialize ⇒ BarChart
constructor
A new instance of BarChart.
- #pre_draw ⇒ Object
- #print_char(v) ⇒ Object
- #update(values) ⇒ Object
Constructor Details
#initialize ⇒ BarChart
Returns a new instance of BarChart.
16 17 18 19 20 |
# File 'lib/tplot/bar_chart.rb', line 16 def initialize @labels = [] @datas ||= [] @limit = 800 end |
Instance Attribute Details
#labels ⇒ Object
Returns the value of attribute labels.
13 14 15 |
# File 'lib/tplot/bar_chart.rb', line 13 def labels @labels end |
#limit ⇒ Object
Returns the value of attribute limit.
14 15 16 |
# File 'lib/tplot/bar_chart.rb', line 14 def limit @limit end |
Instance Method Details
#add(values) ⇒ Object
22 23 24 25 26 |
# File 'lib/tplot/bar_chart.rb', line 22 def add(values) @datas.push(values) @datas.shift if @datas.size > limit draw end |
#calc_mapping_value(value) ⇒ Object
90 91 92 93 94 |
# File 'lib/tplot/bar_chart.rb', line 90 def calc_mapping_value(value) range = (@max_value - @min_value).abs ratio = (value - @min_value).abs.to_f / range.to_f (@height * ratio * 2).to_i end |
#calc_position(value) ⇒ Object
96 97 98 99 100 |
# File 'lib/tplot/bar_chart.rb', line 96 def calc_position(value) range = (@max_value - @min_value).abs ratio = (value - @min_value).abs.to_f / range.to_f (@height * (1 - ratio)).to_i end |
#draw ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tplot/bar_chart.rb', line 34 def draw pre_draw draw_frame_pre draw_line draw_frame_post setpos(0, 0) refresh end |
#draw_frame_post ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tplot/bar_chart.rb', line 113 def draw_frame_post setpos(calc_position(@max_value), 0) addstr(@max_value.to_s) setpos(calc_position(0), 0) addstr(0.to_s) setpos(calc_position(@min_value), 0) addstr(@min_value.to_s) labels.each_with_index do |label, idx| setpos(idx + 2, 2) attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do addstr(label) end end end |
#draw_frame_pre ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tplot/bar_chart.rb', line 102 def draw_frame_pre setpos(calc_position(@max_value), 0) addstr(print_char(calc_mapping_value(@max_value)) * (@width + 1)) setpos(calc_position(0), 0) addstr(print_char(calc_mapping_value(0)) * (@width + 1)) setpos(calc_position(@min_value), 0) addstr(print_char(calc_mapping_value(@min_value)) * (@width + 1)) end |
#draw_line ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tplot/bar_chart.rb', line 57 def draw_line tmp_data = @datas.dup (-@width..0).map(&:abs).each do |col| data = tmp_data.pop break unless data data_with_idx = [] data.each_with_index do |value, idx| data_with_idx.push [value, idx] end data_with_idx.sort{|l, r| r.first.abs <=> l.first.abs }.each do |array| value, idx = array.first, array.last min = calc_position(0) max = calc_position(value) min, max = max, min if min > max (min..max).each do |line| setpos(line, col) attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do addstr(" ") end end end end end |
#init_colors ⇒ Object
132 133 134 135 136 |
# File 'lib/tplot/bar_chart.rb', line 132 def init_colors COLORS.each_with_index do |col,idx| init_pair(col, COLOR_BLACK, col) end end |
#pre_draw ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tplot/bar_chart.rb', line 45 def pre_draw clear start_color init_colors @min_value = (@datas.flatten.min < 0) ? @datas.flatten.min : 0 @max_value = (@datas.flatten.max < 0) ? 1 : @datas.flatten.max @height = lines - 1 @width = cols - 1 end |
#print_char(v) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/tplot/bar_chart.rb', line 82 def print_char(v) if v.even? return "_" else return "-" end end |
#update(values) ⇒ Object
28 29 30 31 32 |
# File 'lib/tplot/bar_chart.rb', line 28 def update(values) @datas.pop @datas.push(values) draw end |