Sha256: e42686b608ae505174eef6da7c7ae9218f9470efbb0de0cc4d462b9acce289aa

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

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

7 entries across 7 versions & 1 rubygems

Version Path
spout-0.12.1 lib/spout/helpers/chart_types.rb
spout-0.12.0 lib/spout/helpers/chart_types.rb
spout-0.12.0.rc2 lib/spout/helpers/chart_types.rb
spout-0.12.0.rc lib/spout/helpers/chart_types.rb
spout-0.12.0.beta2 lib/spout/helpers/chart_types.rb
spout-0.12.0.beta1 lib/spout/helpers/chart_types.rb
spout-0.11.1 lib/spout/helpers/chart_types.rb