class Daru::View::PlotList

Attributes

data[R]

Public Class Methods

new(data=[]) click to toggle source

@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:}) 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])

# File lib/daru/view/plot_list.rb, line 28
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

Public Instance Methods

div() click to toggle source

@return [String] generates html code to include in body tag

# File lib/daru/view/plot_list.rb, line 47
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
export_html_file(path='./plot.html') click to toggle source

@return [void] writes a html file to disk

# File lib/daru/view/plot_list.rb, line 56
def export_html_file(path='./plot.html')
  path = File.expand_path(path, Dir.pwd)
  str = generate_html
  File.write(path, str)
end
show_in_iruby() click to toggle source

@return [void] display in IRuby notebook

# File lib/daru/view/plot_list.rb, line 42
def show_in_iruby
  IRuby.html(div)
end

Private Instance Methods

extract_chart_script(plot) click to toggle source
# File lib/daru/view/plot_list.rb, line 75
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
extract_charts_script(charts_id_div_tag=[]) click to toggle source
# File lib/daru/view/plot_list.rb, line 64
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
generate_html() click to toggle source
# File lib/daru/view/plot_list.rb, line 84
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