Class: TextChart::Designer

Inherits:
Object
  • Object
show all
Defined in:
lib/text_chart/designer.rb

Instance Method Summary collapse

Constructor Details

#initialize(text_chart, size_calc) ⇒ Designer

Returns a new instance of Designer.

Parameters:



5
6
7
8
9
# File 'lib/text_chart/designer.rb', line 5

def initialize(text_chart, size_calc)
  @text_chart = text_chart
  @size_calc = size_calc
  @chart_canvas = build_empty_chart
end

Instance Method Details

#draw_axisArray<String>

Returns:

  • (Array<String>)


22
23
24
25
26
27
28
# File 'lib/text_chart/designer.rb', line 22

def draw_axis
  draw_x_axis
  draw_y_axis
  draw_references

  @chart_canvas
end

#draw_barsArray<String>

Returns:

  • (Array<String>)


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
# File 'lib/text_chart/designer.rb', line 31

def draw_bars
  last_line_index = @chart_canvas.size - 1
  zero_line = last_line_index - @text_chart.size_config(:x_axis_height)
  chart_line = 0
  ref_width = @size_calc.calculate_reference_width
  y_axis_width = @text_chart.size_config(:y_axis_width)
  bar_margin = @text_chart.size_config(:bar_margin)
  bar_row = "###"
  bar_width = @text_chart.size_config(:bar_width)
  bar_start = bar_end = bar_top = 0
  height_of_bars = define_height_of_bars

  height_of_bars.each do |height|
    bar_start = if bar_start == 0
      ref_width + bar_margin + y_axis_width
    else
      # + 1 because bar_end put us on the bar last column
      bar_end + 1 + bar_margin
    end

    # - 1 because bar_start already put us on the bar first column
    bar_end = bar_start + bar_width - 1

    chart_line = zero_line
    bar_top = height - 1
    height.times do |t|
      @chart_canvas[chart_line][bar_start..bar_end] = bar_row

      chart_line -= 1 unless t == bar_top
    end
  end

  @chart_canvas
end

#draw_headerArray<String>

Returns:

  • (Array<String>)


12
13
14
15
16
17
18
19
# File 'lib/text_chart/designer.rb', line 12

def draw_header
  header = []
  header << "#{@text_chart.title}\n"
  header << "#{@text_chart.subtitle}\n"
  header << "\n"

  @chart_canvas.prepend(*header)
end

#paintArray<String>

Returns:

  • (Array<String>)


67
68
69
70
71
72
73
74
75
# File 'lib/text_chart/designer.rb', line 67

def paint
  @chart_canvas.map do |row|
    next if row.gsub!(@text_chart.title, colorize(@text_chart.title, :bold))

    row.gsub!(/-?\d/) { colorize($&, :cyan) }
    row.gsub!(/#+/) { colorize($&, :blue) }
  end
  @chart_canvas
end