Module: Daru::View::Adapter::GooglechartsAdapter

Extended by:
GooglechartsAdapter
Included in:
GooglechartsAdapter
Defined in:
lib/daru/view/adapters/googlecharts.rb

Instance Method Summary collapse

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.

See Also:

  • Daru::View::Adapter::GooglechartsAdapter#Daru#Daru::View#Daru::View::Plot#Daru::View::Plot.export


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.expand_path(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.expand_path(
    '../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/

Examples:

GoogleChart

Set Daru::View.plotting_library = :googlecharts
  (Also set Daru::View.dependent_script(:googlecharts) in web
  frameworks in head tag)
Formulate the data to visualize
  idx = Daru::Index.new ['Year', 'Sales']
  data_rows = [
                ['2004',  1000],
                ['2005',  1170],
                ['2006',  660],
                ['2007',  1030]
              ]
  df_sale_exp = Daru::DataFrame.rows(data_rows)
  df_sale_exp.vectors = idx

Set the options required
  line_options = {
    title: 'Company Performance',
    curveType: 'function',
    legend: { position: 'bottom' }
  }

Draw the Daru::View::Plot object. Default chart type is Line.
  line_chart = Daru::View::Plot.new(df_sale_exp, line_options)
  bar_chart = Daru::View::Plot.new(df_sale_exp, type: :bar)

GoogleChart with data as a link of google spreadsheet

data = 'https://docs.google.com/spreadsheets/d/1XWJLkAwch5GXAt'\
       '_7zOFDcg8Wm8Xv29_8PWuuW15qmAE/gviz/tq?gid=0&headers=1&tq='
query = 'SELECT H, O, Q, R WHERE O > 1'
data << query
options = {type: :area}
chart = Daru::View::Plot.new(data, options)

ChartEditor

data = [
      ['Year', 'Sales', 'Expenses'],
      ['2013',  1000,      400],
      ['2014',  1170,      460],
      ['2015',  660,       1120],
      ['2016',  1030,      540]
]
plot = Daru::View::Plot.new(data, {}, chart_class: 'Charteditor')

Multiple Charts in a row

Draw the Daru::View::PlotList object with the data as an array of
Daru::View::Plots(s) or Daru::View::Table(s) or both
  combined = Daru::View::PlotList([line_chart, bar_chart])

Parameters:

  • data (Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String) (defaults to: [])

    The data provided by the user to generate the google chart. Data in String format represents the URL of the google spreadsheet from which data has to invoked

  • options (Hash) (defaults to: {})

    Various options provided by the user to incorporate in google charts

Returns:

  • (GoogleVisualr::Interactive)

    Returns the chart object based on the chart_type



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/daru/view/adapters/googlecharts.rb', line 75

def init(data=[], options={}, user_options={})
  @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(options)
  @chart = GoogleVisualr::Interactive.const_get(
    @chart_type
  ).new(@table, options)
  @chart.user_options = user_options
  @chart.data = data
  @chart
end

#init_irubyObject



221
222
223
# File 'lib/daru/view/adapters/googlecharts.rb', line 221

def init_iruby
  GoogleVisualr.init_iruby
end

#init_scriptObject



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

Examples:

GoogleChart DataTable

First, set Daru::View.plotting_library = :googlecharts
  (Also set Daru::View.dependent_script(:googlecharts) in web
  frameworks in head tag)
Formulate the data to visualize
  idx = Daru::Index.new ['Year', 'Sales']
  data_rows = [
                ['2004',  1000],
                ['2005',  1170],
                ['2006',  660],
                ['2007',  1030]
              ]
  df_sale_exp = Daru::DataFrame.rows(data_rows)
  df_sale_exp.vectors = idx

Set the options required
  table_options = {
    showRowNumber: true,
    width: '100%',
    height: '100%' ,
  }

Draw the Daru::View::Table object.
  line_chart = Daru::View::Table.new(df_sale_exp, table_options)

GoogleChart Datatable with data as a link of google

spreadsheet
data = 'https://docs.google.com/spreadsheets/d/1XWJLkAwch5GXAt'\
       '_7zOFDcg8Wm8Xv29_8PWuuW15qmAE/gviz/tq?gid=0&headers=1&tq='
query = 'SELECT A, H, O, Q, R, U LIMIT 5 OFFSET 8'
data << query
chart = Daru::View::Table.new(data)

ChartEditor

data = [
      ['Year', 'Sales', 'Expenses'],
      ['2013',  1000,      400],
      ['2014',  1170,      460],
      ['2015',  660,       1120],
      ['2016',  1030,      540]
]
table = Daru::View::Table.new(data, {}, chart_class: 'Charteditor')

Parameters:

  • data (Array, Daru::DataFrame, Daru::Vector, String) (defaults to: [])

    The data provided by the user to generate the google datatable. Data in String format represents the URL of the google spreadsheet from which data has to invoked

  • options (Hash) (defaults to: {})

    Various options provided by the user to incorporate in google datatables

Returns:



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=[], options={}, user_options={})
  # 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(options)
  @table.data = data
  @table.user_options = user_options
  # 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

Parameters:

  • data (String)

    URL of the google spreadsheet from which data has to invoked

Returns:

  • (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