Module: Enumerable

Defined in:
lib/chartkick.rb

Overview

for multiple series use Enumerable so it can be called on arrays

Instance Method Summary collapse

Instance Method Details

#chart_jsonObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chartkick.rb', line 25

def chart_json
  if is_a?(Hash)
    if (key = keys.first) && key.is_a?(Array) && key.size == 2
      group_by { |k, _v| k[0] }.map do |name, data|
        {name: name, data: data.map { |k, v| [k[1], v] }}
      end
    else
      to_a
    end
  elsif is_a?(Array)
    map do |v|
      if v.is_a?(Hash) && v[:data].is_a?(Hash)
        v = v.dup
        v[:data] = v[:data].to_a
      end
      v
    end
  else
    self
  end.to_json
end