class Kovid::AsciiCharts::Cartesian

Public Instance Methods

lines() click to toggle source
# File lib/kovid/ascii_charts.rb, line 226
def lines
  return [[' ', options[:title], ' ', '|', '+-', ' ']] if data.empty?

  lines = [' ']

  bar_width = max_xval_width + 1

  lines << (' ' * max_yval_width) + ' ' + rounded_data.map { |pair| pair[0].to_s.center(bar_width) }.join('')

  y_range.each_with_index do |current_y, i|
    yval = current_y.to_s
    bar = if i == 0
            '+'
          else
            '|'
          end
    current_line = [(' ' * (max_yval_width - yval.length)) + "#{current_y}#{bar}"]

    rounded_data.each do |pair|
      marker = if (i == 0) && options[:hide_zero]
                 '-'
               else
                 '*'
               end
      filler = if i == 0
                 '-'
               else
                 ' '
               end
      comparison = if options[:bar]
                     current_y <= pair[1]
                   else
                     current_y == pair[1]
                   end
      current_line << if comparison
                        marker.center(bar_width, filler)
                      else
                        filler * bar_width
                      end
    end
    lines << current_line.join('')
    current_y += step_size
  end
  lines << ' '
  lines << options[:title].center(lines[1].length) if options[:title]
  lines << ' '
  lines.reverse
end