forked from SciRuby/daru-view
/
plot.rb
118 lines (107 loc) · 3.65 KB
/
plot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Daru
module View
class Plot
attr_reader :chart, :data, :options
attr_accessor :adapter
class << self
# class method
#
# @example
#
# Daru::View::Plot.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
#
# df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]})
# 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`
#
def initialize(data=[], options={}, &block)
# TODO: &block is not used, right now.
@data = data
@options = options
self.adapter = options.delete(:adapter) unless options[:adapter].nil?
@chart = plot_data(data, options, &block)
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 @chart
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(@chart)
end
# generat html file
def export_html_file(path='./plot.html')
@adapter.export_html_file(@chart, path)
end
# @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 Export a HighChart
# data = Daru::Vector.new([5 ,3, 4])
# hchart = Daru::View::Plot.new(data)
# hchart.export('png', 'daru')
def export(export_type='png', file_name='chart')
@adapter.export(@chart, export_type, file_name)
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
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
private
def plot_data(data, 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)
end
end
end
end