Skip to content

Commit

Permalink
Remove buttons and jspdf
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jun 24, 2018
1 parent 31258cf commit 86f8cf7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 332 deletions.
34 changes: 2 additions & 32 deletions lib/daru/view/adapters/googlecharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

module GoogleVisualr
def self.init_script(
dependent_js=['google_visualr.js', 'loader.js', 'jspdf.min.js',
'jquery.min.js', 'xepOnline.jqPlugin.js']
dependent_js=['google_visualr.js', 'loader.js']
)
js = ''
js << "\n<script type='text/javascript'>"
Expand Down Expand Up @@ -86,31 +85,14 @@ def to_html(id=nil, options={})
template = File.read(path)
id ||= SecureRandom.uuid
@html_id = id
chart_script = ''
add_listener('ready', "addNamespace_#{id.tr('-', '_')}") unless
defined?(IRuby)
chart_script << show_script(id, script_tag: false)
chart_script << add_namespace(id) unless defined?(IRuby)
chart_script = show_script(id, script_tag: false)
ERB.new(template).result(binding)
end

def show_in_iruby(dom=SecureRandom.uuid)
IRuby.html(to_html(dom))
end

# @param element_id [String] The ID of the DIV element that the Google
# Chart should be rendered in
# @return [String] js function to add namespace on the generated svg
def add_namespace(id=SecureRandom.uuid)
js = ''
js << "\n function addNamespace_#{id.tr('-', '_')}(){"
js << "\n var svg = jQuery('##{id} svg');"
js << "\n svg.attr('xmlns', 'http://www.w3.org/2000/svg');"
js << "\n svg.css('overflow','visible');"
js << "\n }"
js
end

# @see #Daru::View::Plot.export
def export(export_type='png', file_name='chart')
id = SecureRandom.uuid
Expand Down Expand Up @@ -143,8 +125,6 @@ def extract_export_code(export_type='png', file_name='chart')
case export_type
when 'png'
js << extract_export_png_code(file_name)
when 'pdf'
js << extract_export_pdf_code(file_name)
end
js << "\n }"
js << "\n </script>"
Expand All @@ -163,16 +143,6 @@ def extract_export_png_code(file_name)
js << "\n \tdocument.body.removeChild(a);"
js
end

# @param file_name [String] The name of the file after exporting the chart
# @return [String] the script to export the chart in pdf format
def extract_export_pdf_code(file_name)
js = ''
js << "\n \tvar doc = new jsPDF();"
js << "\n \tdoc.addImage(chart.getImageURI(), 0, 0);"
js << "\n \tdoc.save('#{file_name}.pdf');"
js
end
end

class DataTable
Expand Down
2 changes: 1 addition & 1 deletion lib/daru/view/adapters/googlecharts/iruby_notebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.generate_init_code(dependent_js)
end

# Enable to show plots on IRuby notebook
def self.init_iruby(dependent_js=['google_visualr.js', 'loader.js', 'jspdf.min.js'])
def self.init_iruby(dependent_js=['google_visualr.js', 'loader.js'])
js = generate_init_code(dependent_js)
IRuby.display(IRuby.javascript(js))
end
Expand Down
15 changes: 0 additions & 15 deletions lib/daru/view/templates/googlecharts/chart_div.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
<!-- See usage: http://www.cloudformatter.com/CSS2Pdf.APIDoc.Usage
TODO: Improve the UI of buttons. Also, there is some distortion in bar charts
and column charts, need to fix it. Check out the examples
here: http://www.cloudformatter.com/GoogleCharts -->
<% unless defined?(IRuby) %>
<button id="pdf_<%= id %>" onclick="xepOnline.Formatter.Format(
'<%= id %>', {render: 'download', filename: 'chart'}
);">PDF</button>
<button id="png_<%= id %>" onclick="xepOnline.Formatter.Format(
'<%= id %>', {render:'newwin', mimeType:'image/png'}
);">PNG</button>
<button id="jpg_<%= id %>" onclick="xepOnline.Formatter.Format(
'<%= id %>', {render:'newwin', mimeType:'image/jpg'}
);">JPEG</button>
<% end %>
<div id='<%= id %>'></div>
<script>
<%= chart_script %>
Expand Down
15 changes: 1 addition & 14 deletions lib/tasks/google_charts.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end

namespace :googlecharts do
desc "Update google charts javascript dependent files, from latest Builds on google developers website"
task :update => [:loader, :jspdf, :cloudformatter]
task :update => [:loader]
sh "mkdir -p lib/daru/view/adapters/js/googlecharts_js/"
# FIXME: Updating jsapi is causing error in IRuby notebook and Googlecharts do not work.
# task :jsapi do
Expand All @@ -23,17 +23,4 @@ namespace :googlecharts do
sh "curl -# http://www.gstatic.com/charts/loader.js -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/loader.js"
end
end

task :jspdf do
say "Grabbing jspdf.min.js from the cloudfare..." do
sh "curl -# https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/jspdf.min.js"
end
end

