Skip to content

Commit

Permalink
Merge branch 'master' into export_highchart
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith authored Jul 13, 2018
2 parents 282be2d + 2a62667 commit 2c4e273
Show file tree
Hide file tree
Showing 16 changed files with 3,136 additions and 280 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ Style/SingleLineBlockParams:
Style/PerlBackrefs:
Enabled: false

# Tried to disable in the file itself but this style rubocop was not there in
# ruby 2.0, so it threw Lint/UnneededDisable rubocop offence.
Style/MixinUsage:
Exclude:
- 'lib/daru/view/plot_list.rb'

Layout/SpaceAfterComma:
Enabled: false

Expand Down
1 change: 1 addition & 0 deletions 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 Down
5 changes: 5 additions & 0 deletions lib/daru/view/adapters/googlecharts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ module GooglechartsAdapter
# data << query
# options = {type: :area}
# chart = Daru::View::Plot.new(data, options)
#
# @example Multiple Charts in a row
# 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::PlotList([line_chart, bar_chart])
def init(data=[], options={})
@table = GoogleVisualr::DataTable.new
@table = get_table(data) unless data.is_a?(String)
Expand Down
2 changes: 1 addition & 1 deletion lib/daru/view/adapters/highcharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def to_html(placeholder=random_canvas_id)

def show_in_iruby(placeholder=random_canvas_id)
# TODO : placeholder pass, in plot#div
load_dependencies('iruby')
IRuby.html to_html_iruby(placeholder)
end

Expand All @@ -95,6 +94,7 @@ def show_in_iruby(placeholder=random_canvas_id)
def to_html_iruby(placeholder=random_canvas_id)
# TODO : placeholder pass, in plot#div
@div_id = placeholder
load_dependencies('iruby')
chart_hash_must_be_present
script = high_chart_css(placeholder)
script << high_chart_iruby(extract_chart_class, placeholder, self)
Expand Down
102 changes: 102 additions & 0 deletions lib/daru/view/plot_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'erb'
require 'daru/view/adapters/highcharts/display'
require 'daru/view/adapters/nyaplot/display'
require 'daru/view/adapters/googlecharts/display'

# Otherwise Daru::IRuby module was used and IRuby.html method was not working.
# Have disabled rubocop Style/Mixin for this in .rubocop.yml file
include IRuby::Utils if defined?(IRuby)

module Daru
module View
class PlotList
attr_reader :data

# @param data [Daru::View::Plot, Daru::View::Table] data to visualize
# @return [void] initialize PlotList with data
# @example
#
# 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, adapter: :googlecharts
# )
# plot2 = Daru::View::Plot.new(
# df, chart: { type: 'line' }, adapter: :highcharts
# )
# plots = Daru::View::PlotList.new([plot1, plot2])
#
def initialize(data=[])
unless data.is_a?(Array) && data.all? { |plot|
plot.is_a?(Daru::View::Plot) ||
plot.is_a?(Daru::View::Table)
}
raise ArgumentError, 'Invalid Argument Passed! Valid Arguments '\
'consists an Array of: Daru::View::Plot or '\
'Daru::View::Table (Right now, it is not '\
'implemented for DataTables)'
end
@data = data
end

# @return [void] display in IRuby notebook
def show_in_iruby
IRuby.html(div)
end

# @return [String] generates html code to include in body tag
def div
path = File.expand_path('templates/multiple_charts_div.erb', __dir__)
template = File.read(path)
charts_id_div_tag = []
charts_script = extract_charts_script(charts_id_div_tag)
ERB.new(template).result(binding)
end

# @return [void] writes a html file to disk
def export_html_file(path='./plot.html')
path = File.expand_path(path, Dir.pwd)
str = generate_html
File.write(path, str)
end

private

def extract_charts_script(charts_id_div_tag=[])
charts_script = ''
@data.each do |plot|
chart_script = extract_chart_script(plot)
charts_id_div_tag << chart_script.partition(%r{<div(.*?)<\/div>}ixm)[1]
chart_script.sub!(%r{<div(.*?)<\/div>}ixm, '')
charts_script << chart_script
end
charts_script
end

def extract_chart_script(plot)
# TODO: Implement this for datatables too
return plot.div unless defined?(IRuby.html) &&
plot.is_a?(Daru::View::Plot) &&
plot.chart.is_a?(LazyHighCharts::HighChart)
plot.chart.to_html_iruby
end

def generate_html
path = File.expand_path(
'templates/static_html_multiple_charts.erb', __dir__
)
template = File.read(path)
charts_script = div
set_init_script = {}
initial_script = ''
@data.each do |plot|
adapter = plot.adapter
unless set_init_script[adapter]
set_init_script[adapter] = true
initial_script << plot.adapter.init_script
end
end
ERB.new(template).result(binding)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/daru/view/templates/multiple_charts_div.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<table>
<tr>
<% charts_id_div_tag.each do |chart_div_tag| %>
<td><%= chart_div_tag %></td>
<% end %>
</tr>
</table>
<%= charts_script %>
10 changes: 10 additions & 0 deletions lib/daru/view/templates/static_html_multiple_charts.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html lang='en'>
<head>
<!-- TODO: extend and display with table and chart both in html table -->
<title>Multiple Charts</title>
<%= initial_script %>
</head>
<body>
<%= charts_script %>
</body>
</html>
1 change: 0 additions & 1 deletion spec/adapters/googlecharts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
let(:column_chart_chart) {Daru::View::Plot.
new(data_table.table, column_chart_options)}


describe "initialization Charts" do
it "Default chart GoogleVisualr::Interactive::LineChart " do
expect(Daru::View::Plot.new.chart)
Expand Down
25 changes: 0 additions & 25 deletions spec/adapters/highcharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,31 +328,6 @@
end
end

describe "#to_html_iruby" do
it "should plot HighMap when chart_class is set to map" do
@hc.options[:chart_class] = "Map";
expect(@hc.chart.to_html_iruby(
@placeholder)
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Map/)
end
it "should plot HighStock when chart_class is set to stock" do
@hc.options[:chart_class] = "SToCk";
expect(@hc.chart.to_html_iruby(
@placeholder)
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.StockChart/)
end
it "should plot HighChart otherwise" do
expect(@hc.chart.to_html_iruby(
@placeholder)
).to match(/window\.chart_placeholder\s+=\s+new\s+Highcharts.Chart/)
end
it "should set css correctly" do
expect(@hc.chart.to_html_iruby(
@placeholder)
).to match(/#placeholder .highcharts-background {fill/)
end
end

describe "#export" do
it "should generate the valid script to export the chart in different"\
" formats" do
Expand Down
Loading

0 comments on commit 2c4e273

Please sign in to comment.