Class: RankingDiagram

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ranking) ⇒ RankingDiagram

initialization

Parameters:

  • ranking (Array)

    an array storing the values and the numbers of occurences in nested arrays



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

#countNumber (private)

Returns the overall number of considered entries.

Returns:

  • (Number)

    the overall number of considered entries



25
26
27
# File 'lib/output/index_diagram/ranking_diagram.rb', line 25

def count
  @count
end

#entry_sizeNumber (private)

Returns the maximal length of the value characters that are printed.

Returns:

  • (Number)

    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

#widthNumber (private)

Returns the width available for the bars.

Returns:

  • (Number)

    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

Parameters:

  • ranking (Array)

    an array storing the values and the numbers of occurences in nested arrays



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_numbersString (private)

helper method to initialize the string printing the number values of the x axis

Returns:

  • (String)

    the starting characters of the x axis number string



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

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

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_widthObject (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