Class: Dumbstats::Graph

Inherits:
Bucket
  • Object
show all
Includes:
Initialization
Defined in:
lib/dumbstats/graph.rb

Overview

Prepare statitical graph for text display. A Graph of values.

Constant Summary

Constants inherited from Bucket

Bucket::KEYS

Instance Attribute Summary collapse

Attributes inherited from Bucket

#name

Instance Method Summary collapse

Methods included from Initialization

#update_from_hash!

Methods inherited from Bucket

#add!, #add_delta!, #count!, #count_only?, #empty?, #h, #histogram, #inspect, #rate!, #rate?, #to_a, #to_s

Constructor Details

#initialize(*args) ⇒ Graph

Returns a new instance of Graph.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dumbstats/graph.rb', line 11

def initialize *args
  super
  @force_min, @force_max = @min, @max
  if @values
    values = @values
    @values = [ ]
    values.each { | v | add! v }
    finish!
  end
  @width ||= 20
end

Instance Attribute Details

#valuesObject

Returns the value of attribute values.



8
9
10
# File 'lib/dumbstats/graph.rb', line 8

def values
  @values
end

#values_are_integersObject

Returns the value of attribute values_are_integers.



9
10
11
# File 'lib/dumbstats/graph.rb', line 9

def values_are_integers
  @values_are_integers
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/dumbstats/graph.rb', line 8

def width
  @width
end

Instance Method Details

#bar(value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dumbstats/graph.rb', line 58

def bar value
  x = v_to_x(value).to_i
  binding.pry if x < 0
  if value > @min and x < 1
    bar = '.'
  else
    bar = "*" * x
  end
  bar = "#{bar}#{' ' * (@width - bar.size)}"
  bar
end

#finish!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dumbstats/graph.rb', line 31

def finish!
  super
  return nil if empty?
  @min = @force_min if @force_min
  @max = @force_max if @force_max
  @max_min = @max - @min
  @values_are_integers = @values.all?{|e| Integer === e.to_numeric}
  if @values_are_integers
    if @width > @max_min
      @width = @max_min.to_i + 1
    else
      @max_min = @max_min.to_f
    end
  end
  @width_f = @width.to_f
  self
end

#fix_width!Object



23
24
25
26
27
28
29
# File 'lib/dumbstats/graph.rb', line 23

def fix_width!
  @width = 1 if @width < 1
  @min = x_to_v(0)
  @max = x_to_v(@width + 1)
  @max_min = (@max - @min).to_f
  self
end

#percent(value) ⇒ Object



54
55
56
# File 'lib/dumbstats/graph.rb', line 54

def percent value
  '%5.1f%%' % (value * 100 / sum!.to_f)
end

#sum!Object



49
50
51
52
# File 'lib/dumbstats/graph.rb', line 49

def sum!
  finish! unless @sum
  @sum
end

#v_to_x(v) ⇒ Object



70
71
72
# File 'lib/dumbstats/graph.rb', line 70

def v_to_x v
  (v - @min) * @width / @max_min # = x
end

#x_to_v(x) ⇒ Object



74
75
76
# File 'lib/dumbstats/graph.rb', line 74

def x_to_v x
  (x * @max_min / @width) + @min # = v
end