Skip to content

Commit 4b88756

Browse files
authored
Merge pull request #95 from Prakriti-nith/chartwrapper
Added Chartwrapper
2 parents 2a62667 + bbda094 commit 4b88756

27 files changed

+2160
-1084
lines changed

lib/daru/view/adapters/datatables.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module DatatablesAdapter
1212
# the datatables option concept.
1313
#
1414
# TODO : this docs must be improved
15-
def init_table(data=[], options={})
15+
def init_table(data=[], options={}, _user_options={})
1616
# TODO : create data array from the df and vector data. So that
1717
# we can directly use the array.(No need to create df or vector and
1818
# generate the html table using to_html)

lib/daru/view/adapters/googlecharts.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ module GooglechartsAdapter
6262
# Draw the Daru::View::PlotList object with the data as an array of
6363
# Daru::View::Plots(s) or Daru::View::Table(s) or both
6464
# combined = Daru::View::PlotList([line_chart, bar_chart])
65-
def init(data=[], options={})
65+
def init(data=[], options={}, user_options={})
6666
@table = GoogleVisualr::DataTable.new
6767
@table = get_table(data) unless data.is_a?(String)
6868
validate_url(data) if data.is_a?(String)
6969
@chart_type = extract_chart_type(options)
7070
@chart = GoogleVisualr::Interactive.const_get(
7171
@chart_type
7272
).new(@table, options)
73+
@chart.user_options = user_options
7374
@chart.data = data
7475
@chart
7576
end
@@ -114,7 +115,7 @@ def init(data=[], options={})
114115
# query = 'SELECT A, H, O, Q, R, U LIMIT 5 OFFSET 8'
115116
# data << query
116117
# chart = Daru::View::Table.new(data)
117-
def init_table(data=[], options={})
118+
def init_table(data=[], options={}, user_options={})
118119
# if `options` is something like this :
119120
# {
120121
# cols: [{id: 'task', label: 'Employee Name', type: 'string'},
@@ -130,6 +131,7 @@ def init_table(data=[], options={})
130131
# then directly DataTable is created using options. Use data=[] or nil
131132
@table = GoogleVisualr::DataTable.new(options)
132133
@table.data = data
134+
@table.user_options = user_options
133135
# When data is the URL of the spreadsheet then plot.table will
134136
# contain the empty table as the DataTable is generated in query
135137
# response in js and we can not retrieve the data from google

lib/daru/view/adapters/googlecharts/base_chart.rb

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@ class BaseChart
88
# @return [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String]
99
# Data of GoogleVisualr Chart
1010
attr_accessor :data
11+
# @return [Hash] Various options created to facilitate more features.
12+
# These will be provided by the user
13+
attr_accessor :user_options
1114

12-
# @see #GoogleVisualr::DataTable.query_response_function_name
13-
def query_response_function_name(element_id)
14-
"handleQueryResponse_#{element_id.tr('-', '_')}"
15+
# @see #GooleVisualr::DataTable.extract_option_view
16+
def extract_option_view
17+
return js_parameters(@options.delete('view')) unless @options['view'].nil?
18+
'\'\''
1519
end
1620

