class GoogleVisualr::BaseChart
Constants
- DEFAULT_VERSION
Attributes
data_table[RW]
language[RW]
listeners[RW]
material[RW]
version[RW]
Public Class Methods
new(data_table, options={})
click to toggle source
# File lib/google_visualr/base_chart.rb, line 10 def initialize(data_table, options={}) @data_table = data_table @listeners = [] @version = options.delete(:version) || DEFAULT_VERSION @language = options.delete(:language) @material = options.delete(:material) || false send(:options=, options) end
Public Instance Methods
add_listener(event, callback)
click to toggle source
# File lib/google_visualr/base_chart.rb, line 55 def add_listener(event, callback) @listeners << { :event => event.to_s, :callback => callback } end
chart_class()
click to toggle source
# File lib/google_visualr/base_chart.rb, line 27 def chart_class if material "charts" else "visualization" end end
chart_function_name(element_id)
click to toggle source
# File lib/google_visualr/base_chart.rb, line 43 def chart_function_name(element_id) "draw_#{element_id.gsub('-', '_')}" end
chart_name()
click to toggle source
# File lib/google_visualr/base_chart.rb, line 35 def chart_name if material class_name.gsub!("Chart", "") else class_name end end
class_name()
click to toggle source
# File lib/google_visualr/base_chart.rb, line 23 def class_name self.class.to_s.split("::").last end
draw_js(element_id)
click to toggle source
Generates JavaScript function for rendering the chart.
Parameters:
*div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
# File lib/google_visualr/base_chart.rb, line 86 def draw_js(element_id) js = "" js << "\n function #{chart_function_name(element_id)}() {" js << "\n #{@data_table.to_js}" js << "\n var chart = new google.#{chart_class}.#{chart_name}(document.getElementById('#{element_id}'));" @listeners.each do |listener| js << "\n google.visualization.events.addListener(chart, '#{listener[:event]}', #{listener[:callback]});" end js << "\n chart.draw(data_table, #{js_parameters(@options)});" js << "\n };" js end
load_js(element_id)
click to toggle source
Generates JavaScript for loading the appropriate Google Visualization package, with callback to render chart.
Parameters:
*div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
# File lib/google_visualr/base_chart.rb, line 76 def load_js(element_id) language_opt = ", language: '#{language}'" if language "\n google.load('visualization', '#{version}', {packages: ['#{package_name}']#{language_opt}, callback: #{chart_function_name(element_id)}});" end
options()
click to toggle source
# File lib/google_visualr/base_chart.rb, line 47 def options @options end
options=(options)
click to toggle source
# File lib/google_visualr/base_chart.rb, line 51 def options=(options) @options = stringify_keys!(options) end
package_name()
click to toggle source
# File lib/google_visualr/base_chart.rb, line 19 def package_name self.class.to_s.split("::").last.downcase end
to_js(element_id)
click to toggle source
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.
# File lib/google_visualr/base_chart.rb, line 63 def to_js(element_id) js = "" js << "\n<script type='text/javascript'>" js << load_js(element_id) js << draw_js(element_id) js << "\n</script>" js end