Module: Daru::View::Adapter::GooglechartsAdapter
- Extended by:
- GooglechartsAdapter
- Included in:
- GooglechartsAdapter
- Defined in:
- lib/daru/view/adapters/googlecharts.rb
Instance Method Summary collapse
-
#add_series(plot, opts = {}) ⇒ Object
Generally, in opts Hash, :name, :type, :data , :center=> [X, Y], :size=> Integer, :showInLegend=> Bool, etc may present.
-
#export(plot, export_type = 'png', file_name = 'chart') ⇒ Object
Exporting a googlchart to pdf is not working in IRuby notebook because the dependency of jspdf is not properly loaded in IRuby notebook.
- #export_html_file(plot, path = './plot.html') ⇒ Object
- #generate_body(plot) ⇒ Object
- #generate_html(plot) ⇒ Object
-
#init(data = [], options = {}, user_options = {}) ⇒ GoogleVisualr::Interactive
Read : developers.google.com/chart/ to understand the google charts option concept.
- #init_iruby ⇒ Object
- #init_script ⇒ Object
-
#init_table(data = [], options = {}, user_options = {}) ⇒ GoogleVisualr::DataTable
Returns the table object.
- #show_in_iruby(plot) ⇒ Object
-
#validate_url(data) ⇒ Boolean, void
Returns true for valid URL and raises error for invalid URL.
Instance Method Details
#add_series(plot, opts = {}) ⇒ Object
Generally, in opts Hash, :name, :type, :data , :center=> [X, Y], :size=> Integer, :showInLegend=> Bool, etc may present.
227 228 229 230 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 227 def add_series(plot, opts={}) plot.series(opts) plot end |
#export(plot, export_type = 'png', file_name = 'chart') ⇒ Object
Exporting a googlchart to pdf is not working in IRuby notebook because
the dependency of jspdf is not properly loaded in IRuby notebook.
TODO: Need to find some other way to export the chart to pdf in
IRuby notebook.
201 202 203 204 205 206 207 208 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 201 def export(plot, export_type='png', file_name='chart') raise NotImplementedError, 'Not yet implemented!' unless export_type == 'png' plot.export_iruby(export_type, file_name) if defined? IRuby rescue NameError plot.export(export_type, file_name) end |
#export_html_file(plot, path = './plot.html') ⇒ Object
185 186 187 188 189 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 185 def export_html_file(plot, path='./plot.html') path = File.(path, Dir.pwd) str = generate_html(plot) File.write(path, str) end |
#generate_body(plot) ⇒ Object
181 182 183 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 181 def generate_body(plot) plot.to_html end |
#generate_html(plot) ⇒ Object
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 210 def generate_html(plot) path = File.( '../templates/googlecharts/static_html.erb', __dir__ ) template = File.read(path) chart_script = generate_body(plot) initial_script = init_script id = plot.html_id ERB.new(template).result(binding) end |
#init(data = [], options = {}, user_options = {}) ⇒ GoogleVisualr::Interactive
Read : developers.google.com/chart/ to understand the google charts option concept. and google_visualr : googlevisualr.herokuapp.com/
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 75 def init(data=[], ={}, ={}) @table = GoogleVisualr::DataTable.new @table = get_table(data) unless data.is_a?(String) validate_url(data) if data.is_a?(String) @chart_type = extract_chart_type() @chart = GoogleVisualr::Interactive.const_get( @chart_type ).new(@table, ) @chart. = @chart.data = data @chart end |
#init_iruby ⇒ Object
221 222 223 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 221 def init_iruby GoogleVisualr.init_iruby end |
#init_script ⇒ Object
177 178 179 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 177 def init_script GoogleVisualr.init_script end |
#init_table(data = [], options = {}, user_options = {}) ⇒ GoogleVisualr::DataTable
Returns the table object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 138 def init_table(data=[], ={}, ={}) # if `options` is something like this : # { # cols: [{id: 'task', label: 'Employee Name', type: 'string'}, # {id: 'startDate', label: 'Start Date', type: 'date'}], # rows: [{c:[{v: 'Mike'}, {v: new Date(2008, 1, 28), f:'February 28, 2008'}]}, # {c:[{v: 'Bob'}, {v: new Date(2007, 5, 1)}]}, # {c:[{v: 'Alice'}, {v: new Date(2006, 7, 16)}]}, # {c:[{v: 'Frank'}, {v: new Date(2007, 11, 28)}]}, # {c:[{v: 'Floyd'}, {v: new Date(2005, 3, 13)}]}, # {c:[{v: 'Fritz'}, {v: new Date(2011, 6, 1)}]} # ] # } # then directly DataTable is created using options. Use data=[] or nil @table = GoogleVisualr::DataTable.new() @table.data = data @table. = # When data is the URL of the spreadsheet then plot.table will # contain the empty table as the DataTable is generated in query # response in js and we can not retrieve the data from google # spreadsheet (@see #GoogleVisualr::DataTable.draw_js_spreadsheet) add_data_in_table(data) unless data.is_a?(String) validate_url(data) if data.is_a?(String) @table end |
#show_in_iruby(plot) ⇒ Object
191 192 193 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 191 def show_in_iruby(plot) plot.show_in_iruby end |
#validate_url(data) ⇒ Boolean, void
Returns true for valid URL and raises error for invalid URL
168 169 170 171 172 173 174 175 |
# File 'lib/daru/view/adapters/googlecharts.rb', line 168 def validate_url(data) # `PATTERN_URL.match? data` is faster but does not support older ruby # versions # For testing purpose, it is returning true return true if data.match(PATTERN_URL) raise 'Invalid URL' end |