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

Author:

  • Jeremy Olliver

Instance Attribute Summary collapse

Layout helper methods collapse

View methods collapse

Instance Attribute Details

#google_visualizationsObject

Returns the value of attribute google_visualizations.



9
10
11
# File 'lib/google_visualization.rb', line 9

def google_visualizations
  @google_visualizations
end

#visualization_packagesObject

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_apiObject

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_visualizationsString

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

Returns:

  • (String)

    a javascript tag that contains the generated javascript to render the graphs



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

Parameters:

  • id (String)

    the id of the chart. corresponds to the id of the div

  • chart_type (String)

    the kind of chart to render

  • options (Hash) (defaults to: {})

    the configuration options for the graph



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, options = {}, &block)
  init
  chart_type = chart_type.camelize      # Camelize the chart type, as the Google API follows Camel Case conventions (e.g. ColumnChart, MotionChart)
  options.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(options.delete("data"), options.delete("columns"), options)
  if block_given?
    yield table
  end

  html_options = options.delete("html") || {} # Extract the html options
  @google_visualizations.merge!(id => [chart_type, table, options]) # 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 = html_options.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