Module: Reporta::ChartjsHelper

Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/reporta/chartjs_helper.rb

Instance Method Summary collapse

Instance Method Details

#bar_chart_for(report, x, y, locals = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/reporta/chartjs_helper.rb', line 27

def bar_chart_for(report, x, y, locals={})
  locals.reverse_merge!(
    report: report, 
    x: x,
    y: y, 
    prefix: report.class.to_s.demodulize, 
    rows: report.rows,
    color: '#46BFBD')
  render partial: "reporta/reports/chartjs_bar", locals: locals
end

#chart_pie_from_rows(rows, x, y) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/reporta/chartjs_helper.rb', line 38

def chart_pie_from_rows(rows, x, y)
  return {datasets:'[]', colors: '{}'} if rows.blank?
  chart = {colors: {}, datasets: ''}
  data = []
  rows.each do |row|
    color = cycle(*colors)
    value = row.send(y)
    label = row.send(x) || "Unknown"
    data << { label: "#{label.humanize}", value: value, color: color }
    chart[:colors][row.send(x)] = color
  end
  chart[:datasets] = data.to_json
  chart
end

#colorsObject



53
54
55
# File 'lib/reporta/chartjs_helper.rb', line 53

def colors
  ['#F38630', '#E0E4CC', '#69D2E7','#F7464A', '#E2EAE9', '#D4CCC5', '#949FB1', '#4D5360']
end

#decimals_from_rows(rows, x, y, precision = 2) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/reporta/chartjs_helper.rb', line 65

def decimals_from_rows(rows, x, y, precision=2)
  return [] if rows.blank?
  rows.collect{|r| 
    row = r.send(y)
    row = row.blank? ? 0 : row 
    number_with_precision(row.to_f, precision: precision).to_f }
end

#labels_from_rows(rows, attr, length = 11) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/reporta/chartjs_helper.rb', line 57

def labels_from_rows(rows, attr, length=11)
  return [] if rows.empty?
  rows.collect{|r| 
    row = r.send(attr)
    row = row.blank? ? 'Unknown' : row
    truncate(row.to_s, length: length)}
end

#line_chart_for(report, x, y, locals = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/reporta/chartjs_helper.rb', line 5

def line_chart_for(report, x, y, locals={})
  locals.reverse_merge!(
    report: report, 
    x: x,
    y: y, 
    prefix: report.class.to_s.demodulize, 
    rows: report.rows,
    color: '#46BFBD')
  render partial: "reporta/reports/chartjs_line", locals: locals
end

#pie_chart_for(report, x, y, locals = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/reporta/chartjs_helper.rb', line 16

def pie_chart_for(report, x, y, locals={})
  locals.reverse_merge!(
    report: report, 
    x: x,
    y: y, 
    prefix: report.class.to_s.demodulize, 
    rows: report.rows,
    color: '#46BFBD')
  render partial: "reporta/reports/chartjs_pie", locals: locals
end