From 8d75169cb8c049cb33654220e4e46f8e5989fa36 Mon Sep 17 00:00:00 2001 From: Gustavo Ribeiro Date: Mon, 1 Jan 2024 15:45:05 -0300 Subject: [PATCH] Improve reference generation --- lib/text_chart.rb | 1 - lib/text_chart/designer.rb | 8 +++++++- lib/text_chart/size_calculator.rb | 15 --------------- test/text_chart/designer_test.rb | 5 ----- test/text_chart/size_calculator_test.rb | 21 ++++----------------- test/text_chart_test.rb | 22 ++++++++-------------- 6 files changed, 19 insertions(+), 53 deletions(-) diff --git a/lib/text_chart.rb b/lib/text_chart.rb index 7f24b71..bb63daf 100644 --- a/lib/text_chart.rb +++ b/lib/text_chart.rb @@ -71,7 +71,6 @@ def size_config(key) def define_references r = [*@data.min..@data.max].reverse - r << 0 unless r.include?(0) r.map(&:to_s) end end diff --git a/lib/text_chart/designer.rb b/lib/text_chart/designer.rb index e6ac01f..eb16bb5 100644 --- a/lib/text_chart/designer.rb +++ b/lib/text_chart/designer.rb @@ -38,8 +38,9 @@ def draw_bars bar_row = "###" bar_width = @text_chart.size_config(:bar_width) bar_start = bar_end = bar_top = 0 + height_of_bars = define_height_of_bars - @size_calc.calculate_height_of_bars.each do |height| + height_of_bars.each do |height| bar_start = if bar_start == 0 ref_width + bar_margin + y_axis_width else @@ -157,4 +158,9 @@ def colorize(str, format) "\e[34m#{str}\e[0m" end end + + # @return height of each sample item [Array] + def define_height_of_bars + @text_chart.data.map { |i| @text_chart.refs.size - @text_chart.refs.index(i.to_s) } + end end diff --git a/lib/text_chart/size_calculator.rb b/lib/text_chart/size_calculator.rb index ca87ce6..ba6b32e 100644 --- a/lib/text_chart/size_calculator.rb +++ b/lib/text_chart/size_calculator.rb @@ -72,19 +72,4 @@ def calculate_y_axis_size calculate_number_of_rows - x_axis_row end end - - # @return [Array] the height of each sample item - def calculate_height_of_bars - @height_of_bars ||= - begin - offset = if @text_chart.data.any?(&:negative?) - @text_chart.data.min.abs - else - 0 - end - # + 1 to guarantee that the bar will always be renderde - offset += 1 - @text_chart.data.map { |i| i + offset } - end - end end diff --git a/test/text_chart/designer_test.rb b/test/text_chart/designer_test.rb index d2f5e27..56d8bdd 100644 --- a/test/text_chart/designer_test.rb +++ b/test/text_chart/designer_test.rb @@ -44,7 +44,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase 3 | 2 | 1 | - 0 | ---------------------------------------------------------------- END @@ -59,7 +58,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase 3 | 2 | 1 | - 0 | ---------------------- END @@ -107,7 +105,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase '''''''''''''''### ### ### ### ### ### ### ### '''''''''### ### ### ### ### ### ### ### ### '''### ### ### ### ### ### ### ### ### ### - ### ### ### ### ### ### ### ### ### ### END @@ -122,7 +119,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase '''### ### ### ### ### ### ### ### '''###'''###'''###'''###'''''''''###'''### ### ### ### '''###'''###'''###'''###'''### ### ### ### ### ### - ### ### ### ### ### ### ### ### ### ### END @@ -149,7 +145,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase '''### ### ### '''###'''### ### ### '''###'''###'''###'''### ### - ### ### ### ### ### END diff --git a/test/text_chart/size_calculator_test.rb b/test/text_chart/size_calculator_test.rb index d636880..7fc3353 100644 --- a/test/text_chart/size_calculator_test.rb +++ b/test/text_chart/size_calculator_test.rb @@ -46,9 +46,8 @@ class TextChart::SizeCalculatorTest < Test::Unit::TestCase # 3 | 8 # 2 | 9 # 1 | 10 - # 0 | 11 - # ------- 12 - assert_equal small_sample_result, 12 + # ------- 11 + assert_equal small_sample_result, 11 end test "#calculate_number_of_columns" do @@ -121,20 +120,8 @@ class TextChart::SizeCalculatorTest < Test::Unit::TestCase # 4 | y # 3 | y # 2 | y - # 1 | y - # 0 | y = 11 + # 1 | y = 10 # ---------- - assert_equal small_sample_result, 11 - end - - test "#calculate_height_of_bars" do - no_sample = TextChart.new("", "", []).size_calculator - with_sample = TextChart.new("", "", [*-3..3]).size_calculator - - no_sample_result = no_sample.calculate_height_of_bars - with_sample = with_sample.calculate_height_of_bars - - assert_equal no_sample_result, [1] - assert_equal with_sample, [1, 2, 3, 4, 5, 6, 7] + assert_equal small_sample_result, 10 end end diff --git a/test/text_chart_test.rb b/test/text_chart_test.rb index b982ba3..05e79bb 100644 --- a/test/text_chart_test.rb +++ b/test/text_chart_test.rb @@ -11,7 +11,7 @@ class TextChartTest < Test::Unit::TestCase test "#to_s" do no_sample = TextChart.new("No sample", "Testing", []) - sorted_sample = TextChart.new("Sorted sample", "Testing", [*1..10]) + sorted_sample = TextChart.new("Sorted sample", "Testing", [*5..10]) random_order_sample = TextChart.new( "Random order sample", "Testing", [*1..10].shuffle(random: Random.new(1)) ) @@ -43,18 +43,13 @@ class TextChartTest < Test::Unit::TestCase Sorted sample Testing - 10 |'''''''''''''''''''''''''''''''''''''''''''''''''''''''''### - 9 |'''''''''''''''''''''''''''''''''''''''''''''''''''### ### - 8 |'''''''''''''''''''''''''''''''''''''''''''''### ### ### - 7 |'''''''''''''''''''''''''''''''''''''''### ### ### ### - 6 |'''''''''''''''''''''''''''''''''### ### ### ### ### - 5 |'''''''''''''''''''''''''''### ### ### ### ### ### - 4 |'''''''''''''''''''''### ### ### ### ### ### ### - 3 |'''''''''''''''### ### ### ### ### ### ### ### - 2 |'''''''''### ### ### ### ### ### ### ### ### - 1 |'''### ### ### ### ### ### ### ### ### ### - 0 | ### ### ### ### ### ### ### ### ### ### - ---------------------------------------------------------------- + 10 |'''''''''''''''''''''''''''''''''### + 9 |'''''''''''''''''''''''''''### ### + 8 |'''''''''''''''''''''### ### ### + 7 |'''''''''''''''### ### ### ### + 6 |'''''''''### ### ### ### ### + 5 |'''### ### ### ### ### ### + ---------------------------------------- EXPECTED assert_equal random_order_result, <<~EXPECTED Random order sample @@ -70,7 +65,6 @@ class TextChartTest < Test::Unit::TestCase 3 |'''### ### ### ### ### ### ### ### 2 |'''###'''###'''###'''###'''''''''###'''### ### ### ### 1 |'''###'''###'''###'''###'''### ### ### ### ### ### - 0 | ### ### ### ### ### ### ### ### ### ### ---------------------------------------------------------------- EXPECTED assert_equal duplicated_and_gaps_result, <<~EXPECTED