chartkickm 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/chartkick.rb ADDED
@@ -0,0 +1,46 @@
1
+ require "chartkick/helper"
2
+ require "chartkick/version"
3
+
4
+ # integrations
5
+ require "chartkick/engine" if defined?(Rails)
6
+ require "chartkick/sinatra" if defined?(Sinatra)
7
+
8
+ if defined?(ActiveSupport.on_load)
9
+ ActiveSupport.on_load(:action_view) do
10
+ include Chartkick::Helper
11
+ end
12
+ end
13
+
14
+ module Chartkick
15
+ class << self
16
+ attr_accessor :content_for
17
+ attr_accessor :options
18
+ end
19
+ self.options = {}
20
+ end
21
+
22
+ # for multiple series
23
+ # use Enumerable so it can be called on arrays
24
+ module Enumerable
25
+ def chart_json
26
+ if is_a?(Hash)
27
+ if (key = keys.first) && key.is_a?(Array) && key.size == 2
28
+ group_by { |k, _v| k[0] }.map do |name, data|
29
+ {name: name, data: data.map { |k, v| [k[1], v] }}
30
+ end
31
+ else
32
+ to_a
33
+ end
34
+ elsif is_a?(Array)
35
+ map do |v|
36
+ if v.is_a?(Hash) && v[:data].is_a?(Hash)
37
+ v = v.dup
38
+ v[:data] = v[:data].to_a
39
+ end
40
+ v
41
+ end
42
+ else
43
+ self
44
+ end.to_json
45
+ end
46
+ end