Sha256: 9ec9d247ff18876a27d0ae41c47ada6298bb09b29214916f03e7b7c413de39fe

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Rorschart
  class MultipleSeries

    attr_accessor :raw_series, :pivot_series

    def initialize(raw_series)
      @raw_series = raw_series
      @pivot_series = raw_series.collect { |serie|
        PivotData.new(serie)
      }
    end

    def cols
      cols_with_dup = @pivot_series.inject([]) { |cols, series| 
        cols + series.cols
      }
   
      cols_with_dup.uniq

    end

    def rows
      # create union of all series first columns, to represent all abscisse values available
      union_x = union_of_first_columns()

      # Preparation: store all series rows in a hash indexed by first column
      series_indexed = []
      @pivot_series.each { |serie|
        series_indexed << index_series_by_first_col(serie)
      }

      # The Merge:
      # For abscisse value, grab for each serie the corresponding row - or nil
      union_series = []
      union_x.each { |x|
        row = [x]
        series_indexed.each { |serie_hash|
          row << serie_hash[x]
        }
        union_series << row.flatten
      }

      # Return union of all series
      union_series
    end

    private

    def uniq_label(label)
    end

    def union_of_first_columns
      (
        @pivot_series.inject([]) { |union, serie|
          union + serie.rows
        }
      ).collect{|r| r[0]}.uniq.sort
    end

    def index_series_by_first_col(serie)

      serie_hash = {}
      serie.rows.each { |row|
        serie_hash[row[0]] = row.drop(1)
      }

      serie_hash
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rorschart-0.10.3 lib/rorschart/multiple_series.rb
rorschart-0.10.1 lib/rorschart/multiple_series.rb
rorschart-0.10.0 lib/rorschart/multiple_series.rb