Skip to content

Commit

Permalink
Add plot_list_specs.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jun 27, 2018
1 parent 759b218 commit 132cbf6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ module GooglechartsAdapter
# chart = Daru::View::Plot.new(data, options)
#
# @example Multiple Charts in a row
# Draw the Daru::View::Plot object with the data as an array of
# Draw the Daru::View::PlotList object with the data as an array of
# Daru::View::Plots(s) or Daru::View::Table(s) or both
# combined = Daru::View::Plot([line_chart, bar_chart])
# combined = Daru::View::PlotList([line_chart, bar_chart])
def init(data=[], options={})
# When multiple charts are shown in a row, @chart will contain the
# instance of GoogleVisular::BaseChart so that its data can contain
Expand Down
20 changes: 0 additions & 20 deletions lib/daru/view/plot_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ def show_in_iruby
@adapter.show_in_iruby @charts
end

# dependent js file, to include in head tag using the plot object.
# @example:
# plot_obj.init_script
#
# Note :
# User can directly put the dependent script file into the head tag
# using `Daru::View.dependent_script(:highcharts), by default it loads
# Nyaplot JS files.
#
def init_script
@adapter.init_script
end

# generat html code, to include in body tag
def div
@adapter.generate_body(@charts)
Expand All @@ -75,13 +62,6 @@ def export_html_file(path='./plot.html')
@adapter.export_html_file(@charts, path)
end

# load the corresponding JS files in IRuby notebook.
# This is done automatically when plotting library is set using
# Daru::View.plotting_library = :new_library
def init_iruby
@adapter.init_iruby
end

private

def plot_data(data)
Expand Down
5 changes: 5 additions & 0 deletions lib/daru/view/templates/googlecharts/static_html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<html lang='en'>
<head>
<!-- TODO: extend and display with table and chart both in html table -->
<% if plot.data.is_a?(Array) && (plot.data[0].is_a?(Daru::View::Plot) ||
plot.data[0].is_a?(Daru::View::Table)) %>
<title>Multiple Charts</title>
<% else %>
<title>Chart_<%= id %>_<%= @chart_type%></title>
<% end %>
<%= initial_script %>
</head>
<body>
Expand Down
57 changes: 57 additions & 0 deletions spec/plot_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
describe Daru::View::PlotList, 'Charts plotting with Googlecharts library' do
before { Daru::View.plotting_library = :googlecharts }
let(:options) {{width: 800, height: 720}}
let(:df) do
Daru::DataFrame.new(
{
a: [1, 2, 3, 4, 5, 6],
b: [1, 5, 2, 5, 1, 0],
c: [1, 6, 7, 2, 6, 0]
}, index: 'a'..'f'
)
end
let(:plot_array) { [plot_df, plot_dv, table_array] }
let(:dv) { Daru::Vector.new [1, 2, 3] }
let(:plot_df) { Daru::View::Plot.new(df, options) }
let(:plot_dv) { Daru::View::Plot.new(dv, type: :bar) }
let(:table_array) { Daru::View::Table.new(
[['col1', 'col2', 'col3'],[1, 2, 3]], options) }
let(:combined) { Daru::View::PlotList.new(plot_array) }
context 'initialize with Googlecharts' do
it 'checks the set adapter' do
expect(combined.adapter).to eq(Daru::View::Adapter::GooglechartsAdapter)
end
it 'sets correct data' do
expect(combined).to be_a Daru::View::PlotList
expect(combined.data).to eq plot_array
expect(combined.charts).to be_a GoogleVisualr::BaseChart
end
end
describe '#div' do
subject (:js) { combined.div }
it 'genrates a table in js' do
expect(js).to match(/<table class='columns'>/)
expect(js).to match(/<td><div id=/)
end
it 'generates js of the three plots' do
expect(js).to match(/google.visualization.LineChart/)
expect(js).to match(/google.visualization.BarChart/)
expect(js).to match(/google.visualization.Table/)
end
end
describe '#export_html_file' do
it 'writes valid html code of the multiple Charts to the file' do
combined.export_html_file('./plot.html')
path = File.expand_path('../plot.html', __dir__)
content = File.read(path)
expect(content).to match(/html/i)
expect(content).to match(/<script>/i)
expect(content).to match(/Multiple Charts/i)
expect(content).to match(/google.visualization.DataTable\(\);/i)
expect(content).to match(/google.visualization.LineChart/)
expect(content).to match(/google.visualization.BarChart/)
expect(content).to match(/google.visualization.Table/)
expect(content).to match(/chart.draw\(data_table, \{\}/i)
end
end
end
2 changes: 1 addition & 1 deletion spec/plot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let(:plot_df) { Daru::View::Plot.new(df, type: :line, x: :a, y: :c) }
let(:plot_dv) { Daru::View::Plot.new(dv, type: :line) }
context 'initialize' do
# before { Daru::View.plotting_library = :nyaplot }
before { Daru::View.plotting_library = :nyaplot }
context 'Default plotting_library is nyaplot' do
it { expect(Daru::View.plotting_library).to eq(:nyaplot)}
end
Expand Down

0 comments on commit 132cbf6

Please sign in to comment.