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

Adds ability to generate Material charts when applicable #88

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
google_visualr (2.4.0)
google_visualr (2.4.1)

GEM
remote: https://rubygems.org/
Expand Down
8 changes: 7 additions & 1 deletion lib/google_visualr/app/helpers/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ module ViewHelper

def render_chart(chart, dom, options={})
script_tag = options.fetch(:script_tag) { true }
if script_tag

# render material based charts
is_material_chart = options.fetch(:material) { false }

if is_material_chart
chart.to_material_js(dom).html_safe
elsif script_tag
chart.to_js(dom).html_safe
else
html = ""
Expand Down
40 changes: 40 additions & 0 deletions lib/google_visualr/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ def draw_js(element_id)
js << "\n };"
js
end

# material js option
def material_package_name
chart = chart_name.gsub("Chart","")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Space missing after comma.


Copy link
Collaborator

Choose a reason for hiding this comment

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

Trailing whitespace detected.

# column chart uses the "Bar" namespace
# https://google-developers.appspot.com/chart/interactive/docs/gallery/columnchart#Material
if chart == "Column"
chart = "Bar"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary spacing detected.

end

return chart
Copy link
Collaborator

Choose a reason for hiding this comment

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

Redundant return detected.

end

def to_material_js(element_id)
js = ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unnecessary spacing detected.

js << "\n<script type='text/javascript'>"
js << load_material_js(element_id)
js << draw_material_js(element_id)
js << "\n</script>"
js
end

def load_material_js(element_id)
"\n google.load('visualization','1.1', {packages: ['#{material_package_name.downcase}'], callback: #{chart_function_name(element_id)}});"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [144/80]

end

def draw_material_js(element_id)
js = ""
js << "\n function #{chart_function_name(element_id)}() {"
js << "\n #{@data_table.to_js}"
js << "\n var chart = new google.charts.#{material_package_name}(document.getElementById('#{element_id}'));"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [117/80]

@listeners.each do |listener|
js << "\n google.visualization.events.addListener(chart, '#{listener[:event]}', #{listener[:callback]});"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [116/80]

end
js << "\n chart.draw(data_table, google.charts.#{material_package_name}.convertOptions(#{js_parameters(@options)}));"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [126/80]

js << "\n };"
js
end
# end material js option
end

end
2 changes: 1 addition & 1 deletion lib/google_visualr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GoogleVisualr
VERSION = "2.4.0"
VERSION = "2.4.1"
end