daru-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +122 -0
  5. data/.simplecov +3 -0
  6. data/.travis.yml +18 -0
  7. data/CHANGELOG.md +21 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/CONTRIBUTING.md +53 -0
  10. data/Gemfile +11 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +287 -0
  13. data/Rakefile +22 -0
  14. data/_config.yml +1 -0
  15. data/daru-view.gemspec +52 -0
  16. data/lib/assets/.keep +0 -0
  17. data/lib/daru/view.rb +127 -0
  18. data/lib/daru/view/adapters/datatables.rb +90 -0
  19. data/lib/daru/view/adapters/googlecharts.rb +180 -0
  20. data/lib/daru/view/adapters/googlecharts/data_table_iruby.rb +86 -0
  21. data/lib/daru/view/adapters/googlecharts/display.rb +54 -0
  22. data/lib/daru/view/adapters/googlecharts/iruby_notebook.rb +15 -0
  23. data/lib/daru/view/adapters/highcharts.rb +114 -0
  24. data/lib/daru/view/adapters/highcharts/core_ext/string.rb +10 -0
  25. data/lib/daru/view/adapters/highcharts/display.rb +58 -0
  26. data/lib/daru/view/adapters/highcharts/iruby_notebook.rb +29 -0
  27. data/lib/daru/view/adapters/highcharts/layout_helper_iruby.rb +76 -0
  28. data/lib/daru/view/adapters/js/googlecharts_js/google_visualr.js +44 -0
  29. data/lib/daru/view/adapters/js/googlecharts_js/loader.js +226 -0
  30. data/lib/daru/view/adapters/js/highcharts_js/adapters/mootools-adapter.js +13 -0
  31. data/lib/daru/view/adapters/js/highcharts_js/adapters/prototype-adapter.js +15 -0
  32. data/lib/daru/view/adapters/js/highcharts_js/highcharts-3d.js +74 -0
  33. data/lib/daru/view/adapters/js/highcharts_js/highcharts-more.js +64 -0
  34. data/lib/daru/view/adapters/js/highcharts_js/highcharts.js +399 -0
  35. data/lib/daru/view/adapters/js/highcharts_js/highstock.js +519 -0
  36. data/lib/daru/view/adapters/js/highcharts_js/modules/accessibility.js +48 -0
  37. data/lib/daru/view/adapters/js/highcharts_js/modules/annotations.js +14 -0
  38. data/lib/daru/view/adapters/js/highcharts_js/modules/boost.js +51 -0
  39. data/lib/daru/view/adapters/js/highcharts_js/modules/broken-axis.js +15 -0
  40. data/lib/daru/view/adapters/js/highcharts_js/modules/canvas-tools.js +133 -0
  41. data/lib/daru/view/adapters/js/highcharts_js/modules/data.js +26 -0
  42. data/lib/daru/view/adapters/js/highcharts_js/modules/drilldown.js +25 -0
  43. data/lib/daru/view/adapters/js/highcharts_js/modules/exporting.js +27 -0
  44. data/lib/daru/view/adapters/js/highcharts_js/modules/funnel.js +13 -0
  45. data/lib/daru/view/adapters/js/highcharts_js/modules/heatmap.js +25 -0
  46. data/lib/daru/view/adapters/js/highcharts_js/modules/no-data-to-display.js +12 -0
  47. data/lib/daru/view/adapters/js/highcharts_js/modules/offline-exporting.js +19 -0
  48. data/lib/daru/view/adapters/js/highcharts_js/modules/solid-gauge.js +14 -0
  49. data/lib/daru/view/adapters/js/highcharts_js/modules/treemap.js +31 -0
  50. data/lib/daru/view/adapters/js/highcharts_js/stock/adapters/mootools-adapter.js +13 -0
  51. data/lib/daru/view/adapters/js/highcharts_js/stock/adapters/prototype-adapter.js +15 -0
  52. data/lib/daru/view/adapters/js/highcharts_js/stock/highcharts-more.js +64 -0
  53. data/lib/daru/view/adapters/js/highcharts_js/stock/modules/exporting.js +27 -0
  54. data/lib/daru/view/adapters/js/highcharts_js/stock/modules/funnel.js +13 -0
  55. data/lib/daru/view/adapters/js/nyaplot_js/d3-downloadable.js +129 -0
  56. data/lib/daru/view/adapters/js/nyaplot_js/d3.min.js +5 -0
  57. data/lib/daru/view/adapters/js/nyaplot_js/nyaplot.js +5294 -0
  58. data/lib/daru/view/adapters/nyaplot.rb +62 -0
  59. data/lib/daru/view/adapters/nyaplot/display.rb +9 -0
  60. data/lib/daru/view/adapters/nyaplot/iruby_notebook.rb +22 -0
  61. data/lib/daru/view/app/rails/helpers/view_helper.rb +26 -0
  62. data/lib/daru/view/app/rails/railtie.rb +15 -0
  63. data/lib/daru/view/plot.rb +106 -0
  64. data/lib/daru/view/table.rb +99 -0
  65. data/lib/daru/view/templates/googlecharts/chart_div.erb +4 -0
  66. data/lib/daru/view/templates/googlecharts/init.inline.js.erb +16 -0
  67. data/lib/daru/view/templates/highcharts/init.inline.js.erb +16 -0
  68. data/lib/daru/view/templates/highcharts/static_html.erb +7 -0
  69. data/lib/daru/view/templates/nyaplot/init.inline.js.erb +16 -0
  70. data/lib/daru/view/templates/nyaplot/init_script.erb +3 -0
  71. data/lib/daru/view/version.rb +5 -0
  72. data/lib/tasks/.keep +0 -0
  73. data/lib/tasks/google_charts.rake +25 -0
  74. data/lib/tasks/high_charts.rake +60 -0
  75. data/lib/tasks/nyaplot.rake +25 -0
  76. data/spec/adapters/datatables_spec.rb +21 -0
  77. data/spec/adapters/googlecharts_spec.rb +34 -0
  78. data/spec/adapters/highcharts_spec.rb +177 -0
  79. data/spec/adapters/nyaplot_spec.rb +447 -0
  80. data/spec/app/rails/helpers/view_helpers_spec.rb +35 -0
  81. data/spec/dummy_iruby/.ipynb_checkpoints/Daru DataFrame and DataTables-checkpoint.ipynb +16091 -0
  82. data/spec/dummy_iruby/.ipynb_checkpoints/DataTables | basic examples-checkpoint.ipynb +6 -0
  83. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Line Chart-checkpoint.ipynb +1389 -0
  84. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Area Chart-checkpoint.ipynb +606 -0
  85. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Bar Chart-checkpoint.ipynb +1404 -0
  86. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Bubble Chart-checkpoint.ipynb +866 -0
  87. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Candlestick Chart-checkpoint.ipynb +607 -0
  88. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Combo Chart-checkpoint.ipynb +478 -0
  89. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Histogram-checkpoint.ipynb +1182 -0
  90. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Org Chart-checkpoint.ipynb +6 -0
  91. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Pie Chart-checkpoint.ipynb +6 -0
  92. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Stepped Area Chart-checkpoint.ipynb +699 -0
  93. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Timeline-checkpoint.ipynb +1523 -0
  94. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | Treemap-checkpoint.ipynb +650 -0
  95. data/spec/dummy_iruby/.ipynb_checkpoints/Google Chart | gauge-checkpoint.ipynb +477 -0
  96. data/spec/dummy_iruby/.ipynb_checkpoints/Google Charts | Basics-checkpoint.ipynb +1881 -0
  97. data/spec/dummy_iruby/.ipynb_checkpoints/Google Charts | Column Charts-checkpoint.ipynb +6 -0
  98. data/spec/dummy_iruby/.ipynb_checkpoints/Google Charts | Geo Charts examples-checkpoint.ipynb +791 -0
  99. data/spec/dummy_iruby/.ipynb_checkpoints/Google Charts | Scatter Chart-checkpoint.ipynb +1570 -0
  100. data/spec/dummy_iruby/.ipynb_checkpoints/GoolgeChart | Datatables-checkpoint.ipynb +1792 -0
  101. data/spec/dummy_iruby/.ipynb_checkpoints/HighCharts - Area chart-checkpoint.ipynb +1272 -0
  102. data/spec/dummy_iruby/.ipynb_checkpoints/HighCharts - Bar Chart-checkpoint.ipynb +877 -0
  103. data/spec/dummy_iruby/.ipynb_checkpoints/HighCharts - column charts-checkpoint.ipynb +1587 -0
  104. data/spec/dummy_iruby/.ipynb_checkpoints/HighCharts- Pie charts-checkpoint.ipynb +1126 -0
  105. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts - Combinations -checkpoint.ipynb +925 -0
  106. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts - Daynamic charts-checkpoint.ipynb +839 -0
  107. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts - line graphs-checkpoint.ipynb +1297 -0
  108. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts - line graphs2-checkpoint.ipynb +879 -0
  109. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts - scatter -bubble charts-checkpoint.ipynb +1071 -0
  110. data/spec/dummy_iruby/.ipynb_checkpoints/Highcharts | add_series method and highcharts examples-checkpoint.ipynb +1165 -0
  111. data/spec/dummy_iruby/.ipynb_checkpoints/Nyaplot basics | Daru Examples-checkpoint.ipynb +1383 -0
  112. data/spec/dummy_iruby/.ipynb_checkpoints/Nyaplot other examples-checkpoint.ipynb +6 -0
  113. data/spec/dummy_iruby/.ipynb_checkpoints/Nyaplot testing -checkpoint.ipynb +270 -0
  114. data/spec/dummy_iruby/.ipynb_checkpoints/Nyaplot | Creating Visualizations with DataFrame | from daru examples-checkpoint.ipynb +1716 -0
  115. data/spec/dummy_iruby/.ipynb_checkpoints/Pie Chart using HighCharts2-checkpoint.ipynb +784 -0
  116. data/spec/dummy_iruby/.ipynb_checkpoints/highcharts - Use HTML table (generated from daru) as data source-checkpoint.ipynb +956 -0
  117. data/spec/dummy_iruby/.ipynb_checkpoints/spec_testing-checkpoint.ipynb +1433 -0
  118. data/spec/dummy_iruby/Daru DataFrame and DataTables.ipynb +16091 -0
  119. data/spec/dummy_iruby/DataTables | basic examples.ipynb +15587 -0
  120. data/spec/dummy_iruby/Gemfile +33 -0
  121. data/spec/dummy_iruby/Google Chart | Line Chart.ipynb +1389 -0
  122. data/spec/dummy_iruby/Google Chart | Area Chart.ipynb +606 -0
  123. data/spec/dummy_iruby/Google Chart | Bar Chart.ipynb +1394 -0
  124. data/spec/dummy_iruby/Google Chart | Bubble Chart.ipynb +866 -0
  125. data/spec/dummy_iruby/Google Chart | Candlestick Chart.ipynb +607 -0
  126. data/spec/dummy_iruby/Google Chart | Combo Chart.ipynb +478 -0
  127. data/spec/dummy_iruby/Google Chart | Histogram.ipynb +1182 -0
  128. data/spec/dummy_iruby/Google Chart | Org Chart.ipynb +474 -0
  129. data/spec/dummy_iruby/Google Chart | Pie Chart.ipynb +1177 -0
  130. data/spec/dummy_iruby/Google Chart | Stepped Area Chart.ipynb +699 -0
  131. data/spec/dummy_iruby/Google Chart | Timeline.ipynb +1523 -0
  132. data/spec/dummy_iruby/Google Chart | Treemap.ipynb +659 -0
  133. data/spec/dummy_iruby/Google Chart | gauge.ipynb +477 -0
  134. data/spec/dummy_iruby/Google Charts | Basics.ipynb +1882 -0
  135. data/spec/dummy_iruby/Google Charts | Column Charts.ipynb +1138 -0
  136. data/spec/dummy_iruby/Google Charts | Geo Charts examples.ipynb +791 -0
  137. data/spec/dummy_iruby/Google Charts | Scatter Chart.ipynb +1570 -0
  138. data/spec/dummy_iruby/GoolgeChart | Datatables.ipynb +1801 -0
  139. data/spec/dummy_iruby/HighCharts - Area chart.ipynb +1272 -0
  140. data/spec/dummy_iruby/HighCharts - Bar Chart.ipynb +877 -0
  141. data/spec/dummy_iruby/HighCharts - column charts.ipynb +1587 -0
  142. data/spec/dummy_iruby/HighCharts- Pie charts.ipynb +1197 -0
  143. data/spec/dummy_iruby/Highcharts - Combinations .ipynb +925 -0
  144. data/spec/dummy_iruby/Highcharts - Daynamic charts.ipynb +839 -0
  145. data/spec/dummy_iruby/Highcharts - line graphs.ipynb +1297 -0
  146. data/spec/dummy_iruby/Highcharts - line graphs2.ipynb +879 -0
  147. data/spec/dummy_iruby/Highcharts - scatter -bubble charts.ipynb +1071 -0
  148. data/spec/dummy_iruby/Highcharts | add_series method and highcharts examples.ipynb +1228 -0
  149. data/spec/dummy_iruby/Nyaplot basics | Daru Examples.ipynb +1390 -0
  150. data/spec/dummy_iruby/Nyaplot other examples.ipynb +589 -0
  151. data/spec/dummy_iruby/Nyaplot testing .ipynb +270 -0
  152. data/spec/dummy_iruby/Nyaplot | Creating Visualizations with DataFrame | from daru examples.ipynb +1716 -0
  153. data/spec/dummy_iruby/Pie Chart using HighCharts2.ipynb +784 -0
  154. data/spec/dummy_iruby/highcharts - Use HTML table (generated from daru) as data source.ipynb +951 -0
  155. data/spec/dummy_iruby/spec_testing.ipynb +1433 -0
  156. data/spec/dummy_nanoc/Gemfile +12 -0
  157. data/spec/dummy_nanoc/README.md +40 -0
  158. data/spec/dummy_nanoc/Rules +90 -0
  159. data/spec/dummy_nanoc/content/googlecharts/index.html +29 -0
  160. data/spec/dummy_nanoc/content/highcharts/index.html +42 -0
  161. data/spec/dummy_nanoc/content/index.html +26 -0
  162. data/spec/dummy_nanoc/content/nyaplot/index.html +9 -0
  163. data/spec/dummy_nanoc/content/stylesheet.css +101 -0
  164. data/spec/dummy_nanoc/layouts/default.html +31 -0
  165. data/spec/dummy_nanoc/layouts/googlecharts_layout.html +30 -0
  166. data/spec/dummy_nanoc/layouts/highcharts_layout.html +30 -0
  167. data/spec/dummy_nanoc/layouts/nyaplot_layout.html +30 -0
  168. data/spec/dummy_nanoc/lib/default.rb +300 -0
  169. data/spec/dummy_nanoc/nanoc.yaml +90 -0
  170. data/spec/dummy_nanoc/output/googlecharts/index.html +390 -0
  171. data/spec/dummy_nanoc/output/highcharts/index.html +1153 -0
  172. data/spec/dummy_nanoc/output/index.html +87 -0
  173. data/spec/dummy_nanoc/output/nyaplot/index.html +135 -0
  174. data/spec/dummy_nanoc/output/stylesheet.css +101 -0
  175. data/spec/dummy_nanoc/tmp/nanoc/1029d67644815/checksums +0 -0
  176. data/spec/dummy_nanoc/tmp/nanoc/1029d67644815/compiled_content +2229 -0
  177. data/spec/dummy_nanoc/tmp/nanoc/1029d67644815/dependencies +5 -0
  178. data/spec/dummy_nanoc/tmp/nanoc/1029d67644815/outdatedness +0 -0
  179. data/spec/dummy_nanoc/tmp/nanoc/1029d67644815/rule_memory +0 -0
  180. data/spec/dummy_sinatra/Gemfile +9 -0
  181. data/spec/dummy_sinatra/README.md +15 -0
  182. data/spec/dummy_sinatra/app.rb +143 -0
  183. data/spec/dummy_sinatra/config.ru +0 -0
  184. data/spec/dummy_sinatra/views/googlecharts.erb +31 -0
  185. data/spec/dummy_sinatra/views/googlecharts_layout.erb +12 -0
  186. data/spec/dummy_sinatra/views/highcharts.erb +15 -0
  187. data/spec/dummy_sinatra/views/highcharts_layout.erb +12 -0
  188. data/spec/dummy_sinatra/views/index.erb +2 -0
  189. data/spec/dummy_sinatra/views/layout.erb +26 -0
  190. data/spec/dummy_sinatra/views/nyaplot.erb +13 -0
  191. data/spec/dummy_sinatra/views/nyaplot_layout.erb +14 -0
  192. data/spec/plot_spec.rb +86 -0
  193. data/spec/spec_helper.rb +11 -0
  194. metadata +568 -0
