forked from SciRuby/daru-view
-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.rb
126 lines (117 loc) · 3.82 KB
/
view.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
119
120
121
122
123
124
125
126
require 'daru/view/version'
require 'daru/view/plot'
require 'daru/view/adapters/highcharts/display'
require 'daru/view/adapters/nyaplot/display'
require 'daru/view/adapters/googlecharts/display'
require 'daru/view/table'
# needed in load_lib_in_iruby method
require 'daru/view/adapters/highcharts/iruby_notebook'
require 'daru/view/adapters/nyaplot/iruby_notebook'
require 'daru/view/adapters/googlecharts/iruby_notebook'
# Rails Helper
require 'daru/view/app/rails/railtie.rb' if defined?(Rails)
module Daru
module View
# default Nyaplot library is used.
@plotting_library = :nyaplot
Daru::View::Plot.adapter = @plotting_library
class << self
attr_reader :plotting_library, :table_library
# New plotting library is set. Same time Daru::View::Plot.adapter is set.
def plotting_library=(lib)
case lib
when :nyaplot, :highcharts, :googlecharts
# plot charts
@plotting_library = lib
Daru::View::Plot.adapter = lib
if lib == :googlecharts
# plot table drawing
Daru::View::Table.adapter = lib
end
else
raise ArgumentError, "Unsupported library #{lib}"
end
# When code is running in console/terminal then IRuby NameError.
# Since IRuby methods can't work in console.
begin
load_lib_in_iruby lib.to_s if defined? IRuby
rescue NameError
return
end
end
# New table library is set. Same time Daru::View::Table.adapter is set.
def table_library=(lib)
case lib
when :googlecharts
# plot chart and table drawing
@plotting_library = @table_library = lib
Daru::View::Plot.adapter = Daru::View::Table.adapter = lib
when :datatables
# only for table drawing
@table_library = Daru::View::Table.adapter = lib
else
raise ArgumentError, "Unsupported library #{lib}"
end
# When code is running in console/terminal then IRuby NameError.
# Since IRuby methods can't work in console.
begin
load_lib_in_iruby lib.to_s if defined? IRuby
rescue NameError
return
end
end
# Load the dependent JS files in IRuby notebook. Those JS will help in
# plotting the charts in IRuby cell.
#
# @example
#
# To load the dependent JS file for Nyaplot library
# plotting system (Nyaplot.js, d3.js):
#
# Daru::View.load_lib_in_iruby('nyaplot')
#
# To load the HighCharts dependent JS
# files (highcharts.js, highcharts-3d.js, highstock.js):
#
# Daru::View.load_lib_in_iruby('highcharts')
#
def load_lib_in_iruby(library)
if library.match('highcharts')
library = 'LazyHighCharts'
Object.const_get(library).init_iruby
elsif library.match('googlecharts')
library = 'GoogleVisualr'
Object.const_get(library).init_iruby
elsif library.match('datatables')
library = 'DataTables'
Object.const_get(library).init_iruby
else
Object.const_get(library.capitalize).init_iruby
end
end
# dependent script for the library. It must be added in the head tag
# of the web application.
#
# @example
#
# dep_js = Daru::View.dependent_script(:highcharts)
#
# use in Rails app : <%=raw dep_js %>
#
def dependent_script(lib=:nyaplot)
case lib
when :nyaplot
Nyaplot.init_script
when :highcharts
LazyHighCharts.init_script
when :googlecharts
GoogleVisualr.init_script
when :datatables
DataTables.init_script
else
raise ArgumentError, "Unsupported library #{lib}"
end
end
end
end
end