Skip to content

Commit

Permalink
Move draw_js_chart_wrapper to display
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jul 26, 2018
1 parent c3672e0 commit 14ecc63
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 300 deletions.
26 changes: 7 additions & 19 deletions lib/daru/view/adapters/googlecharts/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,6 @@ def extract_option_view
'\'\''
end

# Generates JavaScript function for rendering the chartwrapper
#
# @param (see #to_js_chart_wrapper)
# @return [String] JS function to render the chartwrapper
def draw_js_chart_wrapper(data, element_id)
js = ''
js << "\n function #{chart_function_name(element_id)}() {"
js << "\n \t#{@data_table.to_js}"
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
js << extract_chart_wrapper_options(data, element_id)
js << "\n \t});"
js << draw_wrapper
js << add_listeners_js('wrapper')
js << "\n };"
js
end

# Generates JavaScript function for rendering the chart when data is URL of
# the google spreadsheet
#
Expand All @@ -57,12 +40,17 @@ def draw_js_spreadsheet(data, element_id=SecureRandom.uuid)
js
end

# Taken from `draw_js` in googlevisualr. While adding the listener,
# the callback code (provided by the user) should be within the function.
#
# @param element_id [String] The ID of the DIV element that the Google
# Chart should be rendered in
# @return [String] JavaScript function for rendering the chart
def draw_chart_js(element_id)
js = ''
js << "\n var chart = null;"
js << "\n function #{chart_function_name(element_id)}() {"
js << "\n #{@data_table.to_js}"
js << "\n chart = new google.#{chart_class}.#{chart_name}"
js << "\n var chart = new google.#{chart_class}.#{chart_name}"
js << "(document.getElementById('#{element_id}'));"
js << add_listeners_js('chart')
js << "\n chart.draw(data_table, #{js_parameters(@options)});"
Expand Down
17 changes: 0 additions & 17 deletions lib/daru/view/adapters/googlecharts/data_table_iruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,6 @@ def draw_js(element_id)
js
end

# Generates JavaScript function for rendering the chartwrapper
#
# @param (see #to_js_chart_wrapper)
# @return [String] JS function to render the chartwrapper
def draw_js_chart_wrapper(data, element_id)
js = ''
js << "\n function #{chart_function_name(element_id)}() {"
js << "\n \t#{to_js}"
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
js << extract_chart_wrapper_options(data, element_id)
js << "\n \t});"
js << draw_wrapper
js << add_listeners_js('wrapper')
js << "\n };"
js
end

# Generates JavaScript function for rendering the google chart table when
# data is URL of the google spreadsheet
#
Expand Down
35 changes: 28 additions & 7 deletions lib/daru/view/adapters/googlecharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def show_in_iruby(dom=SecureRandom.uuid)
IRuby.html to_html(dom)
end

# @return [void] Adds listener to the chart from the
# user_options[:listeners]
def add_listener_to_chart
return unless user_options && user_options[:listeners]
user_options[:listeners].each do |event, callback|
add_listener(event.to_s.downcase, callback)
end
end

# @return [String] js function to add the listener to the chart
def add_listeners_js(type)
js = ''
Expand Down Expand Up @@ -197,13 +206,25 @@ def to_js_spreadsheet(data, element_id=SecureRandom.uuid)
js
end

# @return [void] Adds listener to the chart from the
# user_options[:listeners]
def add_listener_to_chart
return unless user_options && user_options[:listeners]
user_options[:listeners].each do |event, callback|
add_listener(event.to_s.downcase, callback)
end
# Generates JavaScript function for rendering the chartwrapper
#
# @param (see #to_js_chart_wrapper)
# @return [String] JS function to render the chartwrapper
def draw_js_chart_wrapper(data, element_id)
js = ''
js << "\n function #{chart_function_name(element_id)}() {"
js << if is_a?(GoogleVisualr::DataTable)
"\n \t#{to_js}"
else
"\n \t#{@data_table.to_js}"
end
js << "\n \tvar wrapper = new google.visualization.ChartWrapper({"
js << extract_chart_wrapper_options(data, element_id)
js << "\n \t});"
js << draw_wrapper
js << add_listeners_js('wrapper')
js << "\n };"
js
end
end

Expand Down
13 changes: 0 additions & 13 deletions spec/adapters/googlecharts/base_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@
end
end

describe "#draw_js_chart_wrapper" do
it "draws valid JS of the ChartWrapper" do
js = area_chart.chart.draw_js_chart_wrapper(data, 'id')
expect(js).to match(/new google.visualization.DataTable/)
expect(js).to match(/new google.visualization.ChartWrapper/)
expect(js).to match(/chartType: 'AreaChart'/)
expect(js).to match(/dataTable: data_table/)
expect(js).to match(/options: {width: 800/)
expect(js).to match(/containerId: 'id'/)
expect(js).to match(/view: {columns: \[0,1\]}/)
end
end

describe "#draw_js_spreadsheet" do
it "draws valid JS of the chart when "\
"data is imported from google spreadsheets" do
Expand Down
12 changes: 0 additions & 12 deletions spec/adapters/googlecharts/data_table_iruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,4 @@
expect(js).to match(/table.draw\(data_table, \{width: 800\}/i)
end
end

describe "#draw_js_chart_wrapper" do
it "draws valid JS of the ChartWrapper" do
js = table_chartwrapper.table.draw_js_chart_wrapper(data, 'id')
expect(js).to match(/new google.visualization.DataTable/)
expect(js).to match(/new google.visualization.ChartWrapper/)
expect(js).to match(/chartType: 'Table'/)
expect(js).to match(/dataTable: data_table/)
expect(js).to match(/options: \{\}/)
expect(js).to match(/containerId: 'id'/)
end
end
end
22 changes: 22 additions & 0 deletions spec/adapters/googlecharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,26 @@
end
end
end

describe "#draw_js_chart_wrapper" do
it "draws valid JS of the ChartWrapper table" do
js = table_chart_wrapper.table.draw_js_chart_wrapper(data, 'id')
expect(js).to match(/new google.visualization.DataTable/)
expect(js).to match(/new google.visualization.ChartWrapper/)
expect(js).to match(/chartType: 'Table'/)
expect(js).to match(/dataTable: data_table/)
expect(js).to match(/options: \{\}/)
expect(js).to match(/containerId: 'id'/)
end
it "draws valid JS of the ChartWrapper" do
js = area_chart_wrapper.chart.draw_js_chart_wrapper(data, 'id')
expect(js).to match(/new google.visualization.DataTable/)
expect(js).to match(/new google.visualization.ChartWrapper/)
expect(js).to match(/chartType: 'AreaChart'/)
expect(js).to match(/dataTable: data_table/)
expect(js).to match(/options: {}/)
expect(js).to match(/containerId: 'id'/)
expect(js).to match(/view: ''/)
end
end
end
Loading

0 comments on commit 14ecc63

Please sign in to comment.