Class: AsciiChart::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_chart/line.rb

Constant Summary collapse

DEFAULTS =
{
  offset: 3,
  format: '%8.2f ',
  height: nil
}

Instance Method Summary collapse

Constructor Details

#initialize(series, options = {}) ⇒ Line

Returns a new instance of Line.



9
10
11
12
# File 'lib/ascii_chart/line.rb', line 9

def initialize(series, options = {})
  @series = series
  @options = DEFAULTS.merge(options)
end

Instance Method Details

#plotObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ascii_chart/line.rb', line 14

def plot
  max, min = @series.max, @series.min
  interval = (max - min).abs

  @options[:height] ||= interval
  radio = @options[:height].to_f / interval
  offset = @options[:offset]

  intmax, intmin = (max * radio).ceil, (min * radio).floor
  rows = (intmax - intmin).abs
  width = @series.length + offset

  result = (0..rows).map { [' '] * width }

  (intmin..intmax).each do |y|
    label = @options[:format] % (max - (((y - intmin) * interval).to_f / rows))
    result[y - intmin][[offset - label.length, 0].max] = label
    result[y - intmin][offset - 1] = y == 0 ? '' : ''
  end

  highest = (@series.first * radio - intmin).to_i
  result[rows - highest][offset - 1] = ''

  (0...@series.length - 1).each do |x|
    _curr = ((@series[x + 0] * radio).round - intmin).to_i
    _next = ((@series[x + 1] * radio).round - intmin).to_i

    if _curr == _next
      result[rows - _curr][x + offset] = '-'
    else
      result[rows - _curr][x + offset] = _curr > _next ? '' : ''
      result[rows - _next][x + offset] = _curr > _next ? '' : ''

      ([_curr, _next].min + 1...[_curr, _next].max).each do |y|
        result[rows - y][x + offset] = '|'
      end
    end
  end

  result.map(&:join).join("\n")
end