Skip to content

Commit

Permalink
Remove export_image_iruby from plot.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jun 3, 2018
1 parent 1c7fe7d commit 67efe88
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 50 deletions.
6 changes: 1 addition & 5 deletions lib/daru/view/adapters/googlecharts.rb
Expand Up @@ -65,11 +65,7 @@ def export_html_file(plot, path='./plot.html')
File.write(path, str)
end

def export_image(plot, file_name, type='pdf')
# TODO
end

def export_image_iruby(plot, file_name, type='pdf')
def export_type(plot, type='pdf', file_name='chart')
# TODO
end

Expand Down
13 changes: 5 additions & 8 deletions lib/daru/view/adapters/highcharts.rb
Expand Up @@ -64,14 +64,11 @@ def export_html_file(plot, path='./plot.html')
File.write(path, str)
end

# @see #Daru::View::Plot.export_image
def export_image(plot, file_name='chart', type='pdf')
plot.export_image(file_name, type)
end

# @see #Daru::View::Plot.export_image_iruby
def export_image_iruby(plot, file_name='chart', type='pdf')
plot.export_image_iruby(file_name, type)
# @see #Daru::View::Plot.export_type
def export_type(plot, type='pdf', file_name='chart')
plot.export_type_iruby(type, file_name) if defined? IRuby
rescue NameError
plot.export_type(type, file_name)
end

def show_in_iruby(plot)
Expand Down
23 changes: 14 additions & 9 deletions lib/daru/view/adapters/highcharts/display.rb
Expand Up @@ -67,31 +67,36 @@ def to_html_iruby(placeholder=random_canvas_id)
high_chart_iruby(extract_chart_class, placeholder, self)
end

# @see #Daru::View::Plot.export_image
def export_image(file_name='chart', type='pdf')
# @see #Daru::View::Plot.export_type
def export_type(type='pdf', file_name='chart')
js = ''
js << to_html
js << extract_export_code(@div_id, file_name, type)
js << extract_export_code(@div_id, type, file_name)
js
end

# @see #Daru::View::Plot.export_image_iruby
def export_image_iruby(file_name='chart', type='pdf')
# Exports chart to different formats in IRuby notebook
#
# @param type [String] format to which chart has to be exported
# @param file_name [String] The name of the file after exporting the chart
# @return [void] loads the js code of chart along with the code to export
# in IRuby notebook
def export_type_iruby(type='pdf', file_name='chart')
js = ''
js << to_html_iruby
js << extract_export_code_iruby(@div_id, file_name, type)
js << extract_export_code_iruby(@div_id, type, file_name)
IRuby.html js
end

