Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/text_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/text_chart/designer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,4 +158,9 @@ def colorize(str, format)
"\e[34m#{str}\e[0m"
end
end

# @return height of each sample item [Array<Integer>]
def define_height_of_bars
@text_chart.data.map { |i| @text_chart.refs.size - @text_chart.refs.index(i.to_s) }
end
end
15 changes: 0 additions & 15 deletions lib/text_chart/size_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,4 @@ def calculate_y_axis_size
calculate_number_of_rows - x_axis_row
end
end

# @return [Array<Integer>] 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
5 changes: 0 additions & 5 deletions test/text_chart/designer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
3 |
2 |
1 |
0 |
----------------------------------------------------------------
END

Expand All @@ -59,7 +58,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
3 |
2 |
1 |
0 |
----------------------
END

Expand Down Expand Up @@ -107,7 +105,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
'''''''''''''''### ### ### ### ### ### ### ###
'''''''''### ### ### ### ### ### ### ### ###
'''### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###

END

Expand All @@ -122,7 +119,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
'''### ### ### ### ### ### ### ###
'''###'''###'''###'''###'''''''''###'''### ### ### ###
'''###'''###'''###'''###'''### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###

END

Expand All @@ -149,7 +145,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
'''### ### ###
'''###'''### ### ###
'''###'''###'''###'''### ###
### ### ### ### ###

END

Expand Down
21 changes: 4 additions & 17 deletions test/text_chart/size_calculator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
22 changes: 8 additions & 14 deletions test/text_chart_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
Expand Down Expand Up @@ -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
Expand All @@ -70,7 +65,6 @@ class TextChartTest < Test::Unit::TestCase
3 |'''### ### ### ### ### ### ### ###
2 |'''###'''###'''###'''###'''''''''###'''### ### ### ###
1 |'''###'''###'''###'''###'''### ### ### ### ### ###
0 | ### ### ### ### ### ### ### ### ### ###
----------------------------------------------------------------
EXPECTED
assert_equal duplicated_and_gaps_result, <<~EXPECTED
Expand Down