-
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 Charteditor #96
Changes from 11 commits
abacb2d
3dd8fff
7680fd2
6a70844
d531fe2
a30450f
5defa69
3d1e97a
be2939d
81ac864
85cc0b9
f99a319
68d3125
eccd285
cf66066
3829d87
6f94d7b
5de4ed7
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 |
---|---|---|
|
@@ -62,7 +62,9 @@ def google_table_version | |
end | ||
|
||
def package_name | ||
'table' | ||
return 'table' unless | ||
user_options && user_options[:chart_class].to_s.capitalize == 'Charteditor' | ||
'charteditor' | ||
end | ||
|
||
# @return [String] Returns value of the view option provided by the user | ||
|
@@ -81,8 +83,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 | ||
|
||
|
@@ -108,16 +110,14 @@ def draw_js(element_id) | |
# @return [String] JS function to render the chartwrapper | ||
def draw_js_chart_wrapper(data, element_id) | ||
js = '' | ||
js << "\n var wrapper_#{element_id.tr('-', '_')} = null;" | ||
js << "\n function #{chart_function_name(element_id)}() {" | ||
js << "\n \t#{to_js}" | ||
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({" | ||
js << "\n \t\tchartType: 'Table'," | ||
js << append_data(data) | ||
js << "\n \t\toptions: #{js_parameters(@options)}," | ||
js << "\n \t\tcontainerId: '#{element_id}'," | ||
js << "\n \t\tview: #{extract_option_view}" | ||
js << "\n \twrapper_#{element_id.tr('-', '_')} = "\ | ||
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. Same code 2 times ?? |
||
'new google.visualization.ChartWrapper({' | ||
js << extract_chart_wrapper_options(data, element_id) | ||
js << "\n \t});" | ||
js << draw_wrapper | ||
js << draw_wrapper(element_id) | ||
js << "\n };" | ||
js | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,13 @@ def show_script(dom=SecureRandom.uuid, options={}) | |
if script_tag | ||
show_script_with_script_tag(dom) | ||
# Without checking for user_options, data as hash was not working! | ||
elsif user_options && | ||
user_options[:chart_class].to_s.capitalize == 'Chartwrapper' | ||
get_html_chart_wrapper(data, dom) | ||
elsif user_options && user_options[:chart_class] | ||
case user_options[:chart_class].to_s.capitalize | ||
when 'Chartwrapper' | ||
get_html_chart_wrapper(data, dom) | ||
when 'Charteditor' | ||
get_html_chart_editor(data, dom) | ||
end | ||
elsif data.is_a?(String) | ||
get_html_spreadsheet(data, dom) | ||
else | ||
|
@@ -63,6 +67,34 @@ def get_html(dom) | |
html | ||
end | ||
|
||
# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String] | ||
# Data of GoogleVisualr Chart or GoogleVisualr DataTable | ||
# @param dom [String] The ID of the DIV element that the Google | ||
# Chart should be rendered in | ||
# @return [String] js code to render the chart | ||
def get_html_chart_wrapper(data, dom) | ||
html = '' | ||
html << load_js(dom) | ||
html << draw_js_chart_wrapper(data, dom) | ||
html | ||
end | ||
|
||
# @param data [Array, Daru::DataFrame, Daru::Vector, Daru::View::Table, String] | ||
# Data of GoogleVisualr Chart or GoogleVisualr DataTable | ||
# @param dom [String] The ID of the DIV element that the Google | ||
# Chart should be rendered in | ||
# @return [String] js code to render the charteditor | ||
def get_html_chart_editor(data, dom) | ||
html = '' | ||
html << if is_a?(GoogleVisualr::DataTable) | ||
load_js(dom) | ||
else | ||
load_js_chart_editor(dom) | ||
end | ||
html << draw_js_chart_editor(data, dom) | ||
html | ||
end | ||
|
||
# @param data [String] URL of the google spreadsheet in the specified | ||
# format: https://developers.google.com/chart/interactive/docs | ||
# /spreadsheets | ||
|
@@ -79,13 +111,6 @@ 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__ | ||
|
@@ -117,13 +142,43 @@ def append_data(data) | |
"\n \t\tdataTable: data_table," | ||
end | ||
|
||
# @param element_id [String] The ID of the DIV element that the Google | ||
# Chart should be rendered in | ||
# @return [String] unique function name to save the chart | ||
def save_chart_function_name(element_id) | ||
"saveChart_#{element_id.tr('-', '_')}" | ||
end | ||
|
||
# @param (see #draw_js_chart_editor) | ||
# @return [String] options of the ChartWrapper | ||
def extract_chart_wrapper_options(data, element_id) | ||
js = '' | ||
js << if is_a?(GoogleVisualr::DataTable) | ||
"\n \t\tchartType: 'Table'," | ||
else | ||
"\n \t\tchartType: '#{chart_name}'," | ||
end | ||
js << append_data(data) | ||
js << "\n \t\toptions: #{js_parameters(@options)}," | ||
js << "\n \t\tcontainerId: '#{element_id}'," | ||
js << "\n \t\tview: #{extract_option_view}" | ||
js | ||
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 | ||
def draw_wrapper(element_id) | ||
return "\n \twrapper_#{element_id.tr('-', '_')}.draw();" if | ||
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. Just want to know, what if we don't provide 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. If we use only the |
||
user_options[:chart_class].to_s.capitalize == 'Chartwrapper' | ||
'' | ||
js = '' | ||
js << "\n \twrapper_#{element_id.tr('-', '_')}.draw();" | ||
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. same line at 172 . |
||
js << "\n \tchartEditor_#{element_id.tr('-', '_')} = "\ | ||
'new google.visualization.ChartEditor();' | ||
js << "\n \tgoogle.visualization.events.addListener("\ | ||
"chartEditor_#{element_id.tr('-', '_')},"\ | ||
" 'ok', #{save_chart_function_name(element_id)});" | ||
js | ||
end | ||
|
||
# Generates JavaScript and renders the Google Chartwrapper in the | ||
|
@@ -163,6 +218,26 @@ def to_js_spreadsheet(data, element_id=SecureRandom.uuid) | |
js << "\n</script>" | ||
js | ||
end | ||
|
||
# @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 | ||
# ChartEditor should be rendered in | ||
# @return [String] JS function to render the ChartEditor | ||
def draw_js_chart_editor(data, element_id) | ||
js = '' | ||
js << "\n var chartEditor_#{element_id.tr('-', '_')} = null;" | ||
js << draw_js_chart_wrapper(data, element_id) | ||
js << "\n function #{save_chart_function_name(element_id)}(){" | ||
js << "\n \tchartEditor_#{element_id.tr('-', '_')}.getChartWrapper()."\ | ||
"draw(document.getElementById('#{element_id}'));" | ||
js << "\n }" | ||
js << "\n function loadEditor_#{element_id.tr('-', '_')}(){" | ||
js << "\n \tchartEditor_#{element_id.tr('-', '_')}.openDialog("\ | ||
"wrapper_#{element_id.tr('-', '_')}, {});" | ||
js << "\n }" | ||
js | ||
end | ||
end | ||
|
||
class DataTable | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,12 @@ | |
chart_class: 'ChartWrapper' | ||
) | ||
} | ||
let (:plot_spreadsheet_charteditor) { | ||
Daru::View::Plot.new( | ||
data_spreadsheet, {width: 800, view: {columns: [0, 1]}}, chart_class: 'Charteditor' | ||
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. line length |
||
) | ||
} | ||
let(:plot_charteditor) {Daru::View::Plot.new(data, {}, chart_class: 'Charteditor')} | ||
|
||
describe "#extract_option_view" do | ||
it "should return value of view option if view option is provided" do | ||
|
@@ -47,19 +53,12 @@ | |
end | ||
end | ||
|
||
describe "#to_js_chart_wrapper" do | ||
it "draws valid JS of the ChartWrapper when data is URL of the spreadsheet" do | ||
js = area_chart_spreadsheet.chart.to_js_chart_wrapper( | ||
data_spreadsheet, | ||
'id' | ||
) | ||
expect(js).to match(/google.load\('visualization'/) | ||
expect(js).to match(/callback: draw_id/) | ||
expect(js).to match(/new google.visualization.ChartWrapper/) | ||
expect(js).to match(/chartType: 'AreaChart'/) | ||
expect(js).to match(/dataSourceUrl: 'https:\/\/docs.google/) | ||
expect(js).to match(/options: {}/) | ||
expect(js).to match(/containerId: 'id'/) | ||
describe "#load_js_chart_editor" do | ||
it "loads valid packages" do | ||
js = plot_charteditor.chart.load_js_chart_editor('id') | ||
expect(js).to match(/google.load\('visualization', '1.0',/i) | ||
expect(js).to match(/\{packages: \['charteditor'\], callback:/i) | ||
expect(js).to match(/draw_id\}\)/i) | ||
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.
ChartEditor is going through
data_table_iruby.rb
:O . But why ?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.
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 whenchart_class
is set to Charteditor.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.
Is there any example or documentation written, which describes this usecase ?
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.
I have written the rails examples in demo_daru-view and also in the method documentation here.
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.
Write examples in web apps and iruby as well, if haven't.