Module: Display

Included in:
GoogleVisualr::BaseChart, GoogleVisualr::DataTable
Defined in:
lib/daru/view/adapters/googlecharts/display.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formattersObject

Returns the value of attribute formatters.



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

def formatters
  @formatters
end

#html_idString

Holds a value only when to_html method is invoked

Returns:

  • (String)

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



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_chartvoid

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 user_options && user_options[:listeners]

  user_options[: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

Parameters:

  • type (String)

    format to which chart has to be exported

  • file_name (String) (defaults to: 'chart')

    The name of the file after exporting the chart



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

Parameters:

  • dom (String)

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

Returns:

  • (String)

    JS to draw ChartWrapper or ChartEditor

See Also:

  • examples for ChartEditor in shekharrajak/demo_daru-view


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 user_options[: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

Parameters:

  • type (String)

    format to which chart has to be exported

  • file_name (String) (defaults to: 'chart')

    The name of the file after exporting the chart

Returns:

  • (String)

    the script to export the chart



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.

Parameters:

  • dom (String)

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

Returns:

  • (String)

    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.

Parameters:

  • data (Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String)

    Data of GoogleVisualr Chart or GoogleVisualr DataTable

  • dom (String)

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

Returns:

  • (String)

    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.

Parameters:

  • data (Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String)

    Data of GoogleVisualr Chart or GoogleVisualr DataTable

  • dom (String)

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

Returns:

  • (String)

    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.

Parameters:

  • data (String)

    URL of the google spreadsheet in the specified format: developers.google.com/chart/interactive/docs /spreadsheets Query string can be appended to retrieve the data accordingly

  • dom (String)

    The ID of the DIV element that the Google Chart should be rendered in when data is the the URL of the google spreadsheet

Returns:

  • (String)

    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, options={})
  script_tag = options.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 user_options && user_options[: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.

Parameters:

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

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

Returns:

  • (String)

    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 user_options &&
     user_options[: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, options={})
  path = File.expand_path(
    '../../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