@@ -0,0 +1,62 @@
1
+
2
+ require_relative 'nyaplot/iruby_notebook'
3
+ require 'daru'
4
+ require 'nyaplot'
5
+
6
+ module Daru
7
+ module View
8
+ module Adapter
9
+ module NyaplotAdapter
10
+ extend self # rubocop:disable Style/ModuleFunction
11
+ def init(data, options)
12
+ data_new = guess_data(data)
13
+ data_new.plot(options)
14
+ end
15
+
16
+ def export_html_file(plot, path)
17
+ plot.export_html path
18
+ end
19
+
20
+ def show_in_iruby(plot)
21
+ plot.show
22
+ end
23
+
24
+ def init_script
25
+ Nyaplot.init_script
26
+ end
27
+
28
+ def generate_body(plot)
29
+ plot.to_iruby[1]
30
+ end
31
+
32
+ def init_iruby
33
+ Nyaplot.init_iruby
34
+ end
35
+
36
+ private
37
+
38
+ def multi_dimension_check(arr)
39
+ arr.all? { |a| a.class==Array }
40
+ end
41
+
42
+ def guess_data(data_set)
43
+ case
44
+ when [Daru::DataFrame, Daru::Vector].include?(data_set.class)
45
+ data_set
46
+ when data_set.is_a?(Array)
47
+ if data_set.empty?
48
+ Daru::Vector.new([])
49
+ else
50
+ multi_dimension_check(data_set)? Daru::DataFrame.new(data_set) : Daru::Vector.new(data_set)
51
+ end
52
+ else
53
+ raise ArgumentError, "For Nyaplot Library, data must be in
54
+ Daru::Vector or Daru::DataFrame.\n You can change the plotting
55
+ library using the code : \n
56
+ ` Daru::View.plotting_library = :highcharts` or other library."
57
+ end
58
+ end # def guess_data end
59
+ end
60
+ end # Adapter end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ module Nyaplot
2
+ # dependent JS to include in head tag of the web application
3
+ def self.init_script
4
+ init = Nyaplot.generate_init_code
5
+ path = File.expand_path('../../../templates/nyaplot/init_script.erb', __FILE__)
6
+ template = File.read(path)
7
+ ERB.new(template).result(binding)
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module Nyaplot
2
+ # generate initializing code
3
+ def self.generate_init_code_offline(dependent_js)
4
+ js_dir = File.expand_path('../../js/nyaplot_js', __FILE__)
5
+ path = File.expand_path('../../../templates/nyaplot/init.inline.js.erb', __FILE__)
6
+ template = File.read(path)
7
+ ERB.new(template).result(binding)
8
+ end
9
+
10
+ # generate initializing code
11
+ def self.generate_init_code_online
12
+ # TODO : better than present Nyaplot.generate_init_code
13
+ end
14
+
15
+ # Enable to show plots on IRuby notebook
16
+ def self.init_iruby(*)
17
+ #
18
+ # dependent_js=['d3.min.js', 'd3-downloadable.js', 'nyaplot.js']
19
+ js = generate_init_code
20
+ IRuby.display(IRuby.javascript(js))
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'daru/view/plot'
2
+ require 'daru/view/table'
3
+
4
+ module Daru
5
+ module View
6
+ module Rails
7
+ module ViewHelper
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ if respond_to?(:helper_method)
12
+ helper_method 'daru_chart', 'daru_table'
13
+ end
14
+ end
15
+
16
+ def daru_chart(data=[], options={})
17
+ Daru::View::Plot.new(data, options).div.html_safe
18
+ end
19
+
20
+ def daru_table(data=[], options={})
21
+ Daru::View::Table.new(data, options).div.html_safe
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'helpers/view_helper'
2
+
3
+ module Daru
4
+ module View
5
+ module Rails
6
+ class Railtie < ::Rails::Railtie
7
+ initializer 'daru/view' do
8
+ ActiveSupport.on_load(:action_controller) do
9
+ include Daru::View::Rails::ViewHelper
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,106 @@
1
+
2
+ module Daru
3
+ module View
4
+ class Plot
5
+ attr_reader :chart, :data, :options
6
+ attr_accessor :adapter
7
+ class << self
8
+ # class method
9
+ #
10
+ # @example
11
+ #
12
+ # Daru::View::Plot.adapter = :googlecharts
13
+ #
14
+ # Plotting libraries are nyaplot, highcharts, googlecharts
15
+ def adapter=(adapter)
16
+ require "daru/view/adapters/#{adapter}"
17
+ # rubocop:disable Style/ClassVars
18
+ @@adapter = Daru::View::Adapter.const_get(
19
+ adapter.to_s.capitalize + 'Adapter'
20
+ )
21
+ end
22
+ end
23
+
24
+ # @example
25
+ #
26
+ # df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]})
27
+ # Daru::View::Plot.new df, type: :bar, x: :a, y: :b, adapter: :nyaplot
28
+ #
29
+ # Set the new adapter(plotting library) ,for example highcharts:
30
+ #
31
+ # Daru::View.plotting_library = :highcharts
32
+ #
33
+ # To use a particular apdater in certain plot object(s), then user
34
+ # must pass the adapter in `options` hash. e.g. `adapter: :highcharts`
35
+ #
36
+ def initialize(data=[], options={}, &block)
37
+ # TODO: &block is not used, right now.
38
+ @data = data
39
+ @options = options
40
+ self.adapter = options.delete(:adapter) unless options[:adapter].nil?
41
+ @chart = plot_data(data, options, &block)
42
+ end
43
+
44
+ # instance method
45
+ def adapter=(adapter)
46
+ require "daru/view/adapters/#{adapter}"
47
+ @adapter = Daru::View::Adapter.const_get(
48
+ adapter.to_s.capitalize + 'Adapter'
49
+ )
50
+ end
51
+
52
+ # display in IRuby notebook
53
+ def show_in_iruby
54
+ @adapter.show_in_iruby @chart
55
+ end
56
+
57
+ # dependent js file, to include in head tag using the plot object.
58
+ # @example:
59
+ # plot_obj.init_script
60
+ #
61
+ # Note :
62
+ # User can directly put the dependent script file into the head tag
63
+ # using `Daru::View.dependent_script(:highcharts), by default it loads
64
+ # Nyaplot JS files.
65
+ #
66
+ def init_script
67
+ @adapter.init_script
68
+ end
69
+
70
+ # generat html code, to include in body tag
71
+ def div
72
+ @adapter.generate_body(@chart)
73
+ end
74
+
75
+ # generat html file
76
+ def export_html_file(path='./plot.html')
77
+ @adapter.export_html_file(@chart, path)
78
+ end
79
+
80
+ # load the corresponding JS files in IRuby notebook.
81
+ # This is done automatically when plotting library is set using
82
+ # Daru::View.plotting_library = :new_library
83
+ def init_iruby
84
+ @adapter.init_iruby
85
+ end
86
+
87
+ def add_series(opts={})
88
+ case adapter
89
+ when Daru::View::Adapter::HighchartsAdapter
90
+ @chart = @adapter.add_series(@chart, opts)
91
+ else
92
+ raise("Method `add-series` is not valid for #{@adapter}.to_s.capitalize library.")
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def plot_data(data, options)
99
+ # class variable @@aapter is used in instance variable @adapter.
100
+ # so in each object `adapter` variable can be accessed.
101
+ @adapter ||= @@adapter
102
+ @adapter.init(data, options)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,99 @@
1
+
2
+ module Daru
3
+ module View
4
+ class Table
5
+ attr_reader :table, :data, :options
6
+ attr_accessor :adapter
7
+ class << self
8
+ # class method
9
+ #
10
+ # @example
11
+ #
12
+ # Daru::View::Table.adapter = :new_library
13
+ def adapter=(adapter)
14
+ # The library wrapper method for generating table is in the
15
+ # same folder as the plotting library. Since google chart can be
16
+ # used for plotting charts as well as table.
17
+ #
18
+ require "daru/view/adapters/#{adapter}"
19
+ # rubocop:disable Style/ClassVars
20
+ @@adapter = Daru::View::Adapter.const_get(
21
+ adapter.to_s.capitalize + 'Adapter'
22
+ )
23
+ end
24
+ end
25
+
26
+ # TODO: modify the examples
27
+ # @example
28
+ #
29
+ # df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]})
30
+ # Daru::View::Plot.new df, type: :bar, x: :a, y: :b
31
+ #
32
+ # Set the new adapter(plotting library) ,for example highcharts:
33
+ #
34
+ # Daru::View.plotting_library = :highcharts
35
+ #
36
+ # To use a particular apdater in certain plot object(s), then user
37
+ # must pass the adapter in `options` hash. e.g. `adapter: :highcharts`
38
+ #
39
+ def initialize(data=[], options={})
40
+ @data = data
41
+ @options = options
42
+ self.adapter = options.delete(:adapter) unless options[:adapter].nil?
43
+ @table = table_data(data, options)
44
+ end
45
+
46
+ # instance method
47
+ def adapter=(adapter)
48
+ require "daru/view/adapters/#{adapter}"
49
+ @adapter = Daru::View::Adapter.const_get(
50
+ adapter.to_s.capitalize + 'Adapter'
51
+ )
52
+ end
53
+
54
+ # display in IRuby notebook
55
+ def show_in_iruby
56
+ @adapter.show_in_iruby @table
57
+ end
58
+
59
+ # dependent js file, to include in head tag using the plot object.
60
+ # @example:
61
+ # plot_obj.init_script
62
+ #
63
+ # Note :
64
+ # User can directly put the dependent script file into the head tag
65
+ # using `Daru::View.dependent_script(:highcharts), by default it loads
66
+ # Nyaplot JS files.
67
+ #
68
+ def init_script
69
+ @adapter.init_script
70
+ end
71
+
72
+ # generate html code, to include in body tag
73
+ def div
74
+ @adapter.generate_body(@table)
75
+ end
76
+
77
+ # generat html file
78
+ def export_html_file(path='./plot.html')
79
+ @adapter.export_html_file(@table, path)
80
+ end
81
+
82
+ # load the corresponding JS files in IRuby notebook.
83
+ # This is done automatically when plotting library is set using
84
+ # Daru::View.plotting_library = :new_library
85
+ def init_iruby
86
+ @adapter.init_iruby
87
+ end
88
+
89
+ private
90
+
91
+ def table_data(data, options)
92
+ # class variable @@aapter is used in instance variable @adapter.
93
+ # so in each object `adapter` variable can be accessed.
94
+ @adapter ||= @@adapter
95
+ @adapter.init_table(data, options)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,4 @@
1
+ <div id='<%= id %>'></div>
2
+ <script>
3
+ <%= chart_script %>
4
+ </script>
@@ -0,0 +1,16 @@
1
+ <%
2
+
3
+ dependent_js.each{ |dep_js|
4
+ contents = IO.read(File.join(js_dir,dep_js), mode: 'r:UTF-8')
5
+ %>
6
+ /* BEGIN <%= dep_js %> */
7
+
8
+ <%= contents %>
9
+ /* END <%= dep_js %>*/
10
+ <% } %>
11
+
12
+ var event = document.createEvent("HTMLEvents");
13
+ event.initEvent("load_google_charts", false, false);
14
+ window.dispatchEvent(event);
15
+ console.log("Finish loading google charts js files");
16
+
@@ -0,0 +1,16 @@
1
+ <%
2
+
3
+ dependent_js.each{ |dep_js|
4
+ contents = IO.read(File.join(js_dir,dep_js), mode: 'r:UTF-8')
5
+ %>
6
+ /* BEGIN <%= dep_js %> */
7
+
8
+ <%= contents %>
9
+ /* END <%= dep_js %>*/
10
+ <% } %>
11
+
12
+ var event = document.createEvent("HTMLEvents");
13
+ event.initEvent("load_highcharts", false, false);
14
+ window.dispatchEvent(event);
15
+ console.log("Finish loading highchartsjs");
16
+
@@ -0,0 +1,7 @@
1
+ <html lang='en'>
2
+ <head>
3
+ <title>Chart</title>
4
+ <%= initial_script %>
5
+ </head>
6
+ <body><%= chart_div %></body>
7
+ </html>
@@ -0,0 +1,16 @@
1
+ <%
2
+
3
+ dependent_js.each{ |dep_js|
4
+ contents = IO.read(File.join(js_dir,dep_js), mode: 'r:UTF-8')
5
+ %>
6
+ /* BEGIN <%= dep_js %> */
7
+
8
+ <%= contents %>
9
+ /* END <%= dep_js %>*/
10
+ <% } %>
11
+
12
+ var event = document.createEvent("HTMLEvents");
13
+ event.initEvent("load_nyaplot", false, false);
14
+ window.dispatchEvent(event);
15
+ console.log("Finish loading nyaplot.js");
16
+
@@ -0,0 +1,3 @@
1
+ <!-- TODO : remove the link and use downloaded js -->
2
+ <script src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js"></script>
3
+ <script><%= init %></script>
@@ -0,0 +1,5 @@
1
+ module Daru
2
+ module View
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
data/lib/tasks/.keep ADDED
File without changes
@@ -0,0 +1,25 @@
1
+ def say(msg, &block)
2
+ print "#{msg}..."
3
+
4
+ if block_given?
5
+ yield
6
+ puts " Done."
7
+ end
8
+ end
9
+
10
+ namespace :googlecharts do
11
+ desc "Update google charts javascript dependent files, from latest Builds on google developers website"
12
+ task :update => [:jsapi, :loader]
13
+ task :jsapi do
14
+ say "Grabbing Core from google jsapi codebase..." do
15
+ sh "curl -# https://www.google.com/jsapi -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/google_visualr.js"
16
+ end
17
+ end
18
+
19
+ task :loader do
20
+ say "Grabbing loader.js from the google website..." do
21
+ sh "curl -# https://www.gstatic.com/charts/loader.js -L --compressed -o lib/daru/view/adapters/js/googlecharts_js/loader.js"
22
+ end
23
+ end
24
+
25
+ end