Class: GoogleVisualr::BaseChart
- Inherits:
-
Object
- Object
- GoogleVisualr::BaseChart
- Includes:
- Packages, ParamHelpers
- Defined in:
- lib/google_visualr/base_chart.rb
Direct Known Subclasses
Image::BarChart, Image::LineChart, Image::PieChart, Image::SparkLine, Interactive::AnnotatedTimeLine, Interactive::AreaChart, Interactive::BarChart, Interactive::BubbleChart, Interactive::CandlestickChart, Interactive::ColumnChart, Interactive::ComboChart, Interactive::Gauge, Interactive::GeoChart, Interactive::GeoMap, Interactive::IntensityMap, Interactive::LineChart, Interactive::Map, Interactive::MotionChart, Interactive::OrgChart, Interactive::PieChart, Interactive::ScatterChart, Interactive::SteppedAreaChart, Interactive::Table, Interactive::Timeline, Interactive::TreeMap
Instance Attribute Summary collapse
-
#data_table ⇒ Object
Returns the value of attribute data_table.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
Instance Method Summary collapse
- #add_listener(event, callback) ⇒ Object
- #chart_function_name(element_id) ⇒ Object
- #chart_name ⇒ Object
-
#draw_js(element_id) ⇒ Object
Generates JavaScript function for rendering the chart.
-
#initialize(data_table, options = {}) ⇒ BaseChart
constructor
A new instance of BaseChart.
-
#load_js(element_id) ⇒ Object
Generates JavaScript for loading the appropriate Google Visualization package, with callback to render chart.
- #options ⇒ Object
- #options=(options) ⇒ Object
-
#to_js(element_id) ⇒ Object
Generates JavaScript and renders the Google Chart in the final HTML output.
Methods included from ParamHelpers
#js_parameters, #stringify_keys!, #typecast
Methods included from Packages
Constructor Details
#initialize(data_table, options = {}) ⇒ BaseChart
Returns a new instance of BaseChart.
9 10 11 12 13 |
# File 'lib/google_visualr/base_chart.rb', line 9 def initialize(data_table, ={}) @data_table = data_table send(:options=, ) @listeners = [] end |
Instance Attribute Details
#data_table ⇒ Object
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 |
#listeners ⇒ Object
Returns the value of attribute listeners.
7 8 9 |
# File 'lib/google_visualr/base_chart.rb', line 7 def listeners @listeners end |
Instance Method Details
#add_listener(event, callback) ⇒ Object
31 32 33 |
# File 'lib/google_visualr/base_chart.rb', line 31 def add_listener(event, callback) @listeners << { :event => event.to_s, :callback => callback } end |
#chart_function_name(element_id) ⇒ Object
19 20 21 |
# File 'lib/google_visualr/base_chart.rb', line 19 def chart_function_name(element_id) "draw_#{element_id.gsub('-', '_')}" end |
#chart_name ⇒ Object
15 16 17 |
# File 'lib/google_visualr/base_chart.rb', line 15 def chart_name class_name end |
#draw_js(element_id) ⇒ Object
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.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/google_visualr/base_chart.rb', line 60 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.visualization.#{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) ⇒ Object
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.
52 53 54 |
# File 'lib/google_visualr/base_chart.rb', line 52 def load_js(element_id) "\n google.load('visualization','1', {packages: ['#{package_name}'], callback: #{chart_function_name(element_id)}});" end |
#options ⇒ Object
23 24 25 |
# File 'lib/google_visualr/base_chart.rb', line 23 def @options end |
#options=(options) ⇒ Object
27 28 29 |
# File 'lib/google_visualr/base_chart.rb', line 27 def () @options = stringify_keys!() 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.
39 40 41 42 43 44 45 46 |
# File 'lib/google_visualr/base_chart.rb', line 39 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 |