Class: AsciiCharts::Cartesian

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

Constant Summary

Constants inherited from Chart

AsciiCharts::Chart::DEFAULT_MAX_Y_VALS, AsciiCharts::Chart::DEFAULT_MIN_Y_VALS, AsciiCharts::Chart::INFINITY, 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, #series_to_points, #step_size, #to_step, #to_string, #y_range

Constructor Details

This class inherits a constructor from AsciiCharts::Chart

Instance Method Details

#linesObject



6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ascii_charts/cartesian.rb', line 6

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{|point| point[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 |point|
      def marker(series, i)
        if (0 == i) && options[:hide_zero]
          marker = '-'
        else
          if (options[:markers])
            marker = options[:markers][series]

            # unicode characters need to be treated as two-character strings for string.center() to work correctly
            if marker.length > 1
              marker += if 0 == i
                          '-'
                        else
                          ' '
                        end
            end
          else
            marker = '*'
          end
        end
        marker
      end

      filler = if 0 == i
                 '-'
               else
                 ' '
               end

      matching_series = false
      lowest_point = INFINITY
      (1..(point.length - 1)).each do |series|
        if self.options[:bar]
          if current_y <= point[series] && lowest_point > point[series]
            matching_series = series
            lowest_point = point[series]
          end
        else
          if current_y == point[series]
            matching_series = series
          end
        end
      end

      if matching_series
        current_line << marker(matching_series - 1, i).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