Class: Kovid::AsciiCharts::Cartesian

Inherits:
Chart
  • Object
show all
Defined in:
lib/kovid/ascii_charts.rb

Constant Summary

Constants inherited from Chart

Kovid::AsciiCharts::Chart::DEFAULT_MAX_Y_VALS, Kovid::AsciiCharts::Chart::DEFAULT_MIN_Y_VALS, Kovid::AsciiCharts::Chart::STEPS

Instance Attribute Summary

Attributes inherited from Chart

#data, #options

Instance Method Summary collapse

Methods inherited from Chart

#all_ints, #draw, #from_step, #initialize, #max_xval_width, #max_yval, #max_yval_width, #min_yval, #nearest_step, #next_step_down, #next_step_up, #round_value, #rounded_data, #scan_data, #scan_y_range, #step_size, #to_step, #to_string, #y_range

Constructor Details

This class inherits a constructor from Kovid::AsciiCharts::Chart

Instance Method Details

#linesObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 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