Skip to content

Commit

Permalink
Change how the chart references are rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavothecoder committed Dec 23, 2023
1 parent 8301717 commit d4f915c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 112 deletions.
52 changes: 30 additions & 22 deletions lib/text_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@
# Goal: 3km in 16m
# AVG: 13m
#
# 20m |
# | 18m34s
# | ### 15m23s
# 10m | ### 10m09s ### 10m33s
# | ### ### 9m33s ### ###
# | ### ### ### ### ###
# 0m |...###.......###.......###.......###.......###...
# 18 |~~~###
# 17 | ###
# 16 | ###
# 15 |~~~###~~~~~~~~~~~~~~~~~~~~~~~~~~~###
# 14 | ### ###
# 13 | ### ###
# 12 | ### ###
# 11 | ### ###
# 10 |~~~###~~~~~~~###~~~~~~~~~~~~~~~~~###~~~~~~~###
# 9 |~~~###~~~~~~~###~~~~~~~### ### ###
# 8 | ### ### ### ### ###
# 7 | ### ### ### ### ###
# 6 | ### ### ### ### ###
# 5 | ### ### ### ### ###
# 4 | ### ### ### ### ###
# 3 | ### ### ### ### ###
# 2 | ### ### ### ### ###
# 1 | ### ### ### ### ###
# 0 | ### ### ### ### ###
# --------------------------------------------------
#
# Requirements:
Expand All @@ -34,28 +46,24 @@ def initialize(title, goal, data)
@title = title
@goal = goal
@data = data.empty? ? [0] : data
@size_calculator = SizeCalculator.new(@data)
@refs = define_references
@size_calculator = SizeCalculator.new(self)
@designer = Designer.new(self, @size_calculator)
end

attr_reader :title, :goal, :size_calculator, :designer

# @return [Array<Integer>]
def find_references
number_of_refs = @size_calculator.calculate_number_of_references
ref_offset = @size_calculator.calculate_reference_offset

refs = [@data.max]
(number_of_refs - 1).times do
refs << refs.last - ref_offset
end

refs
end
attr_reader :title, :goal, :refs, :data, :size_calculator, :designer

# @return [String]
def to_s
@designer.draw_axis
@designer.draw_header.join
end

private

def define_references
r = @data.sort.reverse
r << 0 unless r.include?(0)
r
end
end
17 changes: 6 additions & 11 deletions lib/text_chart/designer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def draw_axis
@chart_canvas
end

# TODO
# def draw_bars

private

def build_empty_chart
Expand Down Expand Up @@ -66,30 +69,22 @@ def draw_y_axis
end

def draw_references
references = @text_chart.find_references
references = @text_chart.refs
width = @size_calc.calculate_reference_width
number_of_references = references.size
ref_size = ref_start = ref_end = nil
margin_size = 1
row_offset = 2

number_of_references.times do |i|
if i.zero?
chart_row = i
else
chart_row = i + row_offset
row_offset += 2
end

ref_size = references[i].digits.size

if ref_size == (width - margin_size)
ref_start = 0
ref_end = ref_size - 1
@chart_canvas[chart_row][ref_start..ref_end] = references[i].to_s
@chart_canvas[i][ref_start..ref_end] = references[i].to_s
else
ref_start = ref_size
@chart_canvas[chart_row][ref_start] = references[i].to_s
@chart_canvas[i][ref_start] = references[i].to_s
end
end
end
Expand Down
28 changes: 7 additions & 21 deletions lib/text_chart/size_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

class TextChart::SizeCalculator
# @param [Array] data
def initialize(data)
@data = data
# @param [TextChart] text_chart
def initialize(text_chart)
@text_chart = text_chart
end

# @return [Integer]
Expand All @@ -22,11 +22,11 @@ def calculate_number_of_columns
result += left_margin + right_margin

bar_width = 3
result += @data.size * bar_width
result += @text_chart.data.size * bar_width

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

result
end
Expand All @@ -36,7 +36,7 @@ def calculate_number_of_columns
def calculate_reference_width
@reference_width ||=
begin
biggest_item = @data.max
biggest_item = @text_chart.data.max
biggest_item_width = biggest_item.digits.count
reference_margin = 1
biggest_item_width + reference_margin
Expand All @@ -53,13 +53,9 @@ def calculate_number_of_rows
result += x_axis_row

reference_row = 1
number_of_references = calculate_number_of_references
number_of_references = @text_chart.refs.size
number_of_references.times { result += reference_row }

reference_spacing = 2
# -1 to avoid spacing after the top reference.
(number_of_references - 1).times { result += reference_spacing }

