class Daru::View::Plot

Attributes

adapter[RW]
chart[R]
data[R]
options[R]
user_options[R]

Public Class Methods

adapter=(adapter) click to toggle source

class method

@example

Daru::View::Plot.adapter = :googlecharts

Plotting libraries are nyaplot, highcharts, googlecharts

# File lib/daru/view/plot.rb, line 14
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
new(data=[], options={}, user_options={}, &block) click to toggle source

@example

df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:}) Daru::View::Plot.new df, type: :bar, x: :a, y: :b, adapter: :nyaplot

Set the new adapter(plotting library) ,for example highcharts:

Daru::View.plotting_library = :highcharts

To use a particular adapter in certain plot object(s), then user must pass the adapter in `options` hash. e.g. `adapter: :highcharts`

# File lib/daru/view/plot.rb, line 36
def initialize(data=[], options={}, user_options={}, &block)
  # TODO: &block is not used, right now.
  @data = data
  @options = options
  @user_options = user_options
  self.adapter = options.delete(:adapter) unless options[:adapter].nil?
  @chart = plot_data(data, options, user_options, &block)
end

Public Instance Methods

adapter=(adapter) click to toggle source

instance method

# File lib/daru/view/plot.rb, line 46
def adapter=(adapter)
  require "daru/view/adapters/#{adapter}"
  @adapter = Daru::View::Adapter.const_get(
    adapter.to_s.capitalize + 'Adapter'
  )
end
add_series(opts={}) click to toggle source
# File lib/daru/view/plot.rb, line 100
def add_series(opts={})
  case adapter
  when Daru::View::Adapter::HighchartsAdapter
    @chart = @adapter.add_series(@chart, opts)
  else
    raise("Method `add-series` is not valid for #{@adapter}.to_s.capitalize library.")
  end
end
div() click to toggle source

generat html code, to include in body tag

# File lib/daru/view/plot.rb, line 72
def div
  @adapter.generate_body(@chart)
end
export(export_type='png', file_name='chart') click to toggle source

@param type [String] format to which chart has to be exported @param file_name [String] The name of the file after exporting the chart @return [String, void] js code of chart along with the code to export it

and loads the js code to export it in IRuby.

@example

data = Daru::Vector.new([5 ,3, 4])
chart = Daru::View::Plot.new(data)
chart.export('png', 'daru')
# File lib/daru/view/plot.rb, line 89
def export(export_type='png', file_name='chart')
  @adapter.export(@chart, export_type, file_name)
end
export_html_file(path='./plot.html') click to toggle source

generat html file

# File lib/daru/view/plot.rb, line 77
def export_html_file(path='./plot.html')
  @adapter.export_html_file(@chart, path)
end
init_iruby() click to toggle source

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

# File lib/daru/view/plot.rb, line 96
def init_iruby
  @adapter.init_iruby
end
init_script() click to toggle source

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.

# File lib/daru/view/plot.rb, line 67
def init_script
  @adapter.init_script
end
show_in_iruby() click to toggle source

display in IRuby notebook

# File lib/daru/view/plot.rb, line 54
def show_in_iruby
  @adapter.show_in_iruby @chart
end

Private Instance Methods

plot_data(data, options, user_options) click to toggle source
# File lib/daru/view/plot.rb, line 111
def plot_data(data, options, user_options)
  # class variable @@aapter is used in instance variable @adapter.
  # so in each object `adapter` variable can be accessed.
  @adapter ||= @@adapter
  @adapter.init(data, options, user_options)
end