Class: Histogram

Inherits:
Object show all
Defined in:
lib/bad_ass_extensions/histogram.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Histogram

Returns a new instance of Histogram.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bad_ass_extensions/histogram.rb', line 2

def initialize(data, options={})
  
  if data.is_a?(Array)
    puts "Array"
    @hash = data.group_by{|x| x}.inject({}){|hash, val| hash[val[0]] = val[1].length;hash}
  elsif data.is_a?(Hash)  && data[data.keys.first].is_a?(Array)
    puts "Hash of Array"
    @hash = data.inject({}){|hash, val| hash[val[0]] = val[1].length;hash}
  elsif data.is_a?(Hash)  && data[data.keys.first].is_a?(Integer)
    puts "Hash of Integer"
    @hash = data
  elsif data.is_a?(Hash)  && data[data.keys.first].is_a?(String) && data[data.keys.first].to_i.is_a?(Integer)
    puts "Hash of Integer String"
    @hash = data.inject({}){|hash, val| hash[val[0]] = val[1].to_i;hash}
  elsif data.is_a?(Hash) && [Time, DateTime, Date].include?(data[data.keys.first].class)
    @hash = Hash[data.map{|k, v| [k, v.to_i]}]
  else
    raise "Need to be throwing out an array of item, a hash where each val is an array or a hash where each val is an integer"
  end
  puts @hash.inspect
  
  @title = options[:title] || "Histogram by Jay"
  @hist_height = options[:height] || 100
  @total = @hash.inject(0){|sum, val| sum += val[1];sum}
  @sorted_keys = @hash.keys.sort
  @max_key_length = @sorted_keys.map{|x| x.to_s.length}.max
  @max_int_length = @hash.map{|k,v| v.to_s.length}.max
  @hash_modified = @hash.inject({}){|hash, val| hash[val[0]] = ((val[1].to_f/@total)*@hist_height);hash}
end

Instance Method Details

#clean_percent(value) ⇒ Object



61
62
63
64
65
66
# File 'lib/bad_ass_extensions/histogram.rb', line 61

def clean_percent(value)
  int = value.to_i
  int = "%2d" % int # "%02d" if you want to pad with zeros
  prec = (value - int.to_i).to_s[2..5]
  "#{int}.#{prec}"
end

#draw(options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bad_ass_extensions/histogram.rb', line 49

def draw(options={})
  draw_title(@title)
  value = options[:value] || nil
  percent = options[:percent] || nil
  
  @sorted_keys.each do |key|
    value = pad_string(@hash[key].to_s, @max_int_length) if value
    percent = clean_percent(@hash_modified[key]) if percent
    puts "#{pad_string(key, @max_key_length)}\t#{value}\t#{percent}\t#{'*' * (@hash_modified[key]+1)}"
  end
end

#draw_title(string, options = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/bad_ass_extensions/histogram.rb', line 68

def draw_title(string, options={})
  pad_char = options[:pad_char] || '-'
  puts pad_char * (string.length + 6)
  puts "#{pad_char}#{pad_char} " + string + " #{pad_char}#{pad_char}"
  puts pad_char * (string.length + 6)
end

#pad_string(string, max_length) ⇒ Object



32
33
34
35
36
# File 'lib/bad_ass_extensions/histogram.rb', line 32

def pad_string(string, max_length)
  len = string.to_s.length
  pad = max_length - len
  string.to_s + (" " * pad)
end

#sorted_padded_keys(string_array) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/bad_ass_extensions/histogram.rb', line 38

def sorted_padded_keys(string_array)
  max_length = string_array.map{|x| x.length}.max
  new_arry = string_array.inject([]) do |arr, val|
    len = val.length
    pad = max_length - len
    arr << val + (" " * pad)
    arr
  end
  new_arry.sort
end