diff --git a/lib/daru/view/adapters/googlecharts.rb b/lib/daru/view/adapters/googlecharts.rb index e56a54b..e21dd0f 100644 --- a/lib/daru/view/adapters/googlecharts.rb +++ b/lib/daru/view/adapters/googlecharts.rb @@ -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 diff --git a/lib/daru/view/plot_list.rb b/lib/daru/view/plot_list.rb index d7a7165..972ba2c 100644 --- a/lib/daru/view/plot_list.rb +++ b/lib/daru/view/plot_list.rb @@ -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) @@ -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) diff --git a/lib/daru/view/templates/googlecharts/static_html.erb b/lib/daru/view/templates/googlecharts/static_html.erb index b79a3ad..19ed77c 100644 --- a/lib/daru/view/templates/googlecharts/static_html.erb +++ b/lib/daru/view/templates/googlecharts/static_html.erb @@ -1,7 +1,12 @@ + <% if plot.data.is_a?(Array) && (plot.data[0].is_a?(Daru::View::Plot) || + plot.data[0].is_a?(Daru::View::Table)) %> + Multiple Charts + <% else %> Chart_<%= id %>_<%= @chart_type%> + <% end %> <%= initial_script %> diff --git a/spec/plot_list_spec.rb b/spec/plot_list_spec.rb new file mode 100644 index 0000000..b081e78 --- /dev/null +++ b/spec/plot_list_spec.rb @@ -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(//) + expect(js).to match(/
/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 \ No newline at end of file diff --git a/spec/plot_spec.rb b/spec/plot_spec.rb index ddbed39..2c363d3 100644 --- a/spec/plot_spec.rb +++ b/spec/plot_spec.rb @@ -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