17-
# Generates JavaScript when data is imported from spreadsheet and renders
18-
# the Google Chart in the final HTML output when data is URL of the
19-
# google spreadsheet
21+
# Generates JavaScript function for rendering the chartwrapper
2022
#
21-
# @param data [String] URL of the google spreadsheet in the specified
22-
# format: https://developers.google.com/chart/interactive/docs
23-
# /spreadsheets
24-
# Query string can be appended to retrieve the data accordingly
25-
# @param element_id [String] The ID of the DIV element that the Google
26-
# Chart should be rendered in
27-
# @return [String] Javascript code to render the Google Chart when data is
28-
# given as the URL of the google spreadsheet
29-
def to_js_spreadsheet(data, element_id=SecureRandom.uuid)
30-
js = ''
31-
js << "\n<script type='text/javascript'>"
32-
js << load_js(element_id)
33-
js << draw_js_spreadsheet(data, element_id)
34-
js << "\n</script>"
23+
# @param (see #to_js_chart_wrapper)
24+
# @return [String] JS function to render the chartwrapper
25+
def draw_js_chart_wrapper(data, element_id)
26+
js = ''
27+
js << "\n function #{chart_function_name(element_id)}() {"
28+
js << "\n \t#{@data_table.to_js}"
29+
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
30+
js << "\n \t\tchartType: '#{chart_name}',"
31+
js << append_data(data)
32+
js << "\n \t\toptions: #{js_parameters(@options)},"
33+
js << "\n \t\tcontainerId: '#{element_id}',"
34+
js << "\n \t\tview: #{extract_option_view}"
35+
js << "\n \t});"
36+
js << draw_wrapper
37+
js << "\n };"
3538
js
3639
end
3740

lib/daru/view/adapters/googlecharts/data_table_iruby.rb

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class DataTable
1111
# options will enable us to give some styling for table.
1212
# E.g. pagination, row numbers, etc
1313
attr_accessor :options
14+
# @return [Hash] Various options created to facilitate more features.
15+
# These will be provided by the user
16+
attr_accessor :user_options
1417

1518
# included to use `js_parameters` method
1619
include GoogleVisualr::ParamHelpers
@@ -50,37 +53,10 @@ def to_js_full_script(element_id=SecureRandom.uuid)
5053
js
5154
end
5255

53-
# Generates JavaScript and renders the Google Chart DataTable in the
54-
# final HTML output when data is URL of the google spreadsheet
55-
#
56-
# @param data [String] URL of the google spreadsheet in the specified
57-
# format: https://developers.google.com/chart/interactive/docs
58-
# /spreadsheets
59-
# Query string can be appended to retrieve the data accordingly
60-
# @param element_id [String] The ID of the DIV element that the Google
61-
# Chart DataTable should be rendered in
62-
# @return [String] Javascript code to render the Google Chart DataTable
63-
# when data is given as the URL of the google spreadsheet
64-
def to_js_full_script_spreadsheet(data, element_id=SecureRandom.uuid)
65-
js = ''
66-
js << '\n<script type=\'text/javascript\'>'
67-
js << load_js(element_id)
68-
js << draw_js_spreadsheet(data, element_id)
69-
js << '\n</script>'
70-
js
71-
end
72-
7356
def chart_function_name(element_id)
7457
"draw_#{element_id.tr('-', '_')}"
7558
end
7659

77-
# @param element_id [String] The ID of the DIV element that the Google
78-
# Chart DataTable should be rendered in
79-
# @return [String] unique function name to handle query response
80-
def query_response_function_name(element_id)
81-
"handleQueryResponse_#{element_id.tr('-', '_')}"
82-
end
83-
8460
def google_table_version
8561
'1.0'.freeze
8662
end
@@ -89,6 +65,13 @@ def package_name
8965
'table'
9066
end
9167

68+
# @return [String] Returns value of the view option provided by the user
69+
# and '' otherwise
70+
def extract_option_view
71+
return js_parameters(@options.delete(:view)) unless @options[:view].nil?
72+
'\'\''
73+
end
74+
9275
# Generates JavaScript for loading the appropriate Google Visualization
9376
# package, with callback to render chart.
9477
#
@@ -119,6 +102,26 @@ def draw_js(element_id)
119102
js
120103
end
121104

