Skip to content

Commit

Permalink
Adjust Designer#draw_bars to draw the reference lines too
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavothecoder committed Dec 24, 2023
1 parent 300f69a commit e46f37e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
20 changes: 18 additions & 2 deletions lib/text_chart/designer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def draw_bars
zero_line = @chart_canvas.size - 2
chart_line = 0
ref_width = @size_calc.calculate_reference_width
y_axis_width = 1
first_bar_margin = 4
middle_bar_margin = 8
bar_row = "###"
bar_width = 2
bar_height = bar_start = bar_end = 0
bar_height = bar_start = bar_end = bar_top = 0

@text_chart.data.each do |d|
# + 1 to guarantee that the bar will always be rendered
Expand All @@ -53,10 +54,14 @@ def draw_bars
bar_end = bar_start + bar_width

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

chart_line -= 1 unless t == bar_top
end

draw_reference_line(chart_line, ref_width + y_axis_width, bar_start)
end

@chart_canvas
Expand Down Expand Up @@ -118,4 +123,15 @@ def draw_references
end
end
end

def draw_reference_line(chart_line, line_start, line_end)
current_position = line_start
until current_position == line_end
if @chart_canvas[chart_line][current_position] != "#"
@chart_canvas[chart_line][current_position] = "'"
end

current_position += 1
end
end
end
74 changes: 48 additions & 26 deletions test/text_chart/designer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,74 @@ class TextChart::DesignerTest < Test::Unit::TestCase
with_zero_designer = TextChart.new(
"With zero", "Testing", [*0..5].shuffle(random: Random.new(1))
).designer
duplicated_and_gaps = TextChart.new(
"Duplicated and gaps", "Testing", [*1..3, 6, 12].shuffle(random: Random.new(1))
).designer

sorted_result = sorted_designer.draw_bars.join
random_order_result = random_order_designer.draw_bars.join
with_zero_result = with_zero_designer.draw_bars.join
duplicated_and_gaps_result = duplicated_and_gaps.draw_bars.join

expected_sorted_result = <<-END
###
### ###
### ### ###
### ### ### ###
### ### ### ### ###
### ### ### ### ### ###
### ### ### ### ### ### ###
### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ### ### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''### ### ### ### ###
'''''''''''''''''''''''''''''''''''''''''''### ### ### ### ### ###
'''''''''''''''''''''''''''''''''### ### ### ### ### ### ###
'''''''''''''''''''''''### ### ### ### ### ### ### ###
'''''''''''''### ### ### ### ### ### ### ### ###
'''### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###
END
assert_equal sorted_result, expected_sorted_result

expected_random_order_result = <<-END
###
### ###
### ### ###
### ### ### ###
### ### ### ### ###
### ### ### ### ### ###
### ### ### ### ### ### ###
### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###
'''''''''''''###
'''''''''''''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''###'''''''''''''''''''''''''''''''''''''''''''''''''''''''''### ###
'''''''''''''###'''''''### ### ###
'''''''''''''###'''''''###'''''''''''''''''''''''''''''''''''''''''''''''###'''''''###'''''''###
'''''''''''''###'''''''###'''''''### ### ### ###
'''''''''''''###'''''''###'''''''###'''''''''''''''''### ### ### ###
'''### ### ### ### ### ### ### ###
'''###'''''''###'''''''###'''''''###'''''''''''''''''###'''''''### ### ### ###
'''###'''''''###'''''''###'''''''###'''''''### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ###
END
assert_equal random_order_result, expected_random_order_result

expected_with_zero_result = <<-END
###
### ###
### ### ###
### ### ### ###
### ### ### ### ###
### ### ### ### ### ###
'''''''''''''''''''''''''''''''''''''''''''''''''''''###
'''''''''''''''''''''''### ###
'''''''''''''''''''''''###'''''''''''''''''### ###
'''### ### ### ###
'''###'''''''### ### ### ###
'''###'''''''###'''''''###'''''''### ### ###
END
assert_equal with_zero_result, expected_with_zero_result

expected_duplicated_and_gaps_result = <<-END
'''''''''''''''''''''''###
###
###
###
###
###
'''''''''''''''''''''''###'''''''''''''''''###
### ###
### ###
'''### ### ###
'''###'''''''### ### ###
'''###'''''''###'''''''###'''''''### ###
### ### ### ### ###
END
assert_equal duplicated_and_gaps_result, expected_duplicated_and_gaps_result
end
end

0 comments on commit e46f37e

Please sign in to comment.