Class: Stats::PieChartData

Inherits:
Object
  • Object
show all
Defined in:
app/models/stats/pie_chart_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_totals, title, alpha) ⇒ PieChartData

Returns a new instance of PieChartData


5
6
7
8
9
# File 'app/models/stats/pie_chart_data.rb', line 5

def initialize(all_totals, title, alpha)
  @all_totals = all_totals
  @title = title
  @alpha = alpha
end

Instance Attribute Details

#all_totalsObject (readonly)

Returns the value of attribute all_totals


4
5
6
# File 'app/models/stats/pie_chart_data.rb', line 4

def all_totals
  @all_totals
end

#alphaObject (readonly)

Returns the value of attribute alpha


4
5
6
# File 'app/models/stats/pie_chart_data.rb', line 4

def alpha
  @alpha
end

#titleObject (readonly)

Returns the value of attribute title


4
5
6
# File 'app/models/stats/pie_chart_data.rb', line 4

def title
  @title
end

Instance Method Details

#chart_totalsObject (private)


51
52
53
54
55
56
57
58
59
# File 'app/models/stats/pie_chart_data.rb', line 51

def chart_totals
  unless @chart_totals
    @chart_totals = first_n_totals(10)
    if all_totals.size > pie_cutoff
      @chart_totals[-1] = other
    end
  end
  @chart_totals
end

#first_n_totals(n) ⇒ Object (private)


61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/stats/pie_chart_data.rb', line 61

def first_n_totals(n)
  # create a duplicate so that we don't accidentally
  # overwrite the original array
  Array.new(slices) do |i|
    {
      'name' => all_totals[i]['name'],
      'total' => all_totals[i]['total'],
      'id' => all_totals[i]['id']
    }
  end
end

#idsObject


23
24
25
26
27
# File 'app/models/stats/pie_chart_data.rb', line 23

def ids
  @ids ||= Array.new(slices) do |i|
    chart_totals[i]['id']
  end
end

#labelsObject


17
18
19
20
21
# File 'app/models/stats/pie_chart_data.rb', line 17

def labels
  @labels ||= Array.new(slices) do |i|
    chart_totals[i]['name'].truncate(15, :omission => '...')
  end
end

#otherObject (private)


73
74
75
76
77
78
79
# File 'app/models/stats/pie_chart_data.rb', line 73

def other
  {
    'name' => I18n.t('stats.other_actions_label'),
    'id' => -1,
    'total' => subtotal(slices-1, all_totals.size-1)
  }
end

#pie_cutoffObject (private)


37
38
39
# File 'app/models/stats/pie_chart_data.rb', line 37

def pie_cutoff
  10
end

#slicesObject (private)


41
42
43
# File 'app/models/stats/pie_chart_data.rb', line 41

def slices
  @slices ||= [all_totals.size, pie_cutoff].min
end

#subtotal(from, to) ⇒ Object (private)


45
46
47
48
49
# File 'app/models/stats/pie_chart_data.rb', line 45

def subtotal(from, to)
  totals[from..to].inject(0) do |sum, total|
    sum + total
  end
end

#sumObject


29
30
31
32
33
# File 'app/models/stats/pie_chart_data.rb', line 29

def sum
  @sum ||= totals.inject(0) do |sum, total|
    sum + total
  end
end

#totalsObject (private)


81
82
83
# File 'app/models/stats/pie_chart_data.rb', line 81

def totals
  @totals ||= all_totals.map { |item| item['total'] }
end

#valuesObject


11
12
13
14
15
# File 'app/models/stats/pie_chart_data.rb', line 11

def values
  @values ||= Array.new(slices) do |i|
    chart_totals[i]['total'] * 100 / sum
  end
end