Method: GoogleVisualization#visualization
- Defined in:
- lib/google_visualization.rb
#visualization(id, chart_type, options = {}, &block) ⇒ Object
Call this method from the view to insert the visualization graph/chart here. This method will output a div with the specified id, and store the chart data to be rendered later via #render_visualizations
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/google_visualization.rb', line 57 def visualization(id, chart_type, = {}, &block) init chart_type = chart_type.camelize # Camelize the chart type, as the Google API follows Camel Case conventions (e.g. ColumnChart, MotionChart) .stringify_keys! # Ensure consistent hash access @visualization_packages << chart_type # Add the chart type to the packages needed to be loaded # Initialize the data table (with hashed options), and pass it the block for cleaner adding of attributes within the block table = Gvis::DataTable.new(.delete("data"), .delete("columns"), ) if block_given? yield table end = .delete("html") || {} # Extract the html options @google_visualizations.merge!(id => [chart_type, table, ]) # Store our chart in an instance variable to be rendered in the head tag # Output a div with given id on the page right now, that our graph will be embedded into html = .collect {|key,value| "#{key}=\"#{value}\"" }.join(" ") concat raw(%Q(<div id="#{escape_id(id)}" #{html}><!-- /--></div>)) nil # Return nil just incase this is called with an output erb tag, as we don't to output the html twice end |