Class: ActiveAnalytics::Histogram
- Inherits:
-
Object
- Object
- ActiveAnalytics::Histogram
- Defined in:
- app/lib/active_analytics/histogram.rb
Defined Under Namespace
Classes: Bar
Instance Attribute Summary collapse
-
#bars ⇒ Object
readonly
Returns the value of attribute bars.
-
#from_date ⇒ Object
readonly
Returns the value of attribute from_date.
-
#to_date ⇒ Object
readonly
Returns the value of attribute to_date.
Instance Method Summary collapse
- #fill_missing_days(bars, from, to) ⇒ Object
-
#initialize(scope, from_date, to_date) ⇒ Histogram
constructor
A new instance of Histogram.
- #max_value ⇒ Object
- #total ⇒ Object
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
#bars ⇒ Object (readonly)
Returns the value of attribute bars.
3 4 5 |
# File 'app/lib/active_analytics/histogram.rb', line 3 def @bars end |
#from_date ⇒ Object (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_date ⇒ Object (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(, 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_value ⇒ Object
23 24 25 |
# File 'app/lib/active_analytics/histogram.rb', line 23 def max_value @max_total ||= .map(&:value).max end |
#total ⇒ Object
27 28 29 |
# File 'app/lib/active_analytics/histogram.rb', line 27 def total @bars.reduce(0) { |sum, | sum += .value } end |