Class: ActiveAnalytics::Histogram

Inherits:
Object
  • Object
show all
Defined in:
app/lib/active_analytics/histogram.rb

Defined Under Namespace

Classes: Bar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, from_date, to_date) ⇒ Histogram

Returns a new instance of Histogram.



5
6
7
8
9
10
# File 'app/lib/active_analytics/histogram.rb', line 5

def initialize(scope, from_date, to_date)
  @scope = scope
  @from_date, @to_date = from_date, to_date
  @bars = scope.map { |record| Bar.new(record.date, record.total, self) }
  fill_missing_days(@bars, @from_date, @to_date)
end

Instance Attribute Details

#barsObject (readonly)

Returns the value of attribute bars.



3
4
5
# File 'app/lib/active_analytics/histogram.rb', line 3

def bars
  @bars
end

#from_dateObject (readonly)

Returns the value of attribute from_date.



3
4
5
# File 'app/lib/active_analytics/histogram.rb', line 3

def from_date
  @from_date
end

#to_dateObject (readonly)

Returns the value of attribute to_date.



3
4
5
# File 'app/lib/active_analytics/histogram.rb', line 3

def to_date
  @to_date
end

Instance Method Details

#fill_missing_days(bars, from, to) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'app/lib/active_analytics/histogram.rb', line 12

def fill_missing_days(bars, from, to)
  i = 0
  while (day = from + i) <= to
    if !@bars[i] || @bars[i].label != day
      @bars.insert(i, Bar.new(day, 0, self))
    end
    i += 1
  end
  @bars
end

#max_valueObject



23
24
25
# File 'app/lib/active_analytics/histogram.rb', line 23

def max_value
  @max_total ||= bars.map(&:value).max
end

#totalObject



27
28
29
# File 'app/lib/active_analytics/histogram.rb', line 27

def total
  @bars.reduce(0) { |sum, bar| sum += bar.value }
end