Module: Display
- Included in:
- GoogleVisualr::BaseChart, GoogleVisualr::DataTable
- Defined in:
- lib/daru/view/adapters/googlecharts/display.rb
Instance Attribute Summary collapse
-
#formatters ⇒ Object
Returns the value of attribute formatters.
-
#html_id ⇒ String
Holds a value only when to_html method is invoked.
Instance Method Summary collapse
-
#add_listener_to_chart ⇒ void
Adds listener to the chart from the user_options.
- #export(export_type = 'png', file_name = 'chart') ⇒ Object
-
#export_iruby(export_type = 'png', file_name = 'chart') ⇒ void
Exports chart to different formats in IRuby notebook.
-
#extract_chart_wrapper_editor_js(dom) ⇒ String
ChartEditor and ChartWrapper works for both Google Charts and DataTables.
-
#extract_export_code(export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats.
-
#get_html(dom) ⇒ String
Js code to render the chart.
-
#get_html_chart_editor(data, dom) ⇒ String
Js code to render the charteditor.
-
#get_html_chart_wrapper(data, dom) ⇒ String
Js code to render the chart.
-
#get_html_spreadsheet(data, dom) ⇒ String
Js code to render the chart when data is the URL of the google spreadsheet.
- #show_in_iruby(dom = SecureRandom.uuid) ⇒ Object
- #show_script(dom = SecureRandom.uuid, options = {}) ⇒ Object
-
#show_script_with_script_tag(dom = SecureRandom.uuid) ⇒ String
Js code to render the chart with script tag.
- #to_html(id = nil, options = {}) ⇒ Object
Instance Attribute Details
#formatters ⇒ Object
Returns the value of attribute formatters.
13 14 15 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 13 def formatters @formatters end |
#html_id ⇒ String
Holds a value only when to_html method is invoked
12 13 14 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 12 def html_id @html_id end |
Instance Method Details
#add_listener_to_chart ⇒ void
This method returns an undefined value.
Returns Adds listener to the chart from the user_options.
168 169 170 171 172 173 174 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 168 def add_listener_to_chart return unless && [:listeners] [:listeners].each do |event, callback| add_listener(event.to_s.downcase, callback) end end |
#export(export_type = 'png', file_name = 'chart') ⇒ Object
123 124 125 126 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 123 def export(export_type='png', file_name='chart') add_listener('ready', extract_export_code(export_type, file_name)) to_html end |
#export_iruby(export_type = 'png', file_name = 'chart') ⇒ void
This method returns an undefined value.
Exports chart to different formats in IRuby notebook
134 135 136 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 134 def export_iruby(export_type='png', file_name='chart') IRuby.html(export(export_type, file_name)) end |
#extract_chart_wrapper_editor_js(dom) ⇒ String
ChartEditor and ChartWrapper works for both Google Charts and DataTables
55 56 57 58 59 60 61 62 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 55 def extract_chart_wrapper_editor_js(dom) case [:chart_class].to_s.capitalize when 'Chartwrapper' get_html_chart_wrapper(data, dom) when 'Charteditor' get_html_chart_editor(data, dom) end end |
#extract_export_code(export_type = 'png', file_name = 'chart') ⇒ String
Returns the script to export the chart in different formats
143 144 145 146 147 148 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 143 def extract_export_code(export_type='png', file_name='chart') case export_type when 'png' extract_export_png_code(file_name) end end |
#get_html(dom) ⇒ String
Returns js code to render the chart.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 67 def get_html(dom) html = '' html << load_js(dom) html << if is_a?(GoogleVisualr::DataTable) draw_js(dom) else draw_chart_js(dom) end html end |
#get_html_chart_editor(data, dom) ⇒ String
Returns js code to render the charteditor.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 95 def get_html_chart_editor(data, dom) html = '' html << if is_a?(GoogleVisualr::DataTable) load_js(dom) else load_js_chart_editor(dom) end html << draw_js_chart_editor(data, dom) html end |
#get_html_chart_wrapper(data, dom) ⇒ String
Returns js code to render the chart.
83 84 85 86 87 88 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 83 def get_html_chart_wrapper(data, dom) html = '' html << load_js(dom) html << draw_js_chart_wrapper(data, dom) html end |
#get_html_spreadsheet(data, dom) ⇒ String
Returns js code to render the chart when data is the URL of the google spreadsheet.
115 116 117 118 119 120 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 115 def get_html_spreadsheet(data, dom) html = '' html << load_js(dom) html << draw_js_spreadsheet(data, dom) html end |
#show_in_iruby(dom = SecureRandom.uuid) ⇒ Object
162 163 164 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 162 def show_in_iruby(dom=SecureRandom.uuid) IRuby.html to_html(dom) end |
#show_script(dom = SecureRandom.uuid, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 15 def show_script(dom=SecureRandom.uuid, ={}) script_tag = .fetch(:script_tag) { true } # To understand different formatters see: # https://developers.google.com/chart/interactive/docs/reference#formatters apply_formatters if is_a?(GoogleVisualr::DataTable) if script_tag show_script_with_script_tag(dom) # Without checking for user_options, data as hash was not working! elsif && [:chart_class] extract_chart_wrapper_editor_js(dom) elsif data.is_a?(String) get_html_spreadsheet(data, dom) else get_html(dom) end end |
#show_script_with_script_tag(dom = SecureRandom.uuid) ⇒ String
Returns js code to render the chart with script tag.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 35 def show_script_with_script_tag(dom=SecureRandom.uuid) # if it is data table if && [:chart_class].to_s.capitalize == 'Chartwrapper' to_js_chart_wrapper(data, dom) elsif data.is_a?(String) to_js_spreadsheet(data, dom) elsif is_a?(GoogleVisualr::DataTable) to_js_full_script(dom) else to_js(dom) end end |
#to_html(id = nil, options = {}) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/daru/view/adapters/googlecharts/display.rb', line 150 def to_html(id=nil, ={}) path = File.( '../../templates/googlecharts/chart_div.erb', __dir__ ) template = File.read(path) id ||= SecureRandom.uuid @html_id = id add_listener_to_chart chart_script = show_script(id, script_tag: false) ERB.new(template).result(binding) end |