Class: GoogleVisualr::BaseChart

Inherits:
Object
  • Object
show all
Includes:
Packages, ParamHelpers
Defined in:
lib/google_visualr/base_chart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParamHelpers

#js_parameters, #stringify_keys!, #typecast

Methods included from Packages

#class_name, #package_name

Constructor Details

#initialize(data_table, options = {}) ⇒ BaseChart

Returns a new instance of BaseChart.



9
10
11
12
# File 'lib/google_visualr/base_chart.rb', line 9

def initialize(data_table, options={})
  @data_table = data_table
  send(:options=, options)
end

Instance Attribute Details

#data_tableObject

Returns the value of attribute data_table.



7
8
9
# File 'lib/google_visualr/base_chart.rb', line 7

def data_table
  @data_table
end

Instance Method Details

#optionsObject



14
15
16
# File 'lib/google_visualr/base_chart.rb', line 14

def options
  @options
end

#options=(options) ⇒ Object



18
19
20
# File 'lib/google_visualr/base_chart.rb', line 18

def options=(options)
  @options = stringify_keys!(options)
end

#to_js(element_id) ⇒ Object

Generates JavaScript and renders the Google Chart in the final HTML output.

Parameters:

*div_id            [Required] The ID of the DIV element that the Google Chart should be rendered in.


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/google_visualr/base_chart.rb', line 26

def to_js(element_id)
  js  = "\n<script type='text/javascript'>"
  js << "\n  google.load('visualization','1', {packages: ['#{package_name}'], callback: function() {"
  js << "\n    #{@data_table.to_js}"
  js << "\n    var chart = new google.visualization.#{class_name}(document.getElementById('#{element_id}'));"
  js << "\n    chart.draw(data_table, #{js_parameters(@options)});"
  js << "\n  }});"
  js << "\n</script>"

  js
end