Class: RankingDiagram
- Inherits:
-
Object
- Object
- RankingDiagram
- Defined in:
- lib/output/index_diagram/ranking_diagram.rb
Overview
logic class to draw a simple bar chart for the given ranking
Instance Attribute Summary collapse
-
#count ⇒ Number
private
The overall number of considered entries.
-
#entry_size ⇒ Number
private
The maximal length of the value characters that are printed.
-
#width ⇒ Number
private
The width available for the bars.
Instance Method Summary collapse
-
#accumulate_numbers(ranking) ⇒ Object
private
helper method to calculate the number of all considered entries.
-
#initialize(ranking) ⇒ RankingDiagram
constructor
initialization.
-
#initialize_axis_numbers ⇒ String
private
helper method to initialize the string printing the number values of the x axis.
-
#print_hash_pair(entry) ⇒ Object
private
method to print the value, the bar and the percentage of the occurence based on the overall number of considered entries.
-
#print_x_axis ⇒ Object
private
method to print the x data axis.
-
#retrieve_terminal_width ⇒ Object
private
method to get the size information of the used terminal.
Constructor Details
#initialize(ranking) ⇒ RankingDiagram
initialization
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 9 def initialize(ranking) @entry_size = 20 retrieve_terminal_width accumulate_numbers(ranking) ranking.each { |entry| print_hash_pair(entry) } print_x_axis end |
Instance Attribute Details
#count ⇒ Number (private)
Returns the overall number of considered entries.
25 26 27 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 25 def count @count end |
#entry_size ⇒ Number (private)
Returns the maximal length of the value characters that are printed.
27 28 29 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 27 def entry_size @entry_size end |
#width ⇒ Number (private)
Returns the width available for the bars.
23 24 25 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 23 def width @width end |
Instance Method Details
#accumulate_numbers(ranking) ⇒ Object (private)
helper method to calculate the number of all considered entries
71 72 73 74 75 76 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 71 def accumulate_numbers(ranking) @count = 0 ranking.each { |entry| @count += entry[1] } end |
#initialize_axis_numbers ⇒ String (private)
helper method to initialize the string printing the number values of the x axis
81 82 83 84 85 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 81 def initialize_axis_numbers axis = "" (entry_size + 1).times { axis.concat(" ")} axis.concat("0") end |
#print_hash_pair(entry) ⇒ Object (private)
method to print the value, the bar and the percentage of the occurence based on the overall number of considered entries
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 37 def print_hash_pair(entry) print "%#{@entry_size}s |" % [entry[0].to_s[0...@entry_size]] ratio = entry[1] * 1.0 / @count # print dynamic bar for value (ratio * @width).round.times { print "=" } puts " (#{(ratio * 100).round(2)}%)" end |
#print_x_axis ⇒ Object (private)
method to print the x data axis
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 49 def print_x_axis print "%#{@entry_size}s |" % ["portion in %"] axis_numbers = initialize_axis_numbers counter = 0.1; 1.upto(@width) do |i| if ( i % (counter * @width).round == 0) print "|" axis_numbers = axis_numbers[0...-1].concat((counter * 100).round.to_s) counter += 0.1 else print "-" axis_numbers.concat(" ") end end puts "\n#{axis_numbers}" end |
#retrieve_terminal_width ⇒ Object (private)
method to get the size information of the used terminal
30 31 32 33 |
# File 'lib/output/index_diagram/ranking_diagram.rb', line 30 def retrieve_terminal_width ts = TerminalSize::TerminalSize.new() @width = ts.columns - @entry_size - 3 end |