Class: GoogleVisualr::DataTable

Inherits:
Object
  • Object
show all
Includes:
Display, GenerateJavascript, ParamHelpers
Defined in:
lib/daru/view/adapters/googlecharts/google_visualr.rb,
lib/daru/view/adapters/googlecharts/data_table_iruby.rb

Instance Attribute Summary collapse

Attributes included from Display

#formatters, #html_id

Instance Method Summary collapse

Methods included from GenerateJavascript

#add_listeners_js, #append_data, #draw_js_chart_editor, #draw_js_chart_wrapper, #draw_wrapper, #extract_chart_wrapper_options, #extract_export_png_code, #query_response_function_name, #save_chart_function_name, #to_js_chart_wrapper, #to_js_spreadsheet

Methods included from Display

#add_listener_to_chart, #export, #export_iruby, #extract_chart_wrapper_editor_js, #extract_export_code, #get_html, #get_html_chart_editor, #get_html_chart_wrapper, #get_html_spreadsheet, #show_in_iruby, #show_script, #show_script_with_script_tag, #to_html

Constructor Details

#initialize(options = {}) ⇒ DataTable

overiding the current initialze method (of the google_visualr). This might be not a good idea. But right now I need these lines in it : ‘ unless options.nil?` , `unless options.nil?` and `@options = options` Few lines is changed, to fix rubocop error.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 26

def initialize(options={})
  @cols = []
  @rows = []
  @listeners = []
  @options = options
  return if options.empty?

  new_columns(options[:cols]) unless options[:cols].nil?

  return if options[:rows].nil?

  rows = options[:rows]
  rows.each do |row|
    add_row(row[:c])
  end
end

Instance Attribute Details

#dataArray, ...

Holds a value only when generate_body or show_in_iruby method

is invoked in googlecharts.rb

Returns:

  • (Array, Daru::DataFrame, Daru::Vector, String)

    Data of GoogleVisualr DataTable



10
11
12
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 10

def data
  @data
end

#listenersObject

options will enable us to give some styling for table. E.g. pagination, row numbers, etc



13
14
15
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 13

def listeners
  @listeners
end

#optionsObject

options will enable us to give some styling for table. E.g. pagination, row numbers, etc



13
14
15
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 13

def options
  @options
end

#user_optionsHash

Returns Various options created to facilitate more features. These will be provided by the user.

Returns:

  • (Hash)

    Various options created to facilitate more features. These will be provided by the user



16
17
18
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 16

def user_options
  @user_options
end

Instance Method Details

#add_listener(event, callback) ⇒ Array

Adds a listener to the array of listeners

Parameters:

  • event (String)

    name of the event tha will be fired

  • callback (String)

    callback function name for the event

Returns:

  • (Array)

    array of listeners



48
49
50
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 48

def add_listener(event, callback)
  @listeners << {event: event.to_s, callback: callback}
end

#chart_function_name(element_id) ⇒ Object



67
68
69
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 67

def chart_function_name(element_id)
  "draw_#{element_id.tr('-', '_')}"
end

#draw_js(element_id) ⇒ String

Generates JavaScript function for rendering the google chart table.

Parameters:

  • element_id (String)

    The ID of the DIV element that the Google Chart DataTable should be rendered in

Returns:

  • (String)

    JS function to render the google chart table



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 109

def draw_js(element_id)
  js = ''
  js << "\n  function #{chart_function_name(element_id)}() {"
  js << "\n    #{to_js}"
  js << "\n    var table = new google.visualization.Table("
  js << "document.getElementById('#{element_id}'));"
  js << add_listeners_js('table')
  js << "\n    table.draw(data_table, #{js_parameters(@options)}); "
  js << "\n  };"
  js
end

#draw_js_spreadsheet(data, element_id) ⇒ String

Generates JavaScript function for rendering the google chart table when

data is URL of the google spreadsheet

Returns:

  • (String)

    JS function to render the google chart table when data is URL of the google spreadsheet



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 127

def draw_js_spreadsheet(data, element_id)
  js = ''
  js << "\n function #{chart_function_name(element_id)}() {"
  js << "\n   var query = new google.visualization.Query('#{data}');"
  js << "\n   query.send(#{query_response_function_name(element_id)});"
  js << "\n }"
  js << "\n function #{query_response_function_name(element_id)}(response) {"
  js << "\n   var data_table = response.getDataTable();"
  js << "\n   var table = new google.visualization.Table"\
        "(document.getElementById('#{element_id}'));"
  js << add_listeners_js('table')
  js << "\n 	table.draw(data_table, #{js_parameters(@options)});"
  js << "\n };"
  js
end

#extract_option_viewString

Returns value of the view option provided by the user and ” otherwise

Returns:

  • (String)

    Returns value of the view option provided by the user and ” otherwise



84
85
86
87
88
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 84

def extract_option_view
  return js_parameters(@options.delete(:view)) unless @options[:view].nil?

  '\'\''
end

#google_table_versionObject



71
72
73
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 71

def google_table_version
  '1.0'.freeze
end

#load_js(element_id) ⇒ String

Generates JavaScript for loading the appropriate Google Visualization

package, with callback to render chart.

Parameters:

  • element_id (String)

    The ID of the DIV element that the Google Chart DataTable should be rendered in

Returns:

  • (String)

    JS to load to appropriate Google Visualization package



96
97
98
99
100
101
102
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 96

def load_js(element_id)
  js = ''
  js << "\n  google.load('visualization', #{google_table_version}, "
  js << " {packages: ['#{package_name}'], callback:"
  js << " #{chart_function_name(element_id)}});"
  js
end

#package_nameObject



75
76
77
78
79
80
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 75

def package_name
  return 'table' unless
  user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor'

  'charteditor'
end

#to_js_full_script(element_id = SecureRandom.uuid) ⇒ String

Generates JavaScript and renders the Google Chart DataTable in the

final HTML output

Parameters:

  • element_id (String) (defaults to: SecureRandom.uuid)

    The ID of the DIV element that the Google Chart DataTable should be rendered in

Returns:

  • (String)

    Javascript code to render the Google Chart DataTable



58
59
60
61
62
63
64
65
# File 'lib/daru/view/adapters/googlecharts/data_table_iruby.rb', line 58

def to_js_full_script(element_id=SecureRandom.uuid)
  js =  ''
  js << '\n<script type=\'text/javascript\'>'
  js << load_js(element_id)
  js << draw_js(element_id)
  js << '\n</script>'
  js
end