Sha256: df59f330a411b5c824bc0fec4e5a728759935d3ee74a1477741553fc8e8a1c59

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

require 'spout/helpers/array_statistics'
require 'spout/helpers/table_formatting'

module Spout
  module Helpers
    class ChartTypes
      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

      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spout-0.11.0 lib/spout/helpers/chart_types.rb
spout-0.11.0.rc lib/spout/helpers/chart_types.rb
spout-0.11.0.beta3 lib/spout/helpers/chart_types.rb
spout-0.11.0.beta2 lib/spout/helpers/chart_types.rb
spout-0.11.0.beta1 lib/spout/helpers/chart_types.rb