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
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
text_chart demonstration
Show you how cool this is

10 |'''''''''###
9 |'''''''''###'''''''''''''''''''''''''''''''''''''''###
8 |'''''''''###'''''''''''''''''''''''''''''''''### ###
7 |'''''''''###'''### ### ###
6 |'''''''''###'''###'''''''''''''''''''''''''''###'''###'''###
5 |'''''''''###'''###'''### ### ### ###
4 |'''''''''###'''###'''###'''''''''### ### ### ###
3 |'''### ### ### ### ### ### ### ###
2 |'''###'''###'''###'''###'''''''''###'''### ### ### ###
1 |'''###'''###'''###'''###'''### ### ### ### ### ###
0 | ### ### ### ### ### ### ### ### ### ###
----------------------------------------------------------------
73| ###
| ###
67| ### ### ### ###
| ### ### ### ###
54| ### ### ### ### ### ### ###
| ### ### ### ### ### ### ###
44| ### ### ### ### ### ### ### ### ### ###
| ### ### ### ### ### ### ### ### ### ###
33| ### ### ### ### ### ### ### ### ### ### ###
| ### ### ### ### ### ### ### ### ### ### ###
27| ### ### ### ### ### ### ### ### ### ### ### ### ### ###
20| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
14| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
10| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
5| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
----------------------------------------------------------------------------------
```
## Why?

Expand All @@ -42,16 +46,25 @@ Or install it yourself as:

This snippet will generate the text chart at the top of the readme.
```ruby
TextChart.new("text_chart demonstration", "Show you how cool this is", [*1..10].shuffle(random: Random.new(1)).to_s
TextChart.new(
"text_chart demonstration",
"Show you how cool this is",
[17, 54, 47, 64, 27, 56, 33, 66, 14, 45, 10, 21, 24, 27, 73, 54, 20, 67, 44, 5]
).to_s
```

You can enable colors:
```ruby
TextChart.new("text_chart demonstration", "Show you how cool this is", [*1..10].shuffle(random: Random.new(1), true).to_s
TextChart.new(
"text_chart demonstration",
"Show you how cool this is",
[17, 54, 47, 64, 27, 56, 33, 66, 14, 45, 10, 21, 24, 27, 73, 54, 20, 67, 44, 5],
true
).to_s
```
## Limitations

Right now `TextChart` only supports integers.
Right now `TextChart` only supports positive integers.

## Development

Expand Down
43 changes: 22 additions & 21 deletions lib/text_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
# text_chart demonstration
# Show you how cool this is
#
# 10 |'''''''''###
# 9 |'''''''''###'''''''''''''''''''''''''''''''''''''''###
# 8 |'''''''''###'''''''''''''''''''''''''''''''''### ###
# 7 |'''''''''###'''### ### ###
# 6 |'''''''''###'''###'''''''''''''''''''''''''''###'''###'''###
# 5 |'''''''''###'''###'''### ### ### ###
# 4 |'''''''''###'''###'''###'''''''''### ### ### ###
# 3 |'''### ### ### ### ### ### ### ###
# 2 |'''###'''###'''###'''###'''''''''###'''### ### ### ###
# 1 |'''###'''###'''###'''###'''### ### ### ### ### ###
# 0 | ### ### ### ### ### ### ### ### ### ###
# ----------------------------------------------------------------
# 73| ###
# | ###
# 67| ### ### ### ###
# | ### ### ### ###
# 54| ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ###
# 44| ### ### ### ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ### ### ###
# 33| ### ### ### ### ### ### ### ### ### ### ###
# | ### ### ### ### ### ### ### ### ### ### ###
# 27| ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# 20| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# 14| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# 10| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# 5| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# ----------------------------------------------------------------------------------
class TextChart
class Error < StandardError; end

Expand All @@ -30,16 +34,17 @@ class Error < StandardError; end
# @param [Array] data
# @param [Boolean] colors
def initialize(title, subtitle, data, colors = false)
raise Error, "`data` cannot be empty" if data.empty?

@title = title
@subtitle = subtitle
@data = data.empty? ? [0] : data
@data = data.empty? ? [0] : data.filter(&:positive?)
@colors = colors
@refs = define_references
@size_calculator = SizeCalculator.new(self)
@designer = Designer.new(self, @size_calculator)
end

attr_reader :title, :subtitle, :refs, :data, :size_calculator, :designer
attr_reader :title, :subtitle, :data, :size_calculator, :designer

# @return [String]
def to_s
Expand All @@ -65,11 +70,7 @@ def size_config(key)
bar_margin: 1,
bar_width: 3,
x_axis_height: 1,
reference_row_height: 1
bar_row_height: 1,
max_bar_height: 60
}

def define_references
r = [*@data.min..@data.max].reverse
r.map(&:to_s)
end
end
20 changes: 11 additions & 9 deletions lib/text_chart/designer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,24 @@ def draw_y_axis
end

def draw_references
references = @text_chart.refs
references = @text_chart.data
width = @size_calc.calculate_reference_width
number_of_references = references.size
ref_size = ref_start = ref_end = nil
y_axis_size = @size_calc.calculate_y_axis_size
ref_size = ref_start = ref_end = ref_str = nil

number_of_references.times do |i|
ref_size = references[i].size
references.each do |r|
row = y_axis_size - @size_calc.calculate_bar_height(r)
ref_str = r.to_s
ref_size = ref_str.size

if ref_size == width
ref_start = 0
ref_end = ref_size - 1
@chart_canvas[i][ref_start..ref_end] = references[i]
else
ref_start = ref_size
@chart_canvas[i][ref_start] = references[i]
ref_start = width - ref_size
ref_end = [ref_start, ref_size].max
end
@chart_canvas[row][ref_start..ref_end] = ref_str
end
end

Expand All @@ -146,6 +148,6 @@ def colorize(str, format)

# @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) }
@text_chart.data.map { |i| @size_calc.calculate_bar_height(i) }
end
end
38 changes: 35 additions & 3 deletions lib/text_chart/size_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ def calculate_number_of_rows
x_axis_row = @text_chart.size_config(:x_axis_height)
result += x_axis_row

reference_row = @text_chart.size_config(:reference_row_height)
number_of_references = @text_chart.refs.size
number_of_references.times { result += reference_row }
calculate_bar_height(@text_chart.data.max).times do
result += @text_chart.size_config(:bar_row_height)
end

result
end
end

# @param bar [Integer]
# @return the number of rows that we should render [Integer]
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

# @return [Integer]
def calculate_x_axis_size
@x_axis_size ||= calculate_number_of_columns - calculate_reference_width
Expand All @@ -71,4 +80,27 @@ def calculate_y_axis_size
calculate_number_of_rows - x_axis_row
end
end

private

# @param initial value [Numeric]
# @param final value [Numeric]
# @return increase percentile [Float]
def calculate_increase_percentile(initial, final)
float_initial = initial.to_f
float_final = final.to_f
diff = float_final - float_initial
(diff / float_initial) * 100.0
end

# @return the denominator to assure the `max_rows` config [Integer]
def bar_height_limiter
@bar_height_limiter ||=
begin
limiter = 10.0
max_diff = calculate_increase_percentile(@text_chart.data.min, @text_chart.data.max)
limiter *= 10.0 until (max_diff / limiter).round <= @text_chart.size_config(:max_bar_height)
limiter
end
end
end
68 changes: 8 additions & 60 deletions test/text_chart/designer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TextChart::DesignerTest < Test::Unit::TestCase
test "#draw_header" do
text_chart = TextChart.new("This is a nice title", "This is a nice subtitle", [])
text_chart = TextChart.new("This is a nice title", "This is a nice subtitle", [1])

result = text_chart.designer.draw_header.join

Expand All @@ -18,20 +18,11 @@ class TextChart::DesignerTest < Test::Unit::TestCase
end

test "#draw_axis" do
no_sample_designer = TextChart.new("No sample", "Testing", []).designer
small_sample_designer = TextChart.new("Small sample", "Testing", [*1..10]).designer
with_gaps_designer = TextChart.new("With gaps", "Testing", [1, 5, 10]).designer
with_negative_number = TextChart.new("With negative number", "Testing", [*-3..3]).designer

no_sample_result = no_sample_designer.draw_axis.join
small_sample_result = small_sample_designer.draw_axis.join
with_gaps_result = with_gaps_designer.draw_axis.join
with_negative_number_result = with_negative_number.draw_axis.join

assert_equal no_sample_result, <<~END
0|
------
END

assert_equal small_sample_result, <<~END
10|
Expand All @@ -46,53 +37,33 @@ class TextChart::DesignerTest < Test::Unit::TestCase
1|
------------------------------------------
END

assert_equal with_gaps_result, <<~END
10|
9|
8|
7|
6|
|
|
|
|
5|
4|
3|
2|
|
|
|
1|
--------------
END

assert_equal with_negative_number_result, <<~END
3|
2|
1|
0|
-1|
-2|
-3|
------------------------------
END
end

test "#draw_bars" do
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
with_zero_designer = TextChart.new(
"With zero", "Testing", [*0..5].shuffle(random: Random.new(1))
).designer
gaps = TextChart.new(
"Duplicated and gaps", "Testing", [*1..3, 6, 12].shuffle(random: Random.new(1))
).designer
negative = TextChart.new(
"Negative", "Testing", [*-3..3].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
gaps_result = gaps.draw_bars.join
negative_result = negative.draw_bars.join

assert_equal sorted_result, <<-END
###
Expand All @@ -107,7 +78,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ### ### ### ### ### ###

END

assert_equal random_order_result, <<-END
###
### ###
Expand All @@ -121,17 +91,6 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ### ### ### ### ### ###

END

assert_equal with_zero_result, <<-END
###
### ###
### ### ###
### ### ### ###
### ### ### ### ###
### ### ### ### ### ###

END

assert_equal gaps_result, <<-END
###
###
Expand All @@ -147,16 +106,5 @@ class TextChart::DesignerTest < Test::Unit::TestCase
### ### ### ### ###

END

assert_equal negative_result, <<-END
###
### ###
### ### ###
### ### ### ###
### ### ### ### ###
### ### ### ### ### ###
### ### ### ### ### ### ###

END
end
end
Loading