Class: TextChart::SizeCalculator

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

Instance Method Summary collapse

Constructor Details

#initialize(text_chart) ⇒ SizeCalculator

Returns a new instance of SizeCalculator.

Parameters:



5
6
7
# File 'lib/text_chart/size_calculator.rb', line 5

def initialize(text_chart)
  @text_chart = text_chart
end

Instance Method Details

#calculate_bar_height(bar) ⇒ Object

Returns the number of rows that we should render [Integer].

Parameters:

  • bar (Integer)

Returns:

  • the number of rows that we should render [Integer]



63
64
65
66
67
68
# File 'lib/text_chart/size_calculator.rb', line 63

def calculate_bar_height(bar)
  return 0 if bar.zero?

  min_bar_height = 1
  (calculate_increase_percentile(@text_chart.data.min, bar) / bar_height_limiter).round + min_bar_height
end

#calculate_number_of_columnsInteger

Returns:

  • (Integer)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/text_chart/size_calculator.rb', line 10

def calculate_number_of_columns
  @number_of_columns ||=
    begin
      result = 0

      result += calculate_reference_width

      y_axis_width = @text_chart.size_config(:y_axis_width)
      result += y_axis_width

      left_margin = @text_chart.size_config(:bar_margin)
      right_margin = @text_chart.size_config(:bar_margin)
      result += left_margin + right_margin

      bar_width = @text_chart.size_config(:bar_width)
      result += @text_chart.data.size * bar_width

      bar_spacing = @text_chart.size_config(:bar_margin)
      # -1 to avoid adding spacing after the last bar.
      result += (@text_chart.data.size - 1) * bar_spacing

      result
    end
end

#calculate_number_of_rowsInteger

Returns:

  • (Integer)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/text_chart/size_calculator.rb', line 45

def calculate_number_of_rows
  @number_of_rows ||=
    begin
      result = 0

      x_axis_row = @text_chart.size_config(:x_axis_height)
      result += x_axis_row

      calculate_bar_height(@text_chart.data.max).times do
        result += @text_chart.size_config(:bar_row_height)
      end

      result
    end
end

#calculate_reference_widthInteger

Returns:

  • (Integer)


36
37
38
39
40
41
42
# File 'lib/text_chart/size_calculator.rb', line 36

def calculate_reference_width
  @reference_width ||=
    begin
      biggest_number_size = @text_chart.data.map(&:to_s).map(&:size).max
      biggest_number_size
    end
end

#calculate_x_axis_sizeInteger

Returns:

  • (Integer)


71
72
73
# File 'lib/text_chart/size_calculator.rb', line 71

def calculate_x_axis_size
  @x_axis_size ||= calculate_number_of_columns - calculate_reference_width
end

#calculate_y_axis_sizeInteger

Returns:

  • (Integer)


76
77
78
79
80
81
82
# File 'lib/text_chart/size_calculator.rb', line 76

def calculate_y_axis_size
  @y_axis_size ||=
    begin
      x_axis_row = @text_chart.size_config(:x_axis_height)
      calculate_number_of_rows - x_axis_row
    end
end