Module: GoogleVisualization
- Defined in:
- lib/google_visualization.rb
Overview
view helper, and view methods for using the Google Visualization API
For use with rails, include this Module in ApplicationHelper, and call #include_visualization_api and #render_visualizations within the head tag of your html layout See the Readme for examples and usage details. For more detailed info on each visualization see the API
Instance Attribute Summary collapse
-
#google_visualizations ⇒ Object
Returns the value of attribute google_visualizations.
-
#visualization_packages ⇒ Object
Returns the value of attribute visualization_packages.
Layout helper methods collapse
-
#include_visualization_api ⇒ Object
Include the Visualization API code from google.
-
#render_visualizations ⇒ String
Call this method from the within the head tag (or alternately just before the closing body tag) This will render the graph’s generated by the rendering of your view files.
View methods collapse
-
#visualization(id, chart_type, options = {}, &block) ⇒ Object
Call this method from the view to insert the visualization graph/chart here.
Instance Attribute Details
#google_visualizations ⇒ Object
Returns the value of attribute google_visualizations.
9 10 11 |
# File 'lib/google_visualization.rb', line 9 def google_visualizations @google_visualizations end |
#visualization_packages ⇒ Object
Returns the value of attribute visualization_packages.
9 10 11 |
# File 'lib/google_visualization.rb', line 9 def visualization_packages @visualization_packages end |
Instance Method Details
#include_visualization_api ⇒ Object
Include the Visualization API code from google. (Omit this call if you prefer to include the API in your own js package)
16 17 18 19 |
# File 'lib/google_visualization.rb', line 16 def include_visualization_api # Ensure we use https when the page is loaded on https so we don't make the page look insecure javascript_include_tag("//www.google.com/jsapi") end |
#render_visualizations ⇒ String
Call this method from the within the head tag (or alternately just before the closing body tag) This will render the graph’s generated by the rendering of your view files
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/google_visualization.rb', line 24 def render_visualizations if @google_visualizations package_list = @visualization_packages.uniq.collect do |p| package = p.to_s.camelize.downcase package = "corechart" if ["areachart", "barchart", "columnchart", "linechart", "piechart", "combochart"].include?(package) "\'#{package}\'" end output = %Q( <script type="text/javascript"> google.load('visualization', '1', {'packages':[#{package_list.uniq.join(',')}]}); google.setOnLoadCallback(drawCharts); var chartData = {}; var visualizationCharts = {}; function drawCharts() { ) @google_visualizations.each do |id, vis| output += generate_visualization(id, vis[0], vis[1], vis[2]) end output += "} </script>" raw(output + "<!-- Rendered Google Visualizations /-->") else raw("<!-- No graphs on this page /-->") if debugging? end end |
#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 |