Module: GenerateJavascript

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

Instance Method Summary collapse

Instance Method Details

#add_listeners_js(type) ⇒ String

Returns js function to add the listener to the chart.

Returns:

  • (String)

    js function to add the listener to the chart



3
4
5
6
7
8
9
10
11
12
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 3

def add_listeners_js(type)
  js = ''
  @listeners.each do |listener|
    js << "\n    google.visualization.events.addListener("
    js << "#{type}, '#{listener[:event]}', function (e) {"
    js << "\n      #{listener[:callback]}"
    js << "\n    });"
  end
  js
end

#append_data(data) ⇒ String

Returns Data option (dataSourceUrl or dataTable) required to draw the Chartwrapper based upon the data provided.

Parameters:

Returns:

  • (String)

    Data option (dataSourceUrl or dataTable) required to draw the Chartwrapper based upon the data provided.



25
26
27
28
29
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 25

def append_data(data)
  return "\n  \t\tdataSourceUrl: '#{data}'," if data.is_a? String

  "\n  \t\tdataTable: data_table,"
end

#draw_js_chart_editor(data, element_id) ⇒ String

Returns JS function to render the ChartEditor.

Parameters:

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

    Data of GoogleVisualr Chart

  • element_id (String)

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

Returns:

  • (String)

    JS function to render the ChartEditor



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 148

def draw_js_chart_editor(data, element_id)
  js = ''
  js << "\n  var chartEditor_#{element_id.tr('-', '_')} = null;"
  js << draw_js_chart_wrapper(data, element_id)
  js << "\n  function #{save_chart_function_name(element_id)}(){"
  js << "\n  \tchartEditor_#{element_id.tr('-', '_')}.getChartWrapper()."\
        "draw(document.getElementById('#{element_id}'));"
  js << "\n  }"
  js << "\n  function loadEditor_#{element_id.tr('-', '_')}(){"
  js << "\n  \tchartEditor_#{element_id.tr('-', '_')}.openDialog("\
        "wrapper_#{element_id.tr('-', '_')}, {});"
  js << "\n  }"
  js
end

#draw_js_chart_wrapper(data, element_id) ⇒ String

Generates JavaScript function for rendering the chartwrapper

Parameters:

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

    Data of GoogleVisualr Chart/DataTable

  • element_id (String)

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

Returns:

  • (String)

    JS function to render the chartwrapper



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

def draw_js_chart_wrapper(data, element_id)
  js = "\n  var wrapper_#{element_id.tr('-', '_')} = null;"
  js << "\n  function #{chart_function_name(element_id)}() {"
  js << if is_a?(GoogleVisualr::DataTable)
          "\n  \t#{to_js}"
        else
          "\n  \t#{@data_table.to_js}"
        end
  js << "\n  \twrapper_#{element_id.tr('-', '_')} = "\
        'new google.visualization.ChartWrapper({'
  js << extract_chart_wrapper_options(data, element_id)
  js << "\n  \t});"
  js << draw_wrapper(element_id)
  js << add_listeners_js("wrapper_#{element_id.tr('-', '_')}")
  js << "\n  };"
  js
end

#draw_wrapper(element_id) ⇒ String

So that it can be used in ChartEditor also

Returns:

  • (String)

    Returns string to draw the Chartwrapper and ” otherwise



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 57

def draw_wrapper(element_id)
  js = ''
  js << "\n  \twrapper_#{element_id.tr('-', '_')}.draw();"
  unless user_options[:chart_class].to_s.capitalize == 'Chartwrapper'
    js << "\n  \tchartEditor_#{element_id.tr('-', '_')} = "\
          'new google.visualization.ChartEditor();'
    js << "\n  \tgoogle.visualization.events.addListener("\
          "chartEditor_#{element_id.tr('-', '_')},"\
          " 'ok', #{save_chart_function_name(element_id)});"
  end
  js
end

#extract_chart_wrapper_options(data, element_id) ⇒ String

Returns options of the ChartWrapper.

Parameters:

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

    Data of GoogleVisualr Chart

  • element_id (String)

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

Returns:

  • (String)

    options of the ChartWrapper



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

def extract_chart_wrapper_options(data, element_id)
  js = ''
  js << if is_a?(GoogleVisualr::DataTable)
          "\n  \t\tchartType: 'Table',"
        else
          "\n  \t\tchartType: '#{chart_name}',"
        end
  js << append_data(data)
  js << "\n  \t\toptions: #{js_parameters(@options)},"
  js << "\n  \t\tcontainerId: '#{element_id}',"
  js << "\n  \t\tview: #{extract_option_view}"
  js
end

#extract_export_png_code(file_name) ⇒ String

Returns the script to export the chart in png format.

Parameters:

  • file_name (String)

    The name of the file after exporting the chart

Returns:

  • (String)

    the script to export the chart in png format



72
73
74
75
76
77
78
79
80
81
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 72

def extract_export_png_code(file_name)
  js = ''
  js << "\n \tvar a = document.createElement('a');"
  js << "\n \ta.href = chart.getImageURI();"
  js << "\n \ta.download = '#{file_name}.png';"
  js << "\n \tdocument.body.appendChild(a);"
  js << "\n \ta.click();"
  js << "\n \tdocument.body.removeChild(a);"
  js
end

#query_response_function_name(element_id) ⇒ String

Returns unique function name to handle query response.

Parameters:

  • element_id (String)

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

Returns:

  • (String)

    unique function name to handle query response



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

def query_response_function_name(element_id)
  "handleQueryResponse_#{element_id.tr('-', '_')}"
end

#save_chart_function_name(element_id) ⇒ String

Returns unique function name to save the chart.

Parameters:

  • element_id (String)

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

Returns:

  • (String)

    unique function name to save the chart



34
35
36
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 34

def save_chart_function_name(element_id)
  "saveChart_#{element_id.tr('-', '_')}"
end

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

Generates JavaScript and renders the Google Chartwrapper in the

final HTML output.

Parameters:

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

    Data of GoogleVisualr Chart/DataTable

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

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

Returns:

  • (String)

    Javascript code to render the Google Chartwrapper



91
92
93
94
95
96
97
98
# File 'lib/daru/view/adapters/googlecharts/generate_javascript.rb', line 91

def to_js_chart_wrapper(data, element_id=SecureRandom.uuid)
  js =  ''
  js << "\n<script type='text/javascript'>"
  js << load_js(element_id)
  js << draw_js_chart_wrapper(data, element_id)
  js << "\n</script>"
  js
end

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

Generates JavaScript when data is imported from spreadsheet and renders

the Google Chart/Table in the final HTML output when data is 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

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

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

Returns:

  • (String)

    Javascript code to render the Google Chart/Table when data is given as the URL of the google spreadsheet



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

def to_js_spreadsheet(data, element_id=SecureRandom.uuid)
  js =  ''
  js << "\n<script type='text/javascript'>"
  js << load_js(element_id)
  js << draw_js_spreadsheet(data, element_id)
  js << "\n</script>"
  js
end