Skip to content

Commit 6c5b0e7

Browse files
authored
Merge pull request #96 from Prakriti-nith/charteditor_wrapper
Added Charteditor
2 parents f965fbb + 5de4ed7 commit 6c5b0e7

36 files changed

+3264
-2545
lines changed

lib/daru/view/adapters/googlecharts.rb

+38-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'google_visualr'
22
require_relative 'googlecharts/iruby_notebook'
3-
require_relative 'googlecharts/display'
3+
require_relative 'googlecharts/google_visualr'
44
require 'daru'
55
require 'bigdecimal'
66
require 'daru/view/constants'
@@ -58,6 +58,16 @@ module GooglechartsAdapter
5858
# options = {type: :area}
5959
# chart = Daru::View::Plot.new(data, options)
6060
#
61+
# @example ChartEditor
62+
# data = [
63+
# ['Year', 'Sales', 'Expenses'],
64+
# ['2013', 1000, 400],
65+
# ['2014', 1170, 460],
66+
# ['2015', 660, 1120],
67+
# ['2016', 1030, 540]
68+
# ]
69+
# plot = Daru::View::Plot.new(data, {}, chart_class: 'Charteditor')
70+
#
6171
# @example Multiple Charts in a row
6272
# Draw the Daru::View::PlotList object with the data as an array of
6373
# Daru::View::Plots(s) or Daru::View::Table(s) or both
@@ -115,6 +125,16 @@ def init(data=[], options={}, user_options={})
115125
# query = 'SELECT A, H, O, Q, R, U LIMIT 5 OFFSET 8'
116126
# data << query
117127
# chart = Daru::View::Table.new(data)
128+
#
129+
# @example ChartEditor
130+
# data = [
131+
# ['Year', 'Sales', 'Expenses'],
132+
# ['2013', 1000, 400],
133+
# ['2014', 1170, 460],
134+
# ['2015', 660, 1120],
135+
# ['2016', 1030, 540]
136+
# ]
137+
# table = Daru::View::Table.new(data, {}, chart_class: 'Charteditor')
118138
def init_table(data=[], options={}, user_options={})
119139
# if `options` is something like this :
120140
# {
@@ -141,23 +161,6 @@ def init_table(data=[], options={}, user_options={})
141161
@table
142162
end
143163

144-
# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
145-
# The data provided by the user to generate the google datatable.
146-
# Data in String format represents the URL of the google spreadsheet
147-
# from which data has to invoked
148-
# @return [GoogleVisualr::DataTable] the table object will the data
149-
# filled
150-
def get_table(data)
151-
if data.is_a?(Daru::View::Table) &&
152-
data.table.is_a?(GoogleVisualr::DataTable)
153-
data.table
154-
elsif data.is_a?(GoogleVisualr::DataTable)
155-
data
156-
else
157-
add_data_in_table(data)
158-
end
159-
end
160-
161164
# @param data [String] URL of the google spreadsheet from which data
162165
# has to invoked
163166
# @return [Boolean, void] returns true for valid URL and raises error
@@ -216,6 +219,23 @@ def add_series(plot, opts={})
216219

217220
private
218221

222+
# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
223+
# The data provided by the user to generate the google datatable.
224+
# Data in String format represents the URL of the google spreadsheet
225+
# from which data has to invoked
226+
# @return [GoogleVisualr::DataTable] the table object with the data
227+
# filled
228+
def get_table(data)
229+
if data.is_a?(Daru::View::Table) &&
230+
data.table.is_a?(GoogleVisualr::DataTable)
231+
data.table
232+
elsif data.is_a?(GoogleVisualr::DataTable)
233+
data
234+
else
235+
add_data_in_table(data)
236+
end
237+
end
238+
219239
def extract_chart_type(options)
220240
# TODO: Imprvoe this method.
221241
chart_type = options[:type].nil? ? 'Line' : options.delete(:type)

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

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def extract_option_view
1818
'\'\''
1919
end
2020

21+
# @param element_id [String] The ID of the DIV element that the Google
22+
# ChartEditor should be rendered in
23+
# @return [String] Generates JavaScript for loading the charteditor package,
24+
# with callback to render ChartEditor
25+
def load_js_chart_editor(element_id)
26+
js = ''
27+
js << "\n google.load('visualization', '#{version}', "
28+
js << " {packages: ['charteditor'], callback:"
29+
js << " #{chart_function_name(element_id)}});"
30+
js
31+
end
32+
2133
# Generates JavaScript function for rendering the chart when data is URL of
2234
# the google spreadsheet
2335
#

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def google_table_version
7272
end
7373

7474
def package_name
75-
'table'
75+
return 'table' unless
76+
user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor'
77+
'charteditor'
7678
end
7779

7880
# @return [String] Returns value of the view option provided by the user
@@ -91,8 +93,8 @@ def extract_option_view
9193
def load_js(element_id)
9294
js = ''
9395
js << "\n google.load('visualization', #{google_table_version}, "
94-
js << "\n {packages: ['#{package_name}'], callback:"
95-
js << "\n #{chart_function_name(element_id)}});"
96+
js << " {packages: ['#{package_name}'], callback:"
97+
js << " #{chart_function_name(element_id)}});"
9698
js
9799
end
98100

0 commit comments

Comments
 (0)