Class: AmCharts::ChartBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/amcharts/chart_builder.rb,
lib/amcharts/chart_builder/function.rb

Defined Under Namespace

Classes: Function

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chart, template) ⇒ ChartBuilder

Returns a new instance of ChartBuilder.



8
9
10
11
12
13
14
15
# File 'lib/amcharts/chart_builder.rb', line 8

def initialize(chart, template)
  @chart = chart
  @template = template

  if @chart.loading_indicator? and @chart.data_source.empty?
    @chart.listeners.new(:rendered, 'AmCharts.RB.Helpers.hide_loading_indicator')
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object (private)



102
103
104
105
# File 'lib/amcharts/chart_builder.rb', line 102

def method_missing(*args, &block)
  return template.send(*args, &block) if template.respond_to?(args.first)
  super
end

Instance Attribute Details

#chartObject (readonly)

Returns the value of attribute chart.



6
7
8
# File 'lib/amcharts/chart_builder.rb', line 6

def chart
  @chart
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/amcharts/chart_builder.rb', line 6

def template
  @template
end

Instance Method Details

#render_component(component, options = {}, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/amcharts/chart_builder.rb', line 53

def render_component(component, options = {}, &block)
  return unless component
  partial_name = component.respond_to?(:each) ? 'collection' : 'object'
  template_type = block_given? ? :layout : :partial
  concat render_js(partial_name, template_type: template_type, object: component, locals: options, &block)
end

#render_dataObject



33
34
35
# File 'lib/amcharts/chart_builder.rb', line 33

def render_data
  concat render_js('data', locals: { chart: chart })
end

#render_data_sourceObject



37
38
39
40
41
42
43
# File 'lib/amcharts/chart_builder.rb', line 37

def render_data_source
  return if chart.data_source.empty?
  url = chart.data_source[:url]
  params = chart.data_source.fetch(:params, {})
  method = chart.data_source.fetch(:method, 'GET')
  concat render_js('data_source', object: url, locals: { params: params, method: method })
end

#render_exportObject



95
96
97
98
# File 'lib/amcharts/chart_builder.rb', line 95

def render_export
  return if chart.export.empty?
  concat render_js('export', object: chart.export)
end

#render_immediate_data_loadObject



45
46
47
48
49
50
51
# File 'lib/amcharts/chart_builder.rb', line 45

def render_immediate_data_load
  return if chart.data_source.empty? || chart.defer?
  url = chart.data_source[:url]
  params = chart.data_source.fetch(:params, {})
  method = chart.data_source.fetch(:method, 'GET')
  concat render_js('immediate_data_load', locals: { data_source: url, params: params, method: method })
end

#render_js(name, options, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/amcharts/chart_builder.rb', line 23

def render_js(name, options, &block)
  template_type = options.delete(:template_type) || :partial
  template_name = "amcharts/#{name}"

  options[:locals] = (options[:locals] || {}).merge(builder: self)
  options = { template_type => template_name }.merge(options)

  block_given? ? template.render(options, &block) : template.render(options)
end

#render_labelsObject



83
84
85
86
87
# File 'lib/amcharts/chart_builder.rb', line 83

def render_labels
  chart.labels.each do |(text, x, y, options)|
    concat render_js('label', locals: { text: text, x: x, y: y, options: options })
  end
end

#render_legendObject



60
61
62
63
64
65
# File 'lib/amcharts/chart_builder.rb', line 60

def render_legend
  chart.legends.each do |l|
    div = chart.legend_div == true ? "#{chart.container}_legend" : chart.legend_div
    concat render_js('legend', object: l, locals: { div: div })
  end
end

#render_listeners(object = chart, var_name = 'chart') ⇒ Object



89
90
91
92
93
# File 'lib/amcharts/chart_builder.rb', line 89

def render_listeners(object = chart, var_name = 'chart')
  object.listeners.each do |listener|
    concat render_js('listener', locals: { type: listener.type, method: listener.method, var_name: var_name })
  end
end

#render_settings(object, name, index = nil) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
# File 'lib/amcharts/chart_builder.rb', line 67

def render_settings(object, name, index = nil)
  raise ArgumentError, "given object doesn't have settings" unless object.respond_to?(:settings)

  name.concat(index.to_s) if index

  object.settings.each do |key, value|
    concat render_js('settings', locals: { name: name, key: key, value: value })
  end
end

#render_titleObject



77
78
79
80
81
# File 'lib/amcharts/chart_builder.rb', line 77

def render_title
  chart.titles.each do |(title, options)|
    concat render_js('title', locals: { title: title, options: options })
  end
end

#to_js(val) ⇒ Object



17
18
19
20
21
# File 'lib/amcharts/chart_builder.rb', line 17

def to_js(val)
  val = instance_exec(&val) if val.is_a?(Proc)
  val = t(val) if val.is_a?(Symbol)
  raw(val.to_json)
end