amcharts.rb 3.1.1.0.pre → 3.1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +1 -0
  3. data/README.md +70 -6
  4. data/Rakefile +4 -0
  5. data/amcharts.rb.gemspec +10 -8
  6. data/app/assets/images/amcharts/loading.gif +0 -0
  7. data/app/assets/javascripts/amcharts.js +119 -0
  8. data/app/assets/stylesheets/amcharts.css +39 -0
  9. data/app/helpers/amcharts/amcharts_helper.rb +24 -0
  10. data/app/views/amcharts/_collection.erb +5 -0
  11. data/app/views/amcharts/_data.erb +10 -0
  12. data/app/views/amcharts/_legend.erb +4 -0
  13. data/app/views/amcharts/_listener.erb +1 -0
  14. data/app/views/amcharts/_object.erb +12 -0
  15. data/app/views/amcharts/_settings.erb +1 -0
  16. data/app/views/amcharts/_title.erb +1 -0
  17. data/app/views/amcharts/chart_builder.erb +36 -0
  18. data/lib/amcharts.rb +23 -3
  19. data/lib/amcharts/axis.rb +8 -0
  20. data/lib/amcharts/axis/category.rb +5 -0
  21. data/lib/amcharts/axis/value.rb +5 -0
  22. data/lib/amcharts/chart.rb +108 -0
  23. data/lib/amcharts/chart/funnel.rb +7 -0
  24. data/lib/amcharts/chart/gauge.rb +7 -0
  25. data/lib/amcharts/chart/pie.rb +15 -0
  26. data/lib/amcharts/chart/radar.rb +7 -0
  27. data/lib/amcharts/chart/rectangular.rb +22 -0
  28. data/lib/amcharts/chart/serial.rb +10 -0
  29. data/lib/amcharts/chart/xy.rb +7 -0
  30. data/lib/amcharts/chart_builder.rb +79 -0
  31. data/lib/amcharts/chart_builder/function.rb +7 -0
  32. data/lib/amcharts/cursor.rb +5 -0
  33. data/lib/amcharts/graph.rb +14 -0
  34. data/lib/amcharts/legend.rb +5 -0
  35. data/lib/amcharts/listener.rb +10 -0
  36. data/lib/amcharts/scroll_bar.rb +5 -0
  37. data/lib/amcharts/settings.rb +32 -0
  38. data/lib/amcharts/uses_settings.rb +10 -0
  39. data/lib/amcharts/version.rb +1 -1
  40. data/spec/chart/serial_spec.rb +5 -0
  41. data/spec/chart_spec.rb +17 -0
  42. data/vendor/assets/images/amcharts/eraserIcon.gif +0 -0
  43. data/vendor/assets/images/amcharts/eraserIconH.gif +0 -0
  44. data/vendor/assets/images/amcharts/pencilIcon.gif +0 -0
  45. data/vendor/assets/images/amcharts/pencilIconH.gif +0 -0
  46. data/vendor/assets/images/amcharts/xIcon.gif +0 -0
  47. data/vendor/assets/images/amcharts/xIconH.gif +0 -0
  48. data/vendor/assets/javascripts/amcharts/all.js +10 -0
  49. data/vendor/assets/javascripts/amcharts/amstock.js +86 -0
  50. data/vendor/assets/javascripts/amcharts/changeLog.txt +887 -0
  51. data/vendor/assets/javascripts/amcharts/licence.txt +16 -0
  52. data/vendor/assets/javascripts/amcharts/pie.js +1 -1
  53. data/vendor/assets/javascripts/amcharts/thirdPartySoftwareList.txt +11 -0
  54. data/vendor/assets/stylesheets/amcharts/style.css +58 -0
  55. metadata +81 -5
