Class: Daru::View::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/view/plot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = [], options = {}, user_options = {}, &block) ⇒ Plot

df = Daru::DataFrame.new(‘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`



36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



5
6
7
# File 'lib/daru/view/plot.rb', line 5

def adapter
  @adapter
end

#chartObject (readonly)

Returns the value of attribute chart.



4
5
6
# File 'lib/daru/view/plot.rb', line 4

def chart
  @chart
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/daru/view/plot.rb', line 4

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/daru/view/plot.rb', line 4

def options
  @options
end

#user_optionsObject (readonly)

Returns the value of attribute user_options.



4
5
6
# File 'lib/daru/view/plot.rb', line 4

def user_options
  @user_options
end

Class Method Details

.adapter=(adapter) ⇒ Object

class method

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

Plotting libraries are nyaplot, highcharts, googlecharts



14
15
16
17
18
19
20
21
# 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

Instance Method Details

#add_series(opts = {}) ⇒ Object



100
101
102
103
104
105
106
107
# 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

#divObject

generat html code, to include in body tag



72
73
74
# File 'lib/daru/view/plot.rb', line 72

def div
  @adapter.generate_body(@chart)
end

#export(export_type = 'png', file_name = 'chart') ⇒ String, void

Returns js code of chart along with the code to export it and loads the js code to export it in IRuby.

Examples:

data = Daru::Vector.new([5 ,3, 4])
chart = Daru::View::Plot.new(data)
chart.export('png', 'daru')

Parameters:

  • type (String)

    format to which chart has to be exported

  • file_name (String) (defaults to: 'chart')

    The name of the file after exporting the chart

Returns:

  • (String, void)

    js code of chart along with the code to export it and loads the js code to export it in IRuby.



89
90
91
# 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') ⇒ Object

generat html file



77
78
79
# File 'lib/daru/view/plot.rb', line 77

def export_html_file(path='./plot.html')
  @adapter.export_html_file(@chart, path)
end

#init_irubyObject

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



96
97
98
# File 'lib/daru/view/plot.rb', line 96

def init_iruby
  @adapter.init_iruby
end

#init_scriptObject

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.



67
68
69
# File 'lib/daru/view/plot.rb', line 67

def init_script
  @adapter.init_script
end

#show_in_irubyObject

display in IRuby notebook



54
55
56
# File 'lib/daru/view/plot.rb', line 54

def show_in_iruby
  @adapter.show_in_iruby @chart
end