Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Charteditor #96

Merged
merged 18 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'google_visualr'
require_relative 'googlecharts/iruby_notebook'
require_relative 'googlecharts/display'
require_relative 'googlecharts/google_visualr'
require 'daru'
require 'bigdecimal'
require 'daru/view/constants'
Expand Down Expand Up @@ -58,6 +58,16 @@ module GooglechartsAdapter
# options = {type: :area}
# chart = Daru::View::Plot.new(data, options)
#
# @example ChartEditor
# data = [
# ['Year', 'Sales', 'Expenses'],
# ['2013', 1000, 400],
# ['2014', 1170, 460],
# ['2015', 660, 1120],
# ['2016', 1030, 540]
# ]
# plot = Daru::View::Plot.new(data, {}, chart_class: 'Charteditor')
#
# @example Multiple Charts in a row
# Draw the Daru::View::PlotList object with the data as an array of
# Daru::View::Plots(s) or Daru::View::Table(s) or both
Expand Down Expand Up @@ -115,6 +125,16 @@ def init(data=[], options={}, user_options={})
# query = 'SELECT A, H, O, Q, R, U LIMIT 5 OFFSET 8'
# data << query
# chart = Daru::View::Table.new(data)
#
# @example ChartEditor
# data = [
# ['Year', 'Sales', 'Expenses'],
# ['2013', 1000, 400],
# ['2014', 1170, 460],
# ['2015', 660, 1120],
# ['2016', 1030, 540]
# ]
# table = Daru::View::Table.new(data, {}, chart_class: 'Charteditor')
def init_table(data=[], options={}, user_options={})
# if `options` is something like this :
# {
Expand All @@ -141,23 +161,6 @@ def init_table(data=[], options={}, user_options={})
@table
end

# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
# The data provided by the user to generate the google datatable.
# Data in String format represents the URL of the google spreadsheet
# from which data has to invoked
# @return [GoogleVisualr::DataTable] the table object will the data
# filled
def get_table(data)
if data.is_a?(Daru::View::Table) &&
data.table.is_a?(GoogleVisualr::DataTable)
data.table
elsif data.is_a?(GoogleVisualr::DataTable)
data
else
add_data_in_table(data)
end
end

# @param data [String] URL of the google spreadsheet from which data
# has to invoked
# @return [Boolean, void] returns true for valid URL and raises error
Expand Down Expand Up @@ -216,6 +219,23 @@ def add_series(plot, opts={})

private

# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table]
# The data provided by the user to generate the google datatable.
# Data in String format represents the URL of the google spreadsheet
# from which data has to invoked
# @return [GoogleVisualr::DataTable] the table object with the data
# filled
def get_table(data)
if data.is_a?(Daru::View::Table) &&
data.table.is_a?(GoogleVisualr::DataTable)
data.table
elsif data.is_a?(GoogleVisualr::DataTable)
data
else
add_data_in_table(data)
end
end

def extract_chart_type(options)
# TODO: Imprvoe this method.
chart_type = options[:type].nil? ? 'Line' : options.delete(:type)
Expand Down
12 changes: 12 additions & 0 deletions lib/daru/view/adapters/googlecharts/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def extract_option_view
'\'\''
end

# @param element_id [String] The ID of the DIV element that the Google
# ChartEditor should be rendered in
# @return [String] Generates JavaScript for loading the charteditor package,
# with callback to render ChartEditor
def load_js_chart_editor(element_id)
js = ''
js << "\n google.load('visualization', '#{version}', "
js << " {packages: ['charteditor'], callback:"
js << " #{chart_function_name(element_id)}});"
js
end

# Generates JavaScript function for rendering the chart when data is URL of
# the google spreadsheet
#
Expand Down
8 changes: 5 additions & 3 deletions lib/daru/view/adapters/googlecharts/data_table_iruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def google_table_version
end

def package_name
'table'
return 'table' unless
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChartEditor is going through data_table_iruby.rb :O . But why ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChartEditor works for both google charts and tables, and code to generate the table is written in data_table_iruby.rb. charteditor package is loaded in Google Datatables when chart_class is set to Charteditor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any example or documentation written, which describes this usecase ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have written the rails examples in demo_daru-view and also in the method documentation here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write examples in web apps and iruby as well, if haven't.

user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor'
'charteditor'
end

# @return [String] Returns value of the view option provided by the user
Expand All @@ -91,8 +93,8 @@ def extract_option_view
def load_js(element_id)
js = ''
js << "\n google.load('visualization', #{google_table_version}, "
js << "\n {packages: ['#{package_name}'], callback:"
js << "\n #{chart_function_name(element_id)}});"
js << " {packages: ['#{package_name}'], callback:"
js << " #{chart_function_name(element_id)}});"
js
end

Expand Down
Loading