Class: Spout::Helpers::ChartTypes

Inherits:
Object
  • Object
show all
Defined in:
lib/spout/helpers/chart_types.rb

Class Method Summary collapse

Class Method Details

.continuous_buckets(values) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spout/helpers/chart_types.rb', line 21

def self.continuous_buckets(values)
  values.select!{|v| v.is_a? Numeric}
  return [] if values.count == 0
  minimum_bucket = values.min
  maximum_bucket = values.max
  max_buckets = 12
  bucket_size = ((maximum_bucket - minimum_bucket) / max_buckets.to_f)
  precision = (bucket_size == 0 ? 0 : [-Math.log10(bucket_size).floor, 0].max)

  buckets = []
  (0..(max_buckets-1)).to_a.each do |index|
    start = (minimum_bucket + index * bucket_size)
    stop = (start + bucket_size)
    buckets << [start.round(precision),stop.round(precision)]
  end
  buckets
end

.get_bucket(buckets, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spout/helpers/chart_types.rb', line 9

def self.get_bucket(buckets, value)
  return nil if buckets.size == 0 or not value.is_a?(Numeric)
  buckets.each do |b|
    return "#{b[0]} to #{b[1]}" if value >= b[0] and value <= b[1]
  end
  if value <= buckets.first[0]
    "#{buckets.first[0]} to #{buckets.first[1]}"
  else
    "#{buckets.last[0]} to #{buckets.last[1]}"
  end
end