Class: AmCharts::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/amcharts/chart.rb

Direct Known Subclasses

Funnel, Gauge, Pie, Radar, Rectangular

Defined Under Namespace

Classes: Funnel, Gauge, Pie, Radar, Rectangular, Serial, XY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data, &block) ⇒ Chart

Returns a new instance of Chart.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amcharts/chart.rb', line 23

def initialize(*data, &block)
  @data = data.flatten
  @data_source = DataSource.new
  @graphs = Collection[Graph]
  @legends = Collection[Legend]
  @listeners = Collection[Listener]
  @settings = Settings.new(self.class.defaults)
  @export = nil
  @titles = []
  @labels = []
  @functions = []

  update_settings(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegate unknown messages to the settings object



163
164
165
166
# File 'lib/amcharts/chart.rb', line 163

def method_missing(name, *args, &block)
  return type.send(name) if type if name.to_s.end_with?('?')
  @settings.send(name, *args, &block)
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



19
20
21
# File 'lib/amcharts/chart.rb', line 19

def container
  @container
end

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def data
  @data
end

#data_providerObject

Returns the value of attribute data_provider.



19
20
21
# File 'lib/amcharts/chart.rb', line 19

def data_provider
  @data_provider
end

#data_sourceObject

Returns the value of attribute data_source.



19
20
21
# File 'lib/amcharts/chart.rb', line 19

def data_source
  @data_source
end

#export {|@export| ... } ⇒ Object (readonly) Also known as: exportable!

Returns the value of attribute export.

Yields:



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def export
  @export
end

#functionsObject (readonly)

Returns the value of attribute functions.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def functions
  @functions
end

#graphsObject (readonly)

Returns the value of attribute graphs.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def graphs
  @graphs
end

#heightObject

Returns the value of attribute height.



20
21
22
# File 'lib/amcharts/chart.rb', line 20

def height
  @height
end

#labelsObject (readonly)

Returns the value of attribute labels.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def labels
  @labels
end

#legend_divObject (readonly)

Returns the value of attribute legend_div.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def legend_div
  @legend_div
end

#legendsObject (readonly)

Returns the value of attribute legends.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def legends
  @legends
end

#listenersObject (readonly)

Returns the value of attribute listeners.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def listeners
  @listeners
end

#loading_indicatorObject

Returns the value of attribute loading_indicator.



20
21
22
# File 'lib/amcharts/chart.rb', line 20

def loading_indicator
  @loading_indicator
end

#settingsObject (readonly)

Returns the value of attribute settings.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def settings
  @settings
end

#titlesObject (readonly)

Returns the value of attribute titles.



21
22
23
# File 'lib/amcharts/chart.rb', line 21

def titles
  @titles
end

#widthObject

Returns the value of attribute width.



20
21
22
# File 'lib/amcharts/chart.rb', line 20

def width
  @width
end

Class Method Details

.amchart_typeObject



81
82
83
# File 'lib/amcharts/chart.rb', line 81

def self.amchart_type
  "AmCharts.Am#{type.to_s.titleize}Chart()"
end

.clear_defaultsObject



49
50
51
# File 'lib/amcharts/chart.rb', line 49

def clear_defaults
  self.default_settings = nil
end

.defaults(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/amcharts/chart.rb', line 39

def defaults(&block)
  if block_given?
    self.default_settings ||= Settings.new
    instance_exec(self.default_settings, &block)
  else
    return {} if self.default_settings.blank?
    self.default_settings.to_h.symbolize_keys
  end
end

.typeObject



77
78
79
# File 'lib/amcharts/chart.rb', line 77

def self.type
  ActiveSupport::StringInquirer.new(self.name.demodulize.downcase)
end

Instance Method Details

#add_label(text, x, y, options = {}) ⇒ Object



100
101
102
# File 'lib/amcharts/chart.rb', line 100

def add_label(text, x, y, options = {})
  @labels << [text, x, y, options.reverse_merge(size: 11, align: 'left', color: '#000', rotation: 0, alpha: 1, bold: false)]
end

#add_title(text, options = {}) ⇒ Object



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

def add_title(text, options = {})
  @titles << [text, options.reverse_merge(size: 13, bold: true, alpha: 1, color: '#000')]
end

#amchart_typeObject



139
140
141
# File 'lib/amcharts/chart.rb', line 139

def amchart_type
  self.class.amchart_type
end

#call_function(fn) ⇒ Object



152
153
154
# File 'lib/amcharts/chart.rb', line 152

def call_function(fn)
  @functions << fn
end

#category_axisObject

Axes are only defined for rectangular charts



144
145
146
# File 'lib/amcharts/chart.rb', line 144

def category_axis
  nil
end

#category_fieldObject



69
70
71
# File 'lib/amcharts/chart.rb', line 69

def category_field
  @category_field || AmCharts::ChartBuilder::Function.new('new AmCharts.RB.Chart(chart).category_field()')
end

#defer?Boolean

Should remote data be loaded right away?

Returns:

  • (Boolean)


65
66
67
# File 'lib/amcharts/chart.rb', line 65

def defer?
  @data_source.fetch(:defer, false)
end

#detach_legend(div = true) ⇒ Object



127
128
129
# File 'lib/amcharts/chart.rb', line 127

def detach_legend(div = true)
  @legend_div = div
end

#dimensions=(dim) ⇒ Object



112
113
114
# File 'lib/amcharts/chart.rb', line 112

def dimensions=(dim)
  @width, @height = dim.split("x").map(&:to_i)
end

#export?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/amcharts/chart.rb', line 85

def export?
  !@export.nil?
end

#keysObject



73
74
75
# File 'lib/amcharts/chart.rb', line 73

def keys
  data.first.keys
end

#loading_indicator!Object

Show a loading indicator while the chart is rendering A listener will automatically be added to hide the indicator when the rendered event is received



119
120
121
# File 'lib/amcharts/chart.rb', line 119

def loading_indicator!
  @loading_indicator = true
end

#loading_indicator?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/amcharts/chart.rb', line 123

def loading_indicator?
  self.loading_indicator
end

#process_dataObject



131
132
133
# File 'lib/amcharts/chart.rb', line 131

def process_data
  data.each.with_index { |row, index| yield row, index }
end

#typeObject



135
136
137
# File 'lib/amcharts/chart.rb', line 135

def type
  self.class.type
end

#update_settings(&block) ⇒ Object



54
55
56
57
# File 'lib/amcharts/chart.rb', line 54

def update_settings(&block)
  instance_exec(self, &block)
  self
end

#value_axesObject



148
149
150
# File 'lib/amcharts/chart.rb', line 148

def value_axes
  []
end