task :cloudformatter do
say "Grabbing jspdf.min.js from the cloudfare..." do
sh "curl -# http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/jquery.min.js"
sh "curl -# http://www.cloudformatter.com/Resources/Pages/CSS2Pdf/Script/xepOnline.jqPlugin.js -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/xepOnline.jqPlugin.js"
end
end
end
48 changes: 0 additions & 48 deletions spec/adapters/googlecharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
expect(js).to match(
/data_table.addRow\(\[\{v: \"2013\"\}\]\);/i)
expect(js).to match(/google.visualization.AreaChart/i)
expect(js).to match(
/google.visualization.events.addListener\(chart, 'ready', addNamespace_id/
)
expect(js).to match(/chart.draw\(data_table, \{\}/i)
end
it "generates valid JS of the Column Chart" do
Expand Down Expand Up @@ -83,38 +80,6 @@
expect(js).to match(
/data_table.addRow\(\[\{v: \"2013\"\}\]\);/i)
end
context "when IRuby is not defined" do
it "adds namespace for Googlecharts" do
js = column_chart_chart.chart.to_html("id")
expect(js).to match(/function addNamespace_id/)
expect(js).to match(/var svg = jQuery\('#id svg'\);/)
expect(js).to match(
/svg.attr\('xmlns', 'http:\/\/www.w3.org\/2000\/svg'\);/)
expect(js).to match(/svg.css\('overflow','visible'\);/)
end
it "adds namespace for Google datatables" do
js = data_table.table.to_html("id")
expect(js).to match(/function addNamespace_id/)
expect(js).to match(/var svg = jQuery\('#id svg'\);/)
expect(js).to match(
/svg.attr\('xmlns', 'http:\/\/www.w3.org\/2000\/svg'\);/)
expect(js).to match(/svg.css\('overflow','visible'\);/)
end
it "adds ready listener for GoogleCharts" do
area_chart_chart.chart.to_html('id')
expect(area_chart_chart.chart.listeners[0][:event]).to eq('ready')
expect(area_chart_chart.chart.listeners[0][:callback]).to eq(
'addNamespace_id'
)
end
it "adds ready listener for Google datatables" do
data_table.table.to_html('id')
expect(data_table.table.listeners[0][:event]).to eq('ready')
expect(data_table.table.listeners[0][:callback]).to eq(
'addNamespace_id'
)
end
end
end

describe "#show_script" do
Expand Down Expand Up @@ -253,12 +218,6 @@
expect(js).to match(/a.href = chart.getImageURI()/)
expect(js).to match(/a.download = 'chart.png'/)
end
it "generates correct pdf code" do
js = area_chart_chart.chart.export('pdf')
expect(js).to match(/var doc = new jsPDF()/)
expect(js).to match(/doc.addImage\(chart.getImageURI\(\), 0, 0\)/)
expect(js).to match(/doc.save\('chart.pdf'\)/)
end
end

describe "#extract_export_code" do
Expand All @@ -278,12 +237,5 @@
expect(js).to match(/a.click\(\)/)
expect(js).to match(/document.body.removeChild\(a\)/)
end
it "extracts correct pdf code" do
area_chart_chart.chart.html_id = 'id'
js = area_chart_chart.chart.extract_export_code('pdf', 'daru')
expect(js).to match(/var doc = new jsPDF()/)
expect(js).to match(/doc.addImage\(chart.getImageURI\(\), 0, 0\)/)
expect(js).to match(/doc.save\('daru.pdf'\)/)
end
end
end
8 changes: 0 additions & 8 deletions spec/adapters/googlecharts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,6 @@
expect(js).to match(/a.href = chart.getImageURI()/)
expect(js).to match(/a.download = 'chart.png'/)
end
it "generates valid script to export a chart to pdf" do
js = area_chart_chart.adapter.export(area_chart_chart.chart, 'pdf')
expect(js).to match(/google.visualization.DataTable\(\);/)
expect(js).to match(/google.visualization.AreaChart/)
expect(js).to match(/var doc = new jsPDF()/)
expect(js).to match(/doc.addImage\(chart.getImageURI\(\), 0, 0\)/)
expect(js).to match(/doc.save\('chart.pdf'\)/)
end
end

describe "#export_html_file" do
Expand Down
233 changes: 27 additions & 206 deletions spec/dummy_iruby/Google Chart | Area Chart (export examples also).ipynb

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions spec/plot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,5 @@
expect(js).to match(/a.href = chart.getImageURI()/)
expect(js).to match(/a.download = 'daru.png'/)
end
it "generates valid script to export a chart to pdf" do
js = plot_dv.export('pdf', 'daru')
expect(js).to match(/google.visualization.DataTable\(\);/)
expect(js).to match(/google.visualization.LineChart/)
expect(js).to match(/var doc = new jsPDF()/)
expect(js).to match(/doc.addImage\(chart.getImageURI\(\), 0, 0\)/)
expect(js).to match(/doc.save\('daru.pdf'\)/)
end
end
end

0 comments on commit 86f8cf7

Please sign in to comment.