Class: TTY::Sparkline
- Inherits:
-
Object
- Object
- TTY::Sparkline
- Defined in:
- lib/tty/sparkline.rb,
lib/tty/sparkline/version.rb
Overview
Responsible for drawing sparkline in a terminal
Defined Under Namespace
Classes: Error
Constant Summary collapse
- BARS =
%w[▁ ▂ ▃ ▄ ▅ ▆ ▇ █].freeze
- EMPTY =
""
- MAX_BUFFER_SIZE =
2**14
- NEWLINE =
"\n"
- NON_NUMERIC_CONVERSIONS =
%i[empty ignore minimum].freeze
- SPACE =
" "
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#cursor ⇒ TTY::Cursor
readonly
The drawing cursor.
-
#height ⇒ Integer
readonly
The chart height in terminal lines.
-
#left ⇒ Integer
readonly
The left position.
-
#max ⇒ Numeric
The custom maximum value used for scaling bars.
-
#min ⇒ Numeric
The custom minimum value used for scaling bars.
-
#top ⇒ Integer
readonly
The top position.
-
#width ⇒ Integer
readonly
The chart maximum width in terminal columns.
Instance Method Summary collapse
-
#initialize(data = [], top: nil, left: nil, height: 1, width: nil, min: nil, max: nil, bars: BARS, buffer_size: MAX_BUFFER_SIZE, non_numeric: :empty) ⇒ Sparkline
constructor
Create a Sparkline instance.
-
#push(*nums) ⇒ self
(also: #append, #<<)
Append value(s).
-
#render(min: nil, max: nil) ⇒ String
Render data as a sparkline chart.
-
#size ⇒ Integer
The number of values.
Constructor Details
#initialize(data = [], top: nil, left: nil, height: 1, width: nil, min: nil, max: nil, bars: BARS, buffer_size: MAX_BUFFER_SIZE, non_numeric: :empty) ⇒ Sparkline
Create a Sparkline instance
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tty/sparkline.rb', line 99 def initialize(data = [], top: nil, left: nil, height: 1, width: nil, min: nil, max: nil, bars: BARS, buffer_size: MAX_BUFFER_SIZE, non_numeric: :empty) check_minmax(min, max) if min && max check_non_numeric(non_numeric) @data = Array(data).dup @cached_data_size = @data.size @top = top @left = left @height = height @width = width @min = min @max = max @bars = @num_of_bars = .size @buffer_size = buffer_size @non_numeric = non_numeric @filter = ->(value) { value.is_a?(::Numeric) } @cursor = TTY::Cursor end |
Instance Attribute Details
#cursor ⇒ TTY::Cursor (readonly)
The drawing cursor
59 60 61 |
# File 'lib/tty/sparkline.rb', line 59 def cursor @cursor end |
#height ⇒ Integer (readonly)
The chart height in terminal lines
52 53 54 |
# File 'lib/tty/sparkline.rb', line 52 def height @height end |
#left ⇒ Integer (readonly)
The left position
38 39 40 |
# File 'lib/tty/sparkline.rb', line 38 def left @left end |
#max ⇒ Numeric
The custom maximum value used for scaling bars
73 74 75 |
# File 'lib/tty/sparkline.rb', line 73 def max @max end |
#min ⇒ Numeric
The custom minimum value used for scaling bars
66 67 68 |
# File 'lib/tty/sparkline.rb', line 66 def min @min end |
#top ⇒ Integer (readonly)
The top position
31 32 33 |
# File 'lib/tty/sparkline.rb', line 31 def top @top end |
#width ⇒ Integer (readonly)
The chart maximum width in terminal columns
45 46 47 |
# File 'lib/tty/sparkline.rb', line 45 def width @width end |
Instance Method Details
#push(*nums) ⇒ self Also known as: append, <<
Append value(s)
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tty/sparkline.rb', line 134 def push(*nums) @data.push(*nums) @cached_data_size += nums.size if (overflow = @cached_data_size - @buffer_size) > 0 @data.shift(overflow) @cached_data_size -= overflow end self end |
#render(min: nil, max: nil) ⇒ String
Render data as a sparkline chart
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tty/sparkline.rb', line 171 def render(min: nil, max: nil) return EMPTY if @data.empty? buffer = [] calc_min, calc_max = data_minmax(min, max) check_minmax(calc_min, calc_max) height.times do |y| buffer << position(y) if position? @data[data_range].each.with_index do |value, x| = clamp_and_scale(value, calc_min, calc_max) = (, height - 1 - y) = yield(value, , x, y) if block_given? buffer << end buffer << NEWLINE unless y == height - 1 end buffer.join end |
#size ⇒ Integer
The number of values
153 154 155 |
# File 'lib/tty/sparkline.rb', line 153 def size @cached_data_size end |