Class: GoogleVisualr::BaseChart

Inherits:
Object
  • Object
show all
Includes:
Display, GenerateJavascript
Defined in:
lib/daru/view/adapters/googlecharts/base_chart.rb,
lib/daru/view/adapters/googlecharts/google_visualr.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

Instance Attribute Details

#dataArray, ...

Holds a value only when generate_body or show_in_iruby method

is invoked in googlecharts.rb

Returns:



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

def data
  @data
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



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

def user_options
  @user_options
end

Instance Method Details

#draw_chart_js(element_id) ⇒ String

Taken from ‘draw_js` in googlevisualr. While adding the listener,

the callback code (provided by the user) should be within the function.

Parameters:

  • element_id (String)

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

Returns:

  • (String)

    JavaScript function for rendering the chart



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/daru/view/adapters/googlecharts/base_chart.rb', line 62

def draw_chart_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}"
  js << "(document.getElementById('#{element_id}'));"
  js << add_listeners_js('chart')
  js << "\n    chart.draw(data_table, #{js_parameters(@options)});"
  js << "\n  };"
  js
end

#draw_js_spreadsheet(data, element_id = SecureRandom.uuid) ⇒ String

Generates JavaScript function for rendering the chart when data is URL of

the google spreadsheet

Returns:

  • (String)

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/daru/view/adapters/googlecharts/base_chart.rb', line 40

def draw_js_spreadsheet(data, element_id=SecureRandom.uuid)
  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 chart = new google.#{chart_class}.#{chart_name}"\
        "(document.getElementById('#{element_id}'));"
  js << add_listeners_js('chart')
  js << "\n 	chart.draw(data_table, #{js_parameters(@options)});"
  js << "\n };"
  js
end

#extract_option_viewObject

See Also:

  • GoogleVisualr::BaseChart#GooleVisualr#GooleVisualr::DataTable#GooleVisualr::DataTable.extract_option_view


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

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

  '\'\''
end

#load_js_chart_editor(element_id) ⇒ String

Returns Generates JavaScript for loading the charteditor package, with callback to render ChartEditor.

Parameters:

  • element_id (String)

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

Returns:

  • (String)

    Generates JavaScript for loading the charteditor package, with callback to render ChartEditor



26
27
28
29
30
31
32
# File 'lib/daru/view/adapters/googlecharts/base_chart.rb', line 26

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