105+
# Generates JavaScript function for rendering the chartwrapper
106+
#
107+
# @param (see #to_js_chart_wrapper)
108+
# @return [String] JS function to render the chartwrapper
109+
def draw_js_chart_wrapper(data, element_id)
110+
js = ''
111+
js << "\n function #{chart_function_name(element_id)}() {"
112+
js << "\n \t#{to_js}"
113+
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
114+
js << "\n \t\tchartType: 'Table',"
115+
js << append_data(data)
116+
js << "\n \t\toptions: #{js_parameters(@options)},"
117+
js << "\n \t\tcontainerId: '#{element_id}',"
118+
js << "\n \t\tview: #{extract_option_view}"
119+
js << "\n \t});"
120+
js << draw_wrapper
121+
js << "\n };"
122+
js
123+
end
124+
122125
# Generates JavaScript function for rendering the google chart table when
123126
# data is URL of the google spreadsheet
124127
#
@@ -128,14 +131,14 @@ def draw_js(element_id)
128131
def draw_js_spreadsheet(data, element_id)
129132
js = ''
130133
js << "\n function #{chart_function_name(element_id)}() {"
131-
js << "\n var query = new google.visualization.Query('#{data}');"
132-
js << "\n query.send(#{query_response_function_name(element_id)});"
134+
js << "\n var query = new google.visualization.Query('#{data}');"
135+
js << "\n query.send(#{query_response_function_name(element_id)});"
133136
js << "\n }"
134137
js << "\n function #{query_response_function_name(element_id)}(response) {"
135-
js << "\n var data_table = response.getDataTable();"
136-
js << "\n var table = new google.visualization.Table"\
138+
js << "\n var data_table = response.getDataTable();"
139+
js << "\n var table = new google.visualization.Table"\
137140
"(document.getElementById('#{element_id}'));"
138-
js << "\n table.draw(data_table, #{js_parameters(@options)});"
141+
js << "\n table.draw(data_table, #{js_parameters(@options)});"
139142
js << "\n };"
140143
js
141144
end

lib/daru/view/adapters/googlecharts/display.rb

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
require 'erb'
33
require_relative 'data_table_iruby'
44
require_relative 'base_chart'
5+
require 'daru/view/constants'
56

67
module 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\tdataSourceUrl: '#{data}'," if data.is_a? String
117+
"\n \t\tdataTable: 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 \twrapper.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

lib/daru/view/adapters/googlecharts/iruby_notebook.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'daru/view/constants'
2+
13
module GoogleVisualr
24
# generate initializing code
35
def self.generate_init_code(dependent_js)
@@ -8,7 +10,7 @@ def self.generate_init_code(dependent_js)
810
end
911

1012
# Enable to show plots on IRuby notebook
11-
def self.init_iruby(dependent_js=['google_visualr.js', 'loader.js'])
13+
def self.init_iruby(dependent_js=GOOGLECHARTS_DEPENDENCIES)
1214
js = generate_init_code(dependent_js)
1315
IRuby.display(IRuby.javascript(js))
1416
end

lib/daru/view/adapters/highcharts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module HighchartsAdapter
2828
#
2929
# @param [Array/Daru::DataFrame/Daru::Vector] data
3030
#
31-
def init(data=[], options={})
31+
def init(data=[], options={}, _user_options={})
3232
# Alternate way is using `add_series` method.
3333
#
3434
# There are many options present in Highcharts so it is better to use

lib/daru/view/adapters/nyaplot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module View
77
module Adapter
88
module NyaplotAdapter
99
extend self # rubocop:disable Style/ModuleFunction
10-
def init(data, options)
10+
def init(data, options, _user_options={})
1111
data_new = guess_data(data)
1212
data_new.plot(options)
1313
end

lib/daru/view/constants.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# HighCharts CSS dependencies
1919
HIGHCHARTS_DEPENDENCIES_CSS = ['highcharts.css'].freeze
2020

21+
# Dependent GoogleCharts JS constants for web frameworks and IRuby notebook
22+
GOOGLECHARTS_DEPENDENCIES = ['google_visualr.js', 'loader.js'].freeze
23+
2124
# Regex pattern to match a valid URL
2225
PATTERN_URL = Regexp.new(
2326
'^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$'

0 commit comments

Comments
 (0)