# Returns the script to export the chart in different formats for
# web frameworks
#
# @param file_name [String] The name of the file after exporting the chart
# @param placeholder [String] The ID of the DIV element that
# the HighChart should be rendered in
# @param file_name [String] The name of the file after exporting the chart
# @param type [String] format to which chart has to be exported
# @return [String] the script to export the chart in web frameworks
def extract_export_code(placeholder=random_canvas_id, file_name='chart', type='pdf')
def extract_export_code(placeholder=random_canvas_id, type='pdf', file_name='chart')
js = ''
js << "\n <script>"
js << "\n (function() {"
Expand All @@ -114,7 +119,7 @@ def extract_export_code(placeholder=random_canvas_id, file_name='chart', type='p
#
# @param (see #extract_export_code)
# @return [String] the script to export the chart in IRuby notebook
def extract_export_code_iruby(placeholder=random_canvas_id, file_name='chart', type='pdf')
def extract_export_code_iruby(placeholder=random_canvas_id, type='pdf', file_name='chart')
js = ''
js << "\n <script>"
js << "\n (function() {"
Expand Down
6 changes: 1 addition & 5 deletions lib/daru/view/adapters/nyaplot.rb
Expand Up @@ -17,11 +17,7 @@ def export_html_file(plot, path)
plot.export_html path
end

def export_image(plot, file_name, type='pdf')
# TODO
end

def export_image_iruby(plot, file_name, type='pdf')
def export_type(plot, type='pdf', file_name='chart')
# TODO
end

Expand Down
18 changes: 4 additions & 14 deletions lib/daru/view/plot.rb
Expand Up @@ -78,23 +78,13 @@ def export_html_file(path='./plot.html')
@adapter.export_html_file(@chart, path)
end

# Exports chart to different formats in web frameworks
# Exports chart to different formats
#
# @param file_name [String] The name of the file after exporting the chart
# @param type [String] format to which chart has to be exported
# @return [String] js code of chart along with the code to export it
def export_image(file_name='chart', type='pdf')
@adapter.export_image(@chart, file_name, type)
end

# Exports chart to different formats in IRuby notebook
#
# @param file_name [String] The name of the file after exporting the chart
# @param type [String] format to which chart has to be exported
# @return [void] loads the js code of chart along with the code to export
# in IRuby notebook
def export_image_iruby(file_name='chart', type='pdf')
@adapter.export_image_iruby(@chart, file_name, type)
# @return [String] js code of chart along with the code to export it
def export_type(type='pdf', file_name='chart')
@adapter.export_type(@chart, type, file_name)
end

# load the corresponding JS files in IRuby notebook.
Expand Down
9 changes: 5 additions & 4 deletions spec/adapters/highcharts/display_spec.rb
Expand Up @@ -197,17 +197,18 @@
end
end

describe "#export_image" do
describe "#export_type" do
it "should generate the valid script to export the chart in different"\
" formats" do
js = @hc.chart.export_image
js = @hc.chart.export_type('jpg','daru')
expect(js).to match(/\s+new\s+Highcharts.Chart/)
expect(js).to match(/var\s+options\s+=/)
expect(js).to match(/window.chart_/)
expect(js).to match(/\"chart\": \{ \"type\": \"bar\"/)
expect(js).to match(/\"data\": \[ \[ 5,2,3 \],\[ 3,2,4 \],\[ 4,3,4 \]/)
expect(js).to match(/script/)
expect(js).to match(/application\/pdf/)
expect(js).to match(/image\/jpeg/)
expect(js).to match(/daru/)
expect(js).to match(/chart.exportChart/)
end
end
Expand All @@ -234,7 +235,7 @@
describe "#extract_export_code_iruby" do
it "should generate the valid script to export the chart in different"\
" formats in IRuby notebook" do
js = @hc.chart.extract_export_code_iruby(@placeholder,'daru','png')
js = @hc.chart.extract_export_code_iruby(@placeholder, 'png', 'daru')
expect(js).to match(/script/)
expect(js).to match(
/var chartDom = document.getElementById\('placeholder'\)/
Expand Down
6 changes: 3 additions & 3 deletions spec/adapters/highcharts_spec.rb
Expand Up @@ -423,15 +423,15 @@
end
end

describe "#export_image" do
describe "#export_type" do
it "should generate the valid script to export the chart" do
js = @chart_bar.adapter.export_image(@chart_bar.chart)
js = @chart_bar.adapter.export_type(@chart_bar.chart, 'png')
expect(js).to match(/\s+new\s+Highcharts.Chart/)
expect(js).to match(/var\s+options\s+=/)
expect(js).to match(/window.chart_/)
expect(js).to match(/\"chart\": \{ \"type\": \"bar\"/)
expect(js).to match(/script/)
expect(js).to match(/application\/pdf/)
expect(js).to match(/image\/png/)
expect(js).to match(/chart.exportChart/)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/plot_spec.rb
Expand Up @@ -83,9 +83,9 @@
end
end # initialize context end

context '#export_image' do
context '#export_type' do
it "should generate the valid script to export the chart" do
js = plot_df.export_image
js = plot_df.export_type
expect(js).to match(/\s+new\s+Highcharts.Chart/)
expect(js).to match(/var\s+options\s+=/)
expect(js).to match(/window.chart_/)
Expand Down

0 comments on commit 67efe88

Please sign in to comment.