Sha256: 65377fc95e54adc5420d34e25704c161d0b9218dc415a312878c3f92259db21d

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Gruff
  class Store
    attr_reader :data, :norm_data

    def initialize(data_class)
      @data_class = data_class
      @data = []
      @norm_data = []
      @normalized = false
    end

    def add(*args)
      @data << @data_class.new(*args)
    end

    def normalize(args = {})
      unless @normalized
        @data.each do |data_row|
          @norm_data << data_row.normalize(args)
        end

        @normalized = true
      end
    end

    def empty?
      @data.empty?
    end

    def length
      @data.length
    end

    def columns
      @columns ||= @data.empty? ? 0 : @data.map(&:columns).max
    end

    def min
      @min ||= @data.map(&:min).compact.min
    end
    alias min_y min

    def max
      @max ||= @data.map(&:max).compact.max
    end
    alias max_y max

    def min_x
      @min_x ||= @data.map(&:min_x).compact.min
    end

    def max_x
      @max_x ||= @data.map(&:max_x).compact.max
    end

    def sort_data!
      @data = @data.sort_by { |a| -a.points.reduce(0) { |acc, elem| acc + elem.to_f } }
    end

    def sort_norm_data!
      @norm_data = @norm_data.sort_by { |a| -a.points.reduce(0) { |acc, elem| acc + elem.to_f } }
    end

    def reverse!
      @data.reverse!
    end

    def change_colors(colors)
      index = 0
      @data.each do |data_row|
        data_row.color ||= begin
          index = (index + 1) % colors.length
          colors[index - 1]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gruff-0.10.0 lib/gruff/store/store.rb
gruff-0.10.0-java lib/gruff/store/store.rb