forked from SciRuby/daru-view
/
nyaplot.rb
65 lines (55 loc) · 1.58 KB
/
nyaplot.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
require_relative 'nyaplot/iruby_notebook'
require 'daru'
require 'nyaplot'
module Daru
module View
module Adapter
module NyaplotAdapter
extend self # rubocop:disable Style/ModuleFunction
def init(data, options)
data_new = guess_data(data)
data_new.plot(options)
end
def export_html_file(plot, path)
plot.export_html path
end
def export(_plot, _export_type='png', _file_name='chart')
raise 'Not implemented yet'
end
def show_in_iruby(plot)
plot.show
end
def init_script
Nyaplot.init_script
end
def generate_body(plot)
plot.to_iruby[1]
end
def init_iruby
Nyaplot.init_iruby
end
private
def multi_dimension_check(arr)
arr.all? { |a| a.class==Array }
end
def guess_data(data_set)
case
when [Daru::DataFrame, Daru::Vector].include?(data_set.class)
data_set
when data_set.is_a?(Array)
if data_set.empty?
Daru::Vector.new([])
else
multi_dimension_check(data_set)? Daru::DataFrame.new(data_set) : Daru::Vector.new(data_set)
end
else
raise ArgumentError, "For Nyaplot Library, data must be in
Daru::Vector or Daru::DataFrame.\n You can change the plotting
library using the code : \n
` Daru::View.plotting_library = :highcharts` or other library."
end
end
end
end
end
end