Class: Tplot::LineChart
- Inherits:
-
Object
- Object
- Tplot::LineChart
- Includes:
- Curses
- Defined in:
- lib/tplot/line_chart.rb
Direct Known Subclasses
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_position(value) ⇒ Object
- #draw ⇒ Object
- #draw_frame_post ⇒ Object
- #draw_frame_pre ⇒ Object
- #draw_line ⇒ Object
- #init_colors ⇒ Object
-
#initialize ⇒ LineChart
constructor
A new instance of LineChart.
- #pre_draw ⇒ Object
- #update(values) ⇒ Object
Constructor Details
#initialize ⇒ LineChart
Returns a new instance of LineChart.
16 17 18 19 20 |
# File 'lib/tplot/line_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/line_chart.rb', line 13 def labels @labels end |
#limit ⇒ Object
Returns the value of attribute limit.
14 15 16 |
# File 'lib/tplot/line_chart.rb', line 14 def limit @limit end |
Instance Method Details
#add(values) ⇒ Object
22 23 24 25 |
# File 'lib/tplot/line_chart.rb', line 22 def add(values) @datas.push(values) @datas.shift if @datas.size > limit end |
#calc_position(value) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/tplot/line_chart.rb', line 69 def calc_position(value) range = (@max_value - @min_value).abs return 0 if range == 0 ratio = (value - @min_value).abs.to_f / range.to_f (@height * (1 - ratio)).to_i end |
#draw ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tplot/line_chart.rb', line 32 def draw pre_draw draw_frame_pre draw_line draw_frame_post setpos(0, 0) refresh end |
#draw_frame_post ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tplot/line_chart.rb', line 87 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
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tplot/line_chart.rb', line 76 def draw_frame_pre setpos(calc_position(@max_value), 0) addstr("-" * (@width + 1)) setpos(calc_position(0), 0) addstr("-" * (@width + 1)) setpos(calc_position(@min_value), 0) addstr("-" * (@width + 1)) end |
#draw_line ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/tplot/line_chart.rb', line 55 def draw_line tmp_data = @datas.dup (-@width..0).map(&:abs).each do |col| data = tmp_data.pop break unless data data.each_with_index do |value, idx| setpos(calc_position(value), col) attron(color_pair(COLORS[idx % COLORS.size])|A_NORMAL) do addstr("*") end end end end |
#init_colors ⇒ Object
106 107 108 109 110 |
# File 'lib/tplot/line_chart.rb', line 106 def init_colors COLORS.each_with_index do |col,idx| init_pair(col, col, COLOR_BLACK) end end |
#pre_draw ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tplot/line_chart.rb', line 43 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 |
#update(values) ⇒ Object
27 28 29 30 |
# File 'lib/tplot/line_chart.rb', line 27 def update(values) @datas.pop @datas.push(values) end |