22require 'erb'
33require_relative 'data_table_iruby'
44require_relative 'base_chart'
5+ require 'daru/view/constants'
56
67module GoogleVisualr
78 def self . init_script (
8- dependent_js = [ 'google_visualr.js' , 'loader.js' ]
9+ dependent_js = GOOGLECHARTS_DEPENDENCIES
910 )
1011 js = ''
1112 js << "\n <script type='text/javascript'>"
@@ -24,6 +25,10 @@ def show_script(dom=SecureRandom.uuid, options={})
2425 script_tag = options . fetch ( :script_tag ) { true }
2526 if script_tag
2627 show_script_with_script_tag ( dom )
28+ # Without checking for user_options, data as hash was not working!
29+ elsif user_options &&
30+ user_options [ :chart_class ] . to_s . capitalize == 'Chartwrapper'
31+ get_html_chart_wrapper ( data , dom )
2732 elsif data . is_a? ( String )
2833 get_html_spreadsheet ( data , dom )
2934 else
@@ -35,14 +40,14 @@ def show_script(dom=SecureRandom.uuid, options={})
3540 # Chart should be rendered in
3641 # @return [String] js code to render the chart with script tag
3742 def show_script_with_script_tag ( dom = SecureRandom . uuid )
38- # if it is data table and importing data from spreadsheet
39- if is_a? ( GoogleVisualr ::DataTable ) && data . is_a? ( String )
40- to_js_full_script_spreadsheet ( data , dom )
41- elsif is_a? ( GoogleVisualr ::DataTable )
42- to_js_full_script ( dom )
43- # Importing data from spreadsheet
43+ # if it is data table
44+ if user_options &&
45+ user_options [ :chart_class ] . to_s . capitalize == 'Chartwrapper'
46+ to_js_chart_wrapper ( data , dom )
4447 elsif data . is_a? ( String )
4548 to_js_spreadsheet ( data , dom )
49+ elsif is_a? ( GoogleVisualr ::DataTable )
50+ to_js_full_script ( dom )
4651 else
4752 to_js ( dom )
4853 end
@@ -74,6 +79,13 @@ def get_html_spreadsheet(data, dom)
7479 html
7580 end
7681
82+ def get_html_chart_wrapper ( data , dom )
83+ html = ''
84+ html << load_js ( dom )
85+ html << draw_js_chart_wrapper ( data , dom )
86+ html
87+ end
88+
7789 def to_html ( id = nil , options = { } )
7890 path = File . expand_path (
7991 '../../templates/googlecharts/chart_div.erb' , __dir__
@@ -86,7 +98,70 @@ def to_html(id=nil, options={})
8698 end
8799
88100 def show_in_iruby ( dom = SecureRandom . uuid )
89- IRuby . html ( to_html ( dom ) )
101+ IRuby . html to_html ( dom )
102+ end
103+
104+ # @param element_id [String] The ID of the DIV element that the Google
105+ # Chart/DataTable should be rendered in
106+ # @return [String] unique function name to handle query response
107+ def query_response_function_name ( element_id )
108+ "handleQueryResponse_#{ element_id . tr ( '-' , '_' ) } "
109+ end
110+
111+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
112+ # Data of GoogleVisualr DataTable/Chart
113+ # @return [String] Data option (dataSourceUrl or dataTable) required to
114+ # draw the Chartwrapper based upon the data provided.
115+ def append_data ( data )
116+ return "\n \t \t dataSourceUrl: '#{ data } '," if data . is_a? String
117+ "\n \t \t dataTable: data_table,"
118+ end
119+
120+ # So that it can be used in ChartEditor also
121+ #
122+ # @return [String] Returns string to draw the Chartwrapper and '' otherwise
123+ def draw_wrapper
124+ return "\n \t wrapper.draw();" if
125+ user_options [ :chart_class ] . to_s . capitalize == 'Chartwrapper'
126+ ''
127+ end
128+
129+ # Generates JavaScript and renders the Google Chartwrapper in the
130+ # final HTML output.
131+ #
132+ # @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
133+ # Data of GoogleVisualr Chart/DataTable
134+ # @param element_id [String] The ID of the DIV element that the Google
135+ # Chartwrapper should be rendered in
136+ # @return [String] Javascript code to render the Google Chartwrapper
137+ def to_js_chart_wrapper ( data , element_id = SecureRandom . uuid )
138+ js = ''
139+ js << "\n <script type='text/javascript'>"
140+ js << load_js ( element_id )
141+ js << draw_js_chart_wrapper ( data , element_id )
142+ js << "\n </script>"
143+ js
144+ end
145+
146+ # Generates JavaScript when data is imported from spreadsheet and renders
147+ # the Google Chart/Table in the final HTML output when data is URL of the
148+ # google spreadsheet
149+ #
150+ # @param data [String] URL of the google spreadsheet in the specified
151+ # format: https://developers.google.com/chart/interactive/docs
152+ # /spreadsheets
153+ # Query string can be appended to retrieve the data accordingly
154+ # @param element_id [String] The ID of the DIV element that the Google
155+ # Chart/Table should be rendered in
156+ # @return [String] Javascript code to render the Google Chart/Table when
157+ # data is given as the URL of the google spreadsheet
158+ def to_js_spreadsheet ( data , element_id = SecureRandom . uuid )
159+ js = ''
160+ js << "\n <script type='text/javascript'>"
161+ js << load_js ( element_id )
162+ js << draw_js_spreadsheet ( data , element_id )
163+ js << "\n </script>"
164+ js
90165 end
91166 end
92167
0 commit comments