Class: Tplot::Chart
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
- #init_colors ⇒ Object
-
#initialize ⇒ Chart
constructor
A new instance of Chart.
- #plot ⇒ Object
- #pre_draw ⇒ Object
- #print_char(v) ⇒ Object
- #update(values) ⇒ Object
Constructor Details
#initialize ⇒ Chart
Returns a new instance of Chart.
16 17 18 19 20 |
# File 'lib/tplot/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/chart.rb', line 13 def labels @labels end |
#limit ⇒ Object
Returns the value of attribute limit.
14 15 16 |
# File 'lib/tplot/chart.rb', line 14 def limit @limit end |
Instance Method Details
#add(values) ⇒ Object
22 23 24 25 26 |
# File 'lib/tplot/chart.rb', line 22 def add(values) @datas.push(values) @datas.shift if @datas.size > limit draw end |
#calc_mapping_value(value) ⇒ Object
99 100 101 102 103 |
# File 'lib/tplot/chart.rb', line 99 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
105 106 107 108 109 |
# File 'lib/tplot/chart.rb', line 105 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/chart.rb', line 34 def draw pre_draw draw_frame_pre plot draw_frame_post setpos(0, 0) refresh end |
#draw_frame_post ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tplot/chart.rb', line 72 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tplot/chart.rb', line 61 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 |
#init_colors ⇒ Object
111 112 113 114 115 |
# File 'lib/tplot/chart.rb', line 111 def init_colors COLORS.each_with_index do |col,idx| init_pair(col, col, COLOR_BLACK) end end |
#plot ⇒ Object
57 58 59 |
# File 'lib/tplot/chart.rb', line 57 def plot # noop end |
#pre_draw ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tplot/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
91 92 93 94 95 96 97 |
# File 'lib/tplot/chart.rb', line 91 def print_char(v) if v.even? return "_" else return "-" end end |
#update(values) ⇒ Object
28 29 30 31 32 |
# File 'lib/tplot/chart.rb', line 28 def update(values) @datas.pop @datas.push(values) draw end |