@@ -0,0 +1,8 @@
1
+ module AmCharts
2
+ class Axis
3
+ autoload :Category, 'amcharts/axis/category'
4
+ autoload :Value, 'amcharts/axis/value'
5
+
6
+ include UsesSettings
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module AmCharts
2
+ class Axis::Category < Axis
3
+
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module AmCharts
2
+ class Axis::Value < Axis
3
+
4
+ end
5
+ end
@@ -0,0 +1,108 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'active_support/string_inquirer'
3
+ require 'active_support/core_ext/array/access'
4
+ require 'amcharts'
5
+
6
+ module AmCharts
7
+ class Chart
8
+ autoload :Funnel, 'amcharts/chart/funnel'
9
+ autoload :Gauge, 'amcharts/chart/gauge'
10
+ autoload :Pie, 'amcharts/chart/pie'
11
+ autoload :Radar, 'amcharts/chart/radar'
12
+ autoload :Rectangular, 'amcharts/chart/rectangular'
13
+ autoload :Serial, 'amcharts/chart/serial'
14
+ autoload :XY, 'amcharts/chart/xy'
15
+
16
+ attr_accessor :data_provider, :container
17
+ attr_accessor :width, :height, :loading_indicator
18
+ attr_reader :titles, :graphs, :legends, :data, :settings, :listeners, :legend_div
19
+
20
+ def initialize(*data, &block)
21
+ @data = data.flatten
22
+ @graphs = Collection[Graph]
23
+ @legends = Collection[Legend]
24
+ @listeners = Collection[Listener]
25
+ @settings = Settings.new
26
+ @titles = []
27
+ instance_exec(self, &block) if block_given?
28
+ end
29
+
30
+ def category_field
31
+ @category_field || data.first.keys.first
32
+ end
33
+
34
+ def keys
35
+ data.first.keys
36
+ end
37
+
38
+ def self.type
39
+ ActiveSupport::StringInquirer.new(self.name.demodulize.downcase)
40
+ end
41
+
42
+ def self.amchart_type
43
+ "AmCharts.Am#{type.to_s.titleize}Chart()"
44
+ end
45
+
46
+ def add_title(text, options = {})
47
+ @titles << [text, options.reverse_merge(size: 13, bold: true, alpha: 1, color: '#000000')]
48
+ end
49
+
50
+ def width=(w)
51
+ @width = get_dimension_value(w)
52
+ end
53
+
54
+ def height=(h)
55
+ @height = get_dimension_value(h)
56
+ end
57
+
58
+ def dimensions=(dim)
59
+ @width, @height = dim.split("x").map(&:to_i)
60
+ end
61
+
62
+ def loading_indicator!
63
+ @loading_indicator = true
64
+ listeners.new(:rendered, :hide_loading_indicator)
65
+ end
66
+
67
+ def loading_indicator?
68
+ self.loading_indicator
69
+ end
70
+
71
+ def detach_legend(div = true)
72
+ @legend_div = div
73
+ end
74
+
75
+ def process_data
76
+ data.each { |row| yield row }
77
+ end
78
+
79
+ def type
80
+ self.class.type
81
+ end
82
+
83
+ def amchart_type
84
+ self.class.amchart_type
85
+ end
86
+
87
+ # Axes are only defined for rectangular charts
88
+ def category_axis
89
+ nil
90
+ end
91
+
92
+ def value_axes
93
+ []
94
+ end
95
+
96
+ private
97
+
98
+ def get_dimension_value(value)
99
+ value.respond_to?(:call) ? value.call(data) : value
100
+ end
101
+
102
+ # Delegate unknown messages to the settings object
103
+ def method_missing(name, *args, &block)
104
+ return type.send(name) if type if name.to_s.end_with?('?')
105
+ @settings.send(name, *args, &block)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,7 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Funnel < Chart
5
+ # Stub class for Funnel charts
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Gauge < Chart
5
+ # Stub class for Gauge charts
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Pie < Chart
5
+ attr_accessor :value_field, :title_field
6
+
7
+ def title_field
8
+ @title_field || data.first.keys.first
9
+ end
10
+
11
+ def value_field
12
+ @value_field || data.first.keys.second
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Radar < Chart
5
+ # Stub class for Radar charts
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Rectangular < Chart
5
+ attr_reader :value_axes
6
+
7
+ def initialize(*)
8
+ @value_axes = Collection[Axis::Value]
9
+ super
10
+ end
11
+
12
+ def cursor(&block)
13
+ return @cursor unless block_given?
14
+ @cursor ||= Cursor.new(&block)
15
+ end
16
+
17
+ def scrollbar(&block)
18
+ return @scrollbar unless block_given?
19
+ @scrollbar ||= ScrollBar.new(&block)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::Serial < Chart::Rectangular
5
+ def category_axis(&block)
6
+ return @category_axis unless block_given?
7
+ @category_axis ||= Axis::Category.new(&block)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'amcharts/chart'
2
+
3
+ module AmCharts
4
+ class Chart::XY < Chart::Rectangular
5
+ # Stub class for XY charts
6
+ end
7
+ end
@@ -0,0 +1,79 @@
1
+ module AmCharts
2
+ class ChartBuilder
3
+ autoload :Function, 'amcharts/chart_builder/function'
4
+
5
+ attr_reader :template, :chart
6
+
7
+ def initialize(chart, template)
8
+ @chart = chart
9
+ @template = template
10
+ end
11
+
12
+ def to_js(val)
13
+ val = instance_exec(&val) if val.is_a?(Proc)
14
+ val = t(val) if val.is_a?(Symbol)
15
+ raw(val.to_json)
16
+ end
17
+
18
+ def render_js(name, options, &block)
19
+ template_type = options.delete(:template_type) || :partial
20
+ template_name = "amcharts/#{name}"
21
+
22
+ options[:locals] = options[:locals].merge(builder: self)
23
+ options = { template_type => template_name }.merge(options)
24
+
25
+ block_given? ? template.render(options, &block) : template.render(options)
26
+ end
27
+
28
+ def render_data
29
+ concat render_js('data', locals: { chart: chart })
30
+ end
31
+
32
+ def render_component(component, options = {}, &block)
33
+ partial_name = component.respond_to?(:each) ? 'collection' : 'object'
34
+ template_type = block_given? ? :layout : :partial
35
+ concat render_js(partial_name, template_type: template_type, object: component, locals: options, &block)
36
+ end
37
+
38
+ def render_legend
39
+ chart.legends.each do |l|
40
+ div = chart.legend_div == true ? "#{chart.container}_legend" : chart.legend_div
41
+ concat render_js('legend',object: l, locals: { div: div })
42
+ end
43
+ end
44
+
45
+ def render_settings(object, name, index = nil)
46
+ raise ArgumentError, "given object doesn't have settings" unless object.respond_to?(:settings)
47
+
48
+ name.concat(index.to_s) if index
49
+
50
+ object.settings.each do |key, value|
51
+ concat render_js('settings', locals: { name: name, key: key, value: value })
52
+ end
53
+ end
54
+
55
+ def render_title
56
+ chart.titles.each do |(title, options)|
57
+ concat render_js('title', locals: { title: title, options: options })
58
+ end
59
+ end
60
+
61
+ def render_listeners()
62
+ chart.listeners.each do |listener|
63
+ concat render_js('listener', locals: { type: listener.type, method: listener.method })
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def method_missing(*args, &block)
70
+ return template.send(*args, &block) if template.respond_to?(args.first)
71
+ super
72
+ end
73
+
74
+ def respond_to_missing?(*args)
75
+ return true if template.respond_to?(*args)
76
+ super
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ module AmCharts
2
+ ChartBuilder::Function = Struct.new(:name) do
3
+ def to_json(options = {})
4
+ name.to_s
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module AmCharts
2
+ class Cursor
3
+ include UsesSettings
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module AmCharts
2
+ class Graph
3
+ attr_accessor :value_field, :type
4
+ attr_accessor :chart
5
+
6
+ include UsesSettings
7
+
8
+ def initialize(value_field, type)
9
+ super
10
+ @value_field = value_field
11
+ @type = type
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module AmCharts
2
+ class Legend
3
+ include UsesSettings
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ module AmCharts
2
+ class Listener
3
+ attr_accessor :type, :method
4
+
5
+ def initialize(type, method)
6
+ @type = type
7
+ @method = method
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module AmCharts
2
+ class ScrollBar
3
+ include UsesSettings
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+
4
+ module AmCharts
5
+ class Settings
6
+ delegate :[], :each, :fetch, to: :@settings
7
+
8
+ def initialize
9
+ @settings = {}.with_indifferent_access
10
+ end
11
+
12
+ def function(name)
13
+ ChartBuilder::Function.new(name)
14
+ end
15
+
16
+ def method_missing(name, *args, &block)
17
+ if block_given?
18
+ @settings[name] = block.call(*args)
19
+ elsif name.to_s.end_with?('=') and args.length == 1
20
+ prefix = name.to_s.gsub(/=\z/, '')
21
+ @settings[prefix] = args.first
22
+ elsif !args.empty?
23
+ @settings[name] = args.length == 1 ? args.first : args
24
+ elsif name.to_s.end_with?('?')
25
+ prefix = name.to_s.gsub(/\?\z/, '')
26
+ @settings.key?(prefix)
27
+ else
28
+ @settings[name]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module AmCharts
2
+ module UsesSettings
3
+ attr_reader :settings
4
+
5
+ def initialize(*)
6
+ @settings = Settings.new
7
+ yield @settings if block_given?
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module AmCharts
2
- VERSION = "3.1.1.0.pre"
2
+ VERSION = "3.1.1.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ require 'amcharts/chart/serial'
2
+
3
+ describe AmCharts::Chart::Serial do
4
+ its(:type) { should be_serial }
5
+ end
@@ -0,0 +1,17 @@
1
+ require 'amcharts/chart'
2
+
3
+ describe AmCharts::Chart do
4
+ describe "#dimensions" do
5
+ context "setting width and height with the same assignment" do
6
+ before { subject.dimensions = "800x600" }
7
+
8
+ its(:width) { should == 800 }
9
+ its(:height) { should == 600 }
10
+ end
11
+
12
+ context "not setting width and height" do
13
+ its(:width) { should be_nil }
14
+ its(:height) { should be_nil }
15
+ end
16
+ end
17
+ end