Class: AsciiCharts::Cartesian

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

Constant Summary

Constants inherited from Chart

AsciiCharts::Chart::DEFAULT_MAX_Y_VALS, AsciiCharts::Chart::DEFAULT_MIN_Y_VALS, 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 AsciiCharts::Chart

Instance Method Details

#linesObject



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
274
275
276
277
278
# File 'lib/ascii_charts.rb', line 227

def lines
  if self.data.size == 0
    return [[' ', self.options[:title], ' ', '|', '+-', ' ']] 
  end

  lines = [' ']
  
  bar_width = self.max_xval_width + 1

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

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