Class: Chartnado::Renderer

Inherits:
Object
  • Object
show all
Includes:
Series
Defined in:
lib/chartnado/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Series

#median, #series_product, #series_ratio, #series_sum

Constructor Details

#initialize(context, data_block, &render_block) ⇒ Renderer

Returns a new instance of Renderer.



6
7
8
9
10
# File 'lib/chartnado/renderer.rb', line 6

def initialize(context, data_block, &render_block)
  @context = context
  @data_block = data_block
  @render_block = render_block
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



4
5
6
# File 'lib/chartnado/renderer.rb', line 4

def context
  @context
end

#data_blockObject

Returns the value of attribute data_block.



4
5
6
# File 'lib/chartnado/renderer.rb', line 4

def data_block
  @data_block
end

#render_blockObject

Returns the value of attribute render_block.



4
5
6
# File 'lib/chartnado/renderer.rb', line 4

def render_block
  @render_block
end

Instance Method Details

#chart_json(series, show_total: false, reverse_sort: false, percentage: false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chartnado/renderer.rb', line 49

def chart_json(series, show_total: false, reverse_sort: false, percentage: false)
  series = Chartnado::Series::Wrap[series]
  series *= 100.0 if percentage
  if series.has_separate_named_series?
    series = series.to_a
    if series.first.second.respond_to?(:map)
      totals = Hash.new(0.0)
      new_series = series.sort_by { |item| item.first.to_s }
      new_series = new_series.reverse if reverse_sort

      new_series = new_series.map do |name, data|
        {
          name: name,
          data: data.map do |k, v|
            totals[k] += v if show_total
            [k, v]
          end
        }
      end

      if show_total
        [{name: 'Total',
          data: totals.map { |k, v| [k, 0] },
          tooltip: totals.map { |k, v| [k, v] }
         }] + new_series
      else
        new_series
      end
    else
      new_series = series.sort_by { |item| item.first.to_s }
      new_series = new_series.reverse if reverse_sort

      if show_total
        new_series << ['Total', new_series.map(&:second).reduce(0, :+)]
      else
        new_series
      end
    end
  elsif series.hash?
    if (key = series.keys.first) and key.is_a?(Array) and key.size == 2
      totals = Hash.new(0.0)
      new_series = series.group_by { |k, v| k[0] }.sort_by { |k| k.to_s }
      new_series = new_series.reverse if reverse_sort

      new_series = new_series.map do |name, data|
        {
          name: name,
          data: data.map do |k, v|
            totals[k[1]] += v if show_total
            [k[1], v]
          end
        }
      end

      if show_total
        [{name: 'Total',
          data: totals.map { |k, v| [k, 0] },
          tooltip: totals.map { |k, v| [k, v] }
         }] + new_series
      else
        new_series
      end
    else
      new_series = series.sort_by { |key| key.to_s }
      new_series = new_series.reverse if reverse_sort

      if show_total
        new_series << ['Total', new_series.map(&:second).reduce(0, :+)]
      else
        new_series
      end
    end
  elsif series.respond_to?(:map)
    series
  else
    [['Total', series]]
  end
end

#render(*args, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chartnado/renderer.rb', line 14

def render(*args, ** options)
  json_options = {}
  chartkick_options = options.dup

  if args.length > 1
    if args.last.is_a?(Hash)
      json_options = chartkick_options
      chartkick_options = args[1].dup
    end
    args.select { |arg| arg.is_a?(Symbol) }.each do |key|
      json_options[key] = true unless json_options.has_key?(key)
    end
  end

  if json_options[:percentage]
    chartkick_options.reverse_merge!(max: 100.0)
  end

  options = controller.chartnado_options if controller.respond_to?(:chartnado_options)
  options ||= {}

  chart_json_proc = -> (*args) {
    chart_json(Chartnado.with_chartnado_dsl(&data_block), *args)
  }
  renderer = -> {
    context.instance_exec(chartkick_options, json_options, chart_json_proc, &render_block)
  }

  if options[:wrapper_proc]
    context.instance_exec(*args, renderer, ** options, &options[:wrapper_proc])
  else
    renderer.call
  end
end