Class: GoogleVisualr::BaseChart
- Inherits:
-
Object
- Object
- GoogleVisualr::BaseChart
- Includes:
- ParamHelpers
- Defined in:
- lib/google_visualr/base_chart.rb
Direct Known Subclasses
Image::BarChart, Image::LineChart, Image::PieChart, Image::SparkLine, Interactive::AnnotatedTimeLine, Interactive::AnnotationChart, Interactive::AreaChart, Interactive::BarChart, Interactive::BubbleChart, Interactive::Calendar, Interactive::CandlestickChart, Interactive::ColumnChart, Interactive::ComboChart, Interactive::GanttChart, Interactive::Gauge, Interactive::GeoChart, Interactive::GeoMap, Interactive::Histogram, Interactive::IntensityMap, Interactive::LineChart, Interactive::Map, Interactive::MotionChart, Interactive::OrgChart, Interactive::PieChart, Interactive::Sankey, Interactive::ScatterChart, Interactive::SteppedAreaChart, Interactive::Table, Interactive::Timeline, Interactive::TreeMap, Interactive::WordTree
Constant Summary collapse
- DEFAULT_VERSION =
"1.0".freeze
Instance Attribute Summary collapse
-
#data_table ⇒ Object
Returns the value of attribute data_table.
-
#language ⇒ Object
Returns the value of attribute language.
-
#listeners ⇒ Object
Returns the value of attribute listeners.
-
#material ⇒ Object
Returns the value of attribute material.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #add_listener(event, callback) ⇒ Object
- #chart_class ⇒ Object
- #chart_function_name(element_id) ⇒ Object
- #chart_name ⇒ Object
- #class_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
- #package_name ⇒ 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
Constructor Details
#initialize(data_table, options = {}) ⇒ BaseChart
Returns a new instance of BaseChart.
10 11 12 13 14 15 16 17 |
# File 'lib/google_visualr/base_chart.rb', line 10 def initialize(data_table, ={}) @data_table = data_table @listeners = [] @version = .delete(:version) || DEFAULT_VERSION @language = .delete(:language) @material = .delete(:material) || false send(:options=, ) end |
Instance Attribute Details
#data_table ⇒ Object
Returns the value of attribute data_table.
8 9 10 |
# File 'lib/google_visualr/base_chart.rb', line 8 def data_table @data_table end |
#language ⇒ Object
Returns the value of attribute language.
8 9 10 |
# File 'lib/google_visualr/base_chart.rb', line 8 def language @language end |
#listeners ⇒ Object
Returns the value of attribute listeners.
8 9 10 |
# File 'lib/google_visualr/base_chart.rb', line 8 def listeners @listeners end |
#material ⇒ Object
Returns the value of attribute material.
8 9 10 |
# File 'lib/google_visualr/base_chart.rb', line 8 def material @material end |
#version ⇒ Object
Returns the value of attribute version.
8 9 10 |
# File 'lib/google_visualr/base_chart.rb', line 8 def version @version end |
Instance Method Details
#add_listener(event, callback) ⇒ Object
55 56 57 |
# File 'lib/google_visualr/base_chart.rb', line 55 def add_listener(event, callback) @listeners << { :event => event.to_s, :callback => callback } end |
#chart_class ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/google_visualr/base_chart.rb', line 27 def chart_class if material "charts" else "visualization" end end |
#chart_function_name(element_id) ⇒ Object
43 44 45 |
# File 'lib/google_visualr/base_chart.rb', line 43 def chart_function_name(element_id) "draw_#{element_id.gsub('-', '_')}" end |
#chart_name ⇒ Object
35 36 37 38 39 40 41 |
# 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 ⇒ Object
23 24 25 |
# File 'lib/google_visualr/base_chart.rb', line 23 def class_name self.class.to_s.split("::").last 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.
86 87 88 89 90 91 92 93 94 95 96 97 |
# 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) ⇒ 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.
76 77 78 79 80 |
# 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 ⇒ Object
47 48 49 |
# File 'lib/google_visualr/base_chart.rb', line 47 def @options end |
#options=(options) ⇒ Object
51 52 53 |
# File 'lib/google_visualr/base_chart.rb', line 51 def () @options = stringify_keys!() end |
#package_name ⇒ Object
19 20 21 |
# File 'lib/google_visualr/base_chart.rb', line 19 def package_name self.class.to_s.split("::").last.downcase 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.
63 64 65 66 67 68 69 70 |
# 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 |