Skip to content

Commit

Permalink
add ability to use listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
greinacker committed Aug 12, 2012
1 parent 13c76cb commit 83d12c7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/google_visualr/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class BaseChart
include GoogleVisualr::Packages
include GoogleVisualr::ParamHelpers

attr_accessor :data_table
attr_accessor :data_table, :listeners

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

Expand All @@ -19,6 +20,10 @@ def options=(options)
@options = stringify_keys!(options)
end

def add_listener(event, callback)
@listeners << { :event => event.to_s, :callback => callback }
end

# Generates JavaScript and renders the Google Chart in the final HTML output.
#
# Parameters:
Expand All @@ -29,6 +34,9 @@ def to_js(element_id)
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)});"
@listeners.each do |listener|
js << "\n google.visualization.events.addListener(chart, '#{listener[:event]}', #{listener[:callback]});"
end
js << "\n }});"
js << "\n</script>"

Expand Down

0 comments on commit 83d12c7

Please sign in to comment.