Sha256: f34c831bd91714bda3ca1050c5626504839a925ef9c8701b43e779e0c6965642

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

module Rorschart
  class MultipleSeries < RorschartData

    attr_accessor :raw_series, :rorschart_series, :to_sql

    def initialize(raw_series)
      @to_sql = raw_series.map { |serie| serie.to_sql rescue nil }.delete_if { |serie| serie.nil? }
      @raw_series = raw_series
      @rorschart_series = raw_series.collect { |serie|
        RorschartData.new(serie)
      }
    end

    def cols
      cols_with_dup = @rorschart_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 = []
      @rorschart_series.each { |serie|
        series_indexed << index_series_by_first_col(serie) if !serie.cols.nil?
      }

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

      if !asc
        union_series = union_series.reverse
      end

      # Return union of all series
      union_series
    end

    private

    def union_of_first_columns
      (
        @rorschart_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

    def ascending? hsh
      hsh.keys == hsh.keys.sort
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rorschart-0.20.0 lib/rorschart/data/multiple_series.rb
rorschart-0.19.2 lib/rorschart/data/multiple_series.rb
rorschart-0.19.1 lib/rorschart/data/multiple_series.rb