Module: Grafico::Helpers

Defined in:
lib/grafico/helpers.rb

Constant Summary collapse

GRAPH_TYPES =
%w[LineGraph AreaGraph StackGraph StreamGraph BarGraph StackedBarGraph HorizontalBarGraph SparkLine SparkBar]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/grafico/helpers.rb', line 12

def self.included(receiver)
  GRAPH_TYPES.each do |graph|
    receiver.instance_eval do
      define_method(:"#{graph.underscore}_tag") do |e, d, *opts|
        opts = opts.first || Hash.new
        graph_tag(graph, e, d, opts)
      end
    end
  end
end

Instance Method Details

#graph_tag(graph_type, element, data, options = {}) ⇒ Object



5
6
7
8
9
10
# File 'lib/grafico/helpers.rb', line 5

def graph_tag(graph_type, element, data, options = {})
  args = []
  args << data.to_json
  args << options.to_json unless options.empty?
  javascript_tag "var #{options[:variable_name] || (element + graph_type)} = new Grafico.#{graph_type}($('#{element}'), #{args.join(',')});"
end