Class: TTY::Sparkline

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • data (Array<Numeric>) (defaults to: [])

    the data to chart

  • top (Integer) (defaults to: nil)

    the top position

  • left (Integer) (defaults to: nil)

    the left position

  • height (Integer) (defaults to: 1)

    the height in terminal lines

  • width (Integer) (defaults to: nil)

    the maximum width in terminal columns

  • min (Numeric) (defaults to: nil)

    the custom minimum value

  • max (Numeric) (defaults to: nil)

    the custom maximum value

  • bars (Array<String>) (defaults to: BARS)

    the bars used for display

  • buffer_size (Integer) (defaults to: MAX_BUFFER_SIZE)

    the maximum buffer size

  • non_numeric (Symbol) (defaults to: :empty)

    the replacement for a non-numeric value



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 = bars
  @num_of_bars = bars.size
  @buffer_size = buffer_size
  @non_numeric = non_numeric
  @filter = ->(value) { value.is_a?(::Numeric) }
  @cursor = TTY::Cursor
end

Instance Attribute Details

#cursorTTY::Cursor (readonly)

The drawing cursor

Returns:

  • (TTY::Cursor)


59
60
61
# File 'lib/tty/sparkline.rb', line 59

def cursor
  @cursor
end

#heightInteger (readonly)

The chart height in terminal lines

Returns:

  • (Integer)


52
53
54
# File 'lib/tty/sparkline.rb', line 52

def height
  @height
end

#leftInteger (readonly)

The left position

Returns:

  • (Integer)


38
39
40
# File 'lib/tty/sparkline.rb', line 38

def left
  @left
end

#maxNumeric

The custom maximum value used for scaling bars

Returns:

  • (Numeric)


73
74
75
# File 'lib/tty/sparkline.rb', line 73

def max
  @max
end

#minNumeric

The custom minimum value used for scaling bars

Returns:

  • (Numeric)


66
67
68
# File 'lib/tty/sparkline.rb', line 66

def min
  @min
end

#topInteger (readonly)

The top position

Returns:

  • (Integer)


31
32
33
# File 'lib/tty/sparkline.rb', line 31

def top
  @top
end

#widthInteger (readonly)

The chart maximum width in terminal columns

Returns:

  • (Integer)


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)

Examples:

sparkline.push(1, 2, 3, 4)
sparkline << 1 << 2 << 3 << 4

Parameters:

  • nums (Array<Numeric>)

Returns:

  • (self)


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

Examples:

sparkline.render

Parameters:

  • min (Integer) (defaults to: nil)

    the minimum value to display

  • max (Integer) (defaults to: nil)

    the maximum value to display

Returns:

  • (String)

    the rendered 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|
      bar_index = clamp_and_scale(value, calc_min, calc_max)
      bar = convert_to_bar(bar_index, height - 1 - y)
      bar = yield(value, bar, x, y) if block_given?
      buffer << bar
    end
    buffer << NEWLINE unless y == height - 1
  end

  buffer.join
end

#sizeInteger

The number of values

Returns:

  • (Integer)


153
154
155
# File 'lib/tty/sparkline.rb', line 153

def size
  @cached_data_size
end