Skip to content

Commit

Permalink
Add PlotList class
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jun 24, 2018
1 parent a291110 commit 759b218
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 324 deletions.
5 changes: 4 additions & 1 deletion lib/daru/view.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'daru/view/version'
require 'daru/view/plot'
require 'daru/view/plot_list'
require 'daru/view/adapters/highcharts/display'
require 'daru/view/adapters/nyaplot/display'
require 'daru/view/adapters/googlecharts/display'
Expand All @@ -18,6 +19,7 @@ module View
# default Nyaplot library is used.
@plotting_library = :nyaplot
Daru::View::Plot.adapter = @plotting_library
Daru::View::PlotList.adapter = @plotting_library

class << self
attr_reader :plotting_library, :table_library
Expand All @@ -28,7 +30,7 @@ def plotting_library=(lib)
when :nyaplot, :highcharts, :googlecharts
# plot charts
@plotting_library = lib
Daru::View::Plot.adapter = lib
Daru::View::Plot.adapter = Daru::View::PlotList.adapter = lib
if lib == :googlecharts
# plot table drawing
Daru::View::Table.adapter = lib
Expand All @@ -53,6 +55,7 @@ def table_library=(lib)
# plot chart and table drawing
@plotting_library = @table_library = lib
Daru::View::Plot.adapter = Daru::View::Table.adapter = lib
Daru::View::PlotList.adapter = lib
when :datatables
# only for table drawing
@table_library = Daru::View::Table.adapter = lib
Expand Down
95 changes: 95 additions & 0 deletions lib/daru/view/plot_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Daru
module View
class PlotList
attr_reader :charts, :data
attr_accessor :adapter
class << self
# class method
#
# @example
#
# Daru::View::PlotList.adapter = :googlecharts
#
# Plotting libraries are nyaplot, highcharts, googlecharts
def adapter=(adapter)
require "daru/view/adapters/#{adapter}"
# rubocop:disable Style/ClassVars
@@adapter = Daru::View::Adapter.const_get(
adapter.to_s.capitalize + 'Adapter'
)
# rubocop:enable Style/ClassVars
end
end

# @example
#
# Daru::View.plotting_library = :googlecharts
#
# df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]})
# plot1 = Daru::View::Plot.new(
# df, type: :bar, x: :a, y: :b
# )
# plot2 = Daru::View::Plot.new(
# df, type: :column, x: :a, y: :b
# )
# plots = Daru::View::PlotList.new([plot1, plot2])
#
def initialize(data=[])
@data = data
@charts = plot_data(data)
end

# instance method
def adapter=(adapter)
require "daru/view/adapters/#{adapter}"
@adapter = Daru::View::Adapter.const_get(
adapter.to_s.capitalize + 'Adapter'
)
end

# display in IRuby notebook
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)
end

# generat html file
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)
# class variable @@aapter is used in instance variable @adapter.
# so in each object `adapter` variable can be accessed.
@adapter ||= @@adapter
@adapter.init(data)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/adapters/googlecharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
let(:column_chart_chart) {Daru::View::Plot.
new(data_table.table, column_chart_options)}
let(:combined) {
Daru::View::Plot.new([data_table, area_chart_chart, column_chart_chart])
Daru::View::PlotList.new([data_table, area_chart_chart, column_chart_chart])
}

describe "#to_html" do
Expand Down Expand Up @@ -84,7 +84,7 @@
/data_table.addRow\(\[\{v: \"2013\"\}\]\);/i)
end
it "generates valid js of the combined charts" do
js = combined.chart.to_html
js = combined.charts.to_html
expect(js).to match(/google.visualization.Table/)
expect(js).to match(/google.visualization.AreaChart/)
expect(js).to match(/google.visualization.ColumnChart/)
Expand All @@ -93,13 +93,13 @@

describe "#extract_multiple_charts_id_script_path" do
it "returns correct path of the multiple_charts_div" do
id_script_path = combined.chart.extract_multiple_charts_id_script_path
id_script_path = combined.charts.extract_multiple_charts_id_script_path
expect(id_script_path[2]).to match(
/templates\/googlecharts\/multiple_charts_div.erb/
)
end
it "returns correct scripts of the multiple charts" do
id_script_path = combined.chart.extract_multiple_charts_id_script_path
id_script_path = combined.charts.extract_multiple_charts_id_script_path
expect(id_script_path[1][0]).to match(/google.visualization.Table/)
expect(id_script_path[1][1]).to match(/google.visualization.AreaChart/)
expect(id_script_path[1][2]).to match(/google.visualization.ColumnChart/)
Expand Down
4 changes: 2 additions & 2 deletions spec/adapters/googlecharts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
let(:column_chart_chart) {Daru::View::Plot.
new(data_table.table, column_chart_options)}
let(:combined) {
Daru::View::Plot.new([data_table, area_chart_chart, column_chart_chart])
Daru::View::PlotList.new([data_table, area_chart_chart, column_chart_chart])
}

describe "initialization Charts" do
Expand Down Expand Up @@ -211,7 +211,7 @@
expect(js).to match(/chart.draw\(data_table, \{width: 800\}/i)
end
it "generates valid js of the combined charts" do
js = combined.adapter.generate_body(combined.chart)
js = combined.adapter.generate_body(combined.charts)
expect(js).to match(/google.visualization.Table/)
expect(js).to match(/google.visualization.AreaChart/)
expect(js).to match(/google.visualization.ColumnChart/)
Expand Down
Loading

0 comments on commit 759b218

Please sign in to comment.