Class: Prawn::Graph::Series

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/graph/series.rb

Overview

A Prawn::Graph::Series represents a series of data which are to be plotted on a chart.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  title:            nil,
  type:             :bar,
  mark_average:     false,
  mark_minimum:     false,
  mark_maximum:     false,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = [], options = {}) ⇒ Series

Returns a new instance of Series.


20
21
22
23
24
# File 'lib/prawn/graph/series.rb', line 20

def initialize(values = [], options = {})
  @values   = values
  @options  = OpenStruct.new(DEFAULT_OPTIONS.merge(options))
  @uuid = SecureRandom.uuid
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.


10
11
12
# File 'lib/prawn/graph/series.rb', line 10

def options
  @options
end

#uuidObject

Returns the value of attribute uuid.


10
11
12
# File 'lib/prawn/graph/series.rb', line 10

def uuid
  @uuid
end

#valuesObject

Returns the value of attribute values.


10
11
12
# File 'lib/prawn/graph/series.rb', line 10

def values
  @values
end

Instance Method Details

#<<(value) ⇒ Array

Returns The modified values object.

Parameters:

  • value (Object)

    a value to be added to the series. Must be of the same kind as other values.

Returns:

  • (Array)

    The modified values object.


41
42
43
# File 'lib/prawn/graph/series.rb', line 41

def <<(value)
  @values << value
end

#avgNumeric

Returns The average value stored in the values of this Series.

Returns:

  • (Numeric)

    The average value stored in the values of this Series.


63
64
65
66
67
68
69
# File 'lib/prawn/graph/series.rb', line 63

def avg
  if size > 0
    @values.inject(:+) / size
  else
    0
  end
end

#mark_average?Boolean

Returns:

  • (Boolean)

77
78
79
# File 'lib/prawn/graph/series.rb', line 77

def mark_average?
  options.mark_average == true
end

#mark_maximum?Boolean

Returns:

  • (Boolean)

85
86
87
# File 'lib/prawn/graph/series.rb', line 85

def mark_maximum?
  options.mark_maximum == true
end

#mark_minimum?Boolean

Returns:

  • (Boolean)

81
82
83
# File 'lib/prawn/graph/series.rb', line 81

def mark_minimum?
  options.mark_minimum == true
end

#maxNumeric

Returns The largest value stored in the values of this Series.

Returns:

  • (Numeric)

    The largest value stored in the values of this Series.


57
58
59
# File 'lib/prawn/graph/series.rb', line 57

def max
  @values.max || 0
end

#minNumeric

Returns The smallest value stored in the values of this Series.

Returns:

  • (Numeric)

    The smallest value stored in the values of this Series.


47
48
49
50
51
52
53
# File 'lib/prawn/graph/series.rb', line 47

def min
  if values.empty?
    0
  else
    values.sort.collect{ |x| x unless x.zero? }.compact.first
  end
end

#sizeNumeric

Returns The size of the values stored in this Series.

Returns:

  • (Numeric)

    The size of the values stored in this Series.


73
74
75
# File 'lib/prawn/graph/series.rb', line 73

def size
  @values.size
end

#titleString

Returns The value of options.title.

Returns:

  • (String)

    The value of options.title.


28
29
30
# File 'lib/prawn/graph/series.rb', line 28

def title
  options.title
end

#typeSymbol

Returns The value of options.type.

Returns:

  • (Symbol)

    The value of options.type.


34
35
36
# File 'lib/prawn/graph/series.rb', line 34

def type
  options.type
end