-
Notifications
You must be signed in to change notification settings - Fork 20
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 Chartwrapper #95
Changes from 4 commits
3821b7d
0ddb72a
70adda7
2f88184
322a08c
65727ab
00dc358
d3e0bba
bbda094
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
require 'securerandom' | ||
require 'erb' | ||
require_relative 'table_chart_common_methods' | ||
require_relative 'data_table_iruby' | ||
require_relative 'base_chart' | ||
require 'daru/view/constants' | ||
|
||
module GoogleVisualr | ||
def self.init_script( | ||
dependent_js=['google_visualr.js', 'loader.js'] | ||
dependent_js=GOOGLECHARTS_DEPENDENCIES | ||
) | ||
js = '' | ||
js << "\n<script type='text/javascript'>" | ||
|
@@ -24,6 +26,8 @@ def show_script(dom=SecureRandom.uuid, options={}) | |
script_tag = options.fetch(:script_tag) { true } | ||
if script_tag | ||
show_script_with_script_tag(dom) | ||
elsif user_options[:chart_class].to_s.capitalize == 'Chartwrapper' | ||
get_html_chart_wrapper(data, dom) | ||
elsif data.is_a?(String) | ||
get_html_spreadsheet(data, dom) | ||
else | ||
|
@@ -35,14 +39,13 @@ def show_script(dom=SecureRandom.uuid, options={}) | |
# Chart should be rendered in | ||
# @return [String] js code to render the chart with script tag | ||
def show_script_with_script_tag(dom=SecureRandom.uuid) | ||
# if it is data table and importing data from spreadsheet | ||
if is_a?(GoogleVisualr::DataTable) && data.is_a?(String) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How your changes is handling this if condition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have kept the same method name for both table and chart, and now have put that method in the common module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Prakriti-nith , I don't think your replay is relevant . Now are you using the method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
to_js_full_script_spreadsheet(data, dom) | ||
elsif is_a?(GoogleVisualr::DataTable) | ||
to_js_full_script(dom) | ||
# Importing data from spreadsheet | ||
# if it is data table | ||
if user_options[:chart_class].to_s.capitalize == 'Chartwrapper' | ||
to_js_chart_wrapper(data, dom) | ||
elsif data.is_a?(String) | ||
to_js_spreadsheet(data, dom) | ||
elsif is_a?(GoogleVisualr::DataTable) | ||
to_js_full_script(dom) | ||
else | ||
to_js(dom) | ||
end | ||
|
@@ -74,6 +77,13 @@ def get_html_spreadsheet(data, dom) | |
html | ||
end | ||
|
||
def get_html_chart_wrapper(data, dom) | ||
html = '' | ||
html << load_js(dom) | ||
html << draw_js_chart_wrapper(data, dom) | ||
html | ||
end | ||
|
||
def to_html(id=nil, options={}) | ||
path = File.expand_path( | ||
'../../templates/googlecharts/chart_div.erb', __dir__ | ||
|
@@ -86,7 +96,7 @@ def to_html(id=nil, options={}) | |
end | ||
|
||
def show_in_iruby(dom=SecureRandom.uuid) | ||
IRuby.html(to_html(dom)) | ||
IRuby.html to_html(dom) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'google_visualr' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already one module is added for display purpose , please check display module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have moved the common methods to display module. Sorry, I got confused. Actually, display module contained methods that called the methods of base_chart.rb and data_table.rb (like to_js and others) and there was no method that was called back from these files to display. |
||
|
||
module GoogleVisualr | ||
module HelperMethods | ||
# @param element_id [String] The ID of the DIV element that the Google | ||
# Chart DataTable should be rendered in | ||
# @return [String] unique function name to handle query response | ||
def query_response_function_name(element_id) | ||
"handleQueryResponse_#{element_id.tr('-', '_')}" | ||
end | ||
|
||
# @param data [Array, Daru::DataFrame, Daru::Vector, String] Data | ||
# of GoogleVisualr DataTable | ||
# @return [String] Data option (dataSourceUrl or dataTable) required to | ||
# draw the Chartwrapper based upon the data provided. | ||
def append_data(data) | ||
return "\n \t\tdataSourceUrl: '#{data}'," if data.is_a? String | ||
"\n \t\tdataTable: data_table," | ||
end | ||
|
||
# So that it can be used in ChartEditor also | ||
# | ||
# @return [String] Returns string to draw the Chartwrapper and '' otherwise | ||
def draw_wrapper | ||
return "\n \twrapper.draw();" if | ||
user_options[:chart_class].to_s.capitalize == 'Chartwrapper' | ||
'' | ||
end | ||
|
||
# Generates JavaScript and renders the Google Chartwrapper in the | ||
# final HTML output. | ||
# | ||
# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String] | ||
# Data of GoogleVisualr Chart | ||
# @param element_id [String] The ID of the DIV element that the Google | ||
# Chartwrapper should be rendered in | ||
# @return [String] Javascript code to render the Google Chartwrapper | ||
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 | ||
|
||
# Generates JavaScript when data is imported from spreadsheet and renders | ||
# the Google Chart in the final HTML output when data is URL of the | ||
# google spreadsheet | ||
# | ||
# @param data [String] URL of the google spreadsheet in the specified | ||
# format: https://developers.google.com/chart/interactive/docs | ||
# /spreadsheets | ||
# Query string can be appended to retrieve the data accordingly | ||
# @param element_id [String] The ID of the DIV element that the Google | ||
# Chart should be rendered in | ||
# @return [String] Javascript code to render the Google Chart when data is | ||
# given as the URL of the google spreadsheet | ||
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 | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
_user_options
,_
is not required, right ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless user_options is used in the method (in which it has been passed as a parameter), we need to prefix it with
_
as rubocop was throwingLint/UnusedMethodArgument
error.