result
end
end
Expand All @@ -77,14 +73,4 @@ def calculate_y_axis_size
calculate_number_of_rows - x_axis_row
end
end

# @return [Integer]
def calculate_number_of_references
@number_of_references ||= [(@data.size.to_f / 2.0).round, 1].max
end

# @return [Integer]
def calculate_reference_offset
@reference_offset ||= (@data.max + @data.min) / calculate_number_of_references
end
end
49 changes: 41 additions & 8 deletions test/text_chart/designer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,53 @@ class TextChart::DesignerTest < Test::Unit::TestCase

expected_small_sample = <<~END
10 |
|
|
9 |
8 |
|
|
7 |
6 |
|
|
5 |
4 |
|
|
3 |
2 |
1 |
0 |
----------------------------------------------------------------------------------------------------
END
assert_equal small_sample_result, expected_small_sample
end

# test "#draw_bars" do
# no_sample_designer = TextChart.new("No sample", "Testing", []).designer
# sorted_designer = TextChart.new("Sorted", "Testing", [*1..10]).designer
# random_order_designer = TextChart.new(
# "Random order", "Testing", [*1..10].shuffle(random: Random.new(1))
# ).designer

# no_sample_result = no_sample_designer.draw_bars.join
# sorted_result = sorted_designer.draw_bars.join
# random_order_result = random_order_designer.draw_bars.join

# expected_no_sample = <<~END

# END
# assert_equal no_sample_result, expected_no_sample

# expected_small_sample = <<~END
# 10 |
# | ###
# | ### ###
# | ### ### ###
# | ### ### ### ###
# | ### ### ### ### ###
# 5 | ### ### ### ### ### ###
# | ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ### ### ###
# 0 | ### ### ### ### ### ### ### ### ### ###
# ----------------------------------------------------------------------------------------------------
# END
# assert_equal small_sample_result, expected_small_sample
# end
end
55 changes: 20 additions & 35 deletions test/text_chart/size_calculator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ class TextChart::SizeCalculatorTest < Test::Unit::TestCase

# Example:
# 10 | 1
# | 2
# | 3
# 8 | 4
# | 5
# | 6
# 6 | 7
# | 8
# | 9
# 4 | 10
# | 11
# | 12
# 2 | 13
# ------- 14
assert_equal small_sample_result, 14
# 9 | 2
# 8 | 3
# 7 | 4
# 6 | 5
# 5 | 6
# 4 | 7
# 3 | 8
# 2 | 9
# 1 | 10
# 0 | 11
# ------- 12
assert_equal small_sample_result, 12
end

test "#calculate_number_of_columns" do
Expand Down Expand Up @@ -112,30 +110,17 @@ class TextChart::SizeCalculatorTest < Test::Unit::TestCase

# Example:
# 10 | y
# | y
# | y
# 9 | y
# 8 | y
# | y
# | y
# 7 | y
# 6 | y
# | y
# | y
# 5 | y
# 4 | y
# | y
# | y
# 2 | y = 13
# 3 | y
# 2 | y
# 1 | y
# 0 | y = 11
# ----------
assert_equal small_sample_result, 13
end

test "#calculate_reference_offset" do
no_sample = TextChart.new("", "", []).size_calculator
small_sample = TextChart.new("", "", [*1..10]).size_calculator

no_sample_result = no_sample.calculate_reference_offset
small_sample_result = small_sample.calculate_reference_offset

assert_equal no_sample_result, 0
assert_equal small_sample_result, 2
assert_equal small_sample_result, 11
end
end
18 changes: 3 additions & 15 deletions test/text_chart_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ class TextChartTest < Test::Unit::TestCase
end
end

test "#find_references" do
no_sample = TextChart.new("No sample", "Testing", [])
small_sample = TextChart.new("Small sample", "Testing", [*1..10])

no_sample_refs = no_sample.find_references
small_sample_refs = small_sample.find_references

assert_equal no_sample_refs, [0]
assert_equal small_sample_refs, [10, 8, 6, 4, 2]
end

test "#to_s" do
no_sample = TextChart.new("No sample", "Testing", [])
small_sample = TextChart.new("Small sample", "Testing", [*1..5])
Expand All @@ -39,12 +28,11 @@ class TextChartTest < Test::Unit::TestCase
Goal: Testing
5 |
|
|
4 |
3 |
|
|
2 |
1 |
0 |
--------------------------------------------------
EXPECTED
end
Expand Down

0 comments on commit d4f915c

Please sign in to comment.