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,86 @@
1
+ require 'securerandom'
2
+ require 'google_visualr'
3
+
4
+ module GoogleVisualr
5
+ class DataTable
6
+ # options will enable us to give some styling for table.
7
+ # E.g. pagination, row numbers, etc
8
+ attr_accessor :options
9
+
10
+ # included to use `js_parameters` method
11
+ include GoogleVisualr::ParamHelpers
12
+
13
+ # overiding the current initialze method (of the google_visualr).
14
+ # This might be not a good idea. But right now I need these lines in it :
15
+ # ` unless options[:cols].nil?` , `unless options[:rows].nil?` and
16
+ # `@options = options`
17
+ # Few lines is changed, to fix rubocop error.
18
+ def initialize(options={})
19
+ @cols = []
20
+ @rows = []
21
+ @options = options
22
+ return if options.empty?
23
+
24
+ new_columns(options[:cols]) unless options[:cols].nil?
25
+
26
+ return if options[:rows].nil?
27
+ rows = options[:rows]
28
+ rows.each do |row|
29
+ add_row(row[:c])
30
+ end
31
+ end
32
+
33
+ # Generates JavaScript and renders the Google Chart DataTable in the
34
+ # final HTML output.
35
+ #
36
+ # Parameters:
37
+ # *div_id [Required] The ID of the DIV element that the Google Chart DataTable should be rendered in.
38
+ def to_js_full_script(element_id=SecureRandom.uuid)
39
+ js = ''
40
+ js << '\n<script type=\'text/javascript\'>'
41
+ js << load_js(element_id)
42
+ js << draw_js(element_id)
43
+ js << '\n</script>'
44
+ js
45
+ end
46
+
47
+ def chart_function_name(element_id)
48
+ "draw_#{element_id.tr('-', '_')}"
49
+ end
50
+
51
+ def google_table_version
52
+ '1.0'.freeze
53
+ end
54
+
55
+ def package_name
56
+ 'table'
57
+ end
58
+
59
+ # Generates JavaScript for loading the appropriate Google Visualization package, with callback to render chart.
60
+ #
61
+ # Parameters:
62
+ # *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
63
+ def load_js(element_id)
64
+ js = ''
65
+ js << "\n google.load('visualization', #{google_table_version}, "
66
+ js << "\n {packages: ['#{package_name}'], callback:"
67
+ js << "\n #{chart_function_name(element_id)}});"
68
+ js
69
+ end
70
+
71
+ # Generates JavaScript function for rendering the chart.
72
+ #
73
+ # Parameters:
74
+ # *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
75
+ def draw_js(element_id)
76
+ js = ''
77
+ js << "\n function #{chart_function_name(element_id)}() {"
78
+ js << "\n #{to_js}"
79
+ js << "\n var table = new google.visualization.Table("
80
+ js << "\n document.getElementById('#{element_id}'));"
81
+ js << "\n table.draw(data_table, #{js_parameters(@options)}); "
82
+ js << "\n };"
83
+ js
84
+ end
85
+ end # class Datatable end
86
+ end
@@ -0,0 +1,54 @@
1
+ require 'securerandom'
2
+ require 'erb'
3
+ require_relative 'data_table_iruby'
4
+
5
+ module GoogleVisualr
6
+ def self.init_script(
7
+ dependent_js=['google_visualr.js', 'loader.js']
8
+ )
9
+ js = ''
10
+ js << "\n<script type='text/javascript'>"
11
+ js << GoogleVisualr.generate_init_code(dependent_js)
12
+ js << "\n</script>"
13
+ js
14
+ end
15
+
16
+ module Display
17
+ def show_script(dom=SecureRandom.uuid, options={})
18
+ script_tag = options.fetch(:script_tag) { true }
19
+ if script_tag
20
+ # if it is data table
21
+ if is_a?(GoogleVisualr::DataTable)
22
+ to_js_full_script(dom)
23
+ else
24
+ to_js(dom)
25
+ end
26
+ else
27
+ html = ''
28
+ html << load_js(dom)
29
+ html << draw_js(dom)
30
+ html
31
+ end
32
+ end
33
+
34
+ def to_html(id=nil, options={})
35
+ path = File.expand_path('../../../templates/googlecharts/chart_div.erb', __FILE__)
36
+ template = File.read(path)
37
+ id ||= SecureRandom.uuid
38
+ chart_script = show_script(id, script_tag: false)
39
+ ERB.new(template).result(binding)
40
+ end
41
+
42
+ def show_in_iruby(dom=SecureRandom.uuid)
43
+ IRuby.html(to_html(dom))
44
+ end
45
+ end
46
+
47
+ class DataTable
48
+ include Display
49
+ end
50
+
51
+ class BaseChart
52
+ include Display
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ module GoogleVisualr
2
+ # generate initializing code
3
+ def self.generate_init_code(dependent_js)
4
+ js_dir = File.expand_path('../../js/googlecharts_js', __FILE__)
5
+ path = File.expand_path('../../../templates/googlecharts/init.inline.js.erb', __FILE__)
6
+ template = File.read(path)
7
+ ERB.new(template).result(binding)
8
+ end
9
+
10
+ # Enable to show plots on IRuby notebook
11
+ def self.init_iruby(dependent_js=['google_visualr.js', 'loader.js'])
12
+ js = generate_init_code(dependent_js)
13
+ IRuby.display(IRuby.javascript(js))
14
+ end
15
+ end
@@ -0,0 +1,114 @@
1
+ require 'lazy_high_charts'
2
+ require_relative 'highcharts/iruby_notebook'
3
+ require_relative 'highcharts/display'
4
+ require_relative 'highcharts/core_ext/string'
5
+ require 'daru'
6
+
7
+ module Daru
8
+ module View
9
+ module Adapter
10
+ module HighchartsAdapter
11
+ extend self # rubocop:disable Style/ModuleFunction
12
+
13
+ # Read : https://www.highcharts.com/docs/chart-concepts to understand
14
+ # the highcharts option concept.
15
+ #
16
+ # TODO : this docs must be improved
17
+ #
18
+ # @param [Hash] options the options to create a chart with.
19
+ # @option opts [String] :title The chart title
20
+ # @option opts [String] :subtitle The chart subtitle
21
+ # @option opts [String/Symbol] :type The chart type
22
+ # @option opts [Daru::Vector / Array] :x X Axis data
23
+ # @option opts [Daru::Vector / Array] :y Y Axis data
24
+ # @option opts [Hash] :chart The chart options(there are many
25
+ # options in chart Hash, same as LazyHighCahrts or HighCharts)
26
+ # @option opts [Hash] :plotOptions The plot options, how the plot
27
+ # type is configured
28
+ #
29
+ # @param [Array/Daru::DataFrame/Daru::Vector] data
30
+ #
31
+ def init(data=[], options={})
32
+ data_new = guess_data(data)
33
+ # TODO : for multiple series need some modification
34
+ # Alternate way is using `add_series` method.
35
+ #
36
+ # There are many options present in Highcharts so it is better to use
37
+ # directly all the options. That means Daru::View::Plot will
38
+ # behave same as LazyHighCharts when `data` is an Array and
39
+ # `options` are passed.
40
+ #
41
+ @chart = LazyHighCharts::HighChart.new do |f|
42
+ # all the options present in `options` and about the
43
+ # series (means name, type, data) used in f.series(..)
44
+ f.options = options.empty? ? LazyHighCharts::HighChart.new.defaults_options : options
45
+
46
+ series_type = options[:type] unless options[:type].nil?
47
+ series_name = options[:name] unless options[:name].nil?
48
+ f.series(type: series_type, name: series_name, data: data_new)
49
+ end
50
+ @chart
51
+ end
52
+
53
+ def init_script
54
+ LazyHighCharts.init_script
55
+ end
56
+
57
+ def generate_body(plot)
58
+ plot.to_html
59
+ end
60
+
61
+ def export_html_file(plot, path='./plot.html')
62
+ path = File.expand_path(path, Dir.pwd)
63
+ str = generate_html(plot)
64
+ File.write(path, str)
65
+ end
66
+
67
+ def show_in_iruby(plot)
68
+ plot.show_in_iruby
69
+ end
70
+
71
+ def generate_html(plot)
72
+ path = File.expand_path('../../templates/highcharts/static_html.erb', __FILE__)
73
+ template = File.read(path)
74
+ initial_script = init_script
75
+ chart_div = generate_body(plots)
76
+ ERB.new(template).result(binding)
77
+ end
78
+
79
+ def init_iruby
80
+ LazyHighCharts.init_iruby
81
+ end
82
+
83
+ # Generally, in opts Hash, :name, :type, :data , :center=> [X, Y],
84
+ # :size=> Integer, :showInLegend=> Bool, etc may present.
85
+ def add_series(plot, opts={})
86
+ plot.series(opts)
87
+ plot
88
+ end
89
+
90
+ private
91
+
92
+ def guess_data(data_set)
93
+ case
94
+ when data_set.is_a?(Daru::DataFrame)
95
+ # TODO : Currently I didn't find use case for multi index.
96
+ return ArgumentError unless data_set.index.is_a?(Daru::Index)
97
+ index_val = data_set.index.to_a
98
+ data_set.access_row_tuples_by_indexs(*index_val)
99
+ when data_set.is_a?(Daru::Vector)
100
+ data_set.to_a
101
+ when data_set.is_a?(Array)
102
+ data_set
103
+ when data_set.is_a?(String)
104
+ # it can be `id` of html table
105
+ data_set
106
+ else
107
+ # TODO: error msg
108
+ raise ArgumentError
109
+ end
110
+ end
111
+ end # HighchartsAdapter end
112
+ end # Adapter end
113
+ end
114
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ def js_code(true_or_false=true)
3
+ @_lazy_high_charts_js_code = true_or_false
4
+ self
5
+ end
6
+
7
+ def js_code?
8
+ @_lazy_high_charts_js_code || false
9
+ end
10
+ end
@@ -0,0 +1,58 @@
1
+ require_relative 'layout_helper_iruby'
2
+ require_relative 'iruby_notebook'
3
+
4
+ module LazyHighCharts
5
+ def self.init_script(
6
+ dependent_js=['highstock.js', 'modules/exporting.js',
7
+ 'highcharts-3d.js', 'modules/data.js']
8
+ )
9
+ # Highstock is based on Highcharts, meaning it has all the core
10
+ # functionality of Highcharts, plus some additional features. So
11
+ # highstock.js contains highcharts.js .If highstock.js is removed then
12
+ # add highchart.js to make chart script work.
13
+ #
14
+ # Note: Don't reorder the dependent_js elements. It must be loaded in
15
+ # the same sequence. Otherwise some of the JS overlap and doesn't work.
16
+ js = ''
17
+ js << "\n<script type='text/javascript'>"
18
+ js << LazyHighCharts.generate_init_code(dependent_js)
19
+ js << "\n</script>"
20
+ js
21
+ end
22
+
23
+ class HighChart
24
+ # @example
25
+ #
26
+ # To display the html code of the chart, use `to_html`. To see the same
27
+ # in IRuby notebook use `show_in_iruby`.
28
+ # User can also use :
29
+ # `IRuby.html chart.to_html` (or)
30
+ # `IRuby.html chart.to_html.to_s` (or)
31
+ # `IRuby.display chart.to_html, mime: 'text/html'`
32
+ # to get the same chart in IRuby notebook.
33
+ #
34
+ def to_html(placeholder=random_canvas_id)
35
+ chart_hash_must_be_present
36
+ high_chart(placeholder, self)
37
+ end
38
+
39
+ def show_in_iruby(placeholder=random_canvas_id)
40
+ # TODO : placeholder pass, in plot#div
41
+ chart_hash_must_be_present
42
+ IRuby.html high_chart_iruby(placeholder, self)
43
+ end
44
+
45
+ # This method is not needed if `to_html` generates the same code. Here
46
+ # `to_html` generates the code with `onload`, so there is need of
47
+ # `high_chart_iruby` which doesn't use `onload` in chart script.
48
+ def to_html_iruby(placeholder=random_canvas_id)
49
+ # TODO : placeholder pass, in plot#div
50
+ chart_hash_must_be_present
51
+ high_chart_iruby(placeholder, self)
52
+ end
53
+
54
+ def chart_hash_must_be_present
55
+ @options[:chart] ||= {}
56
+ end
57
+ end # class end
58
+ end
@@ -0,0 +1,29 @@
1
+ module LazyHighCharts
2
+ # generate initializing code
3
+ def self.generate_init_code(dependent_js)
4
+ js_dir = File.expand_path('../../js/highcharts_js', __FILE__)
5
+ path = File.expand_path('../../../templates/highcharts/init.inline.js.erb', __FILE__)
6
+ template = File.read(path)
7
+ ERB.new(template).result(binding)
8
+ end
9
+
10
+ # Enable to show plots on IRuby notebook
11
+ def self.init_iruby(
12
+ dependent_js=['highcharts.js', 'modules/exporting.js',
13
+ 'highcharts-3d.js', 'modules/data.js']
14
+ )
15
+ # TODO: include highstock.js for highstock and modules/*.js files for
16
+ # exporting and getting data from various source like csv files etc.
17
+ #
18
+ # Highstock.js includes the highcharts.js, so only one of them required.
19
+ # see: https://www.highcharts.com/errors/16
20
+ #
21
+ # , 'modules/exporting.js' : for the exporting button
22
+ # data.js for getting data as csv or html table.
23
+ # 'highcharts-more.js' : for arearange and some other chart type
24
+ # Note: Don't reorder the dependent_js elements. It must be loaded in
25
+ # the same sequence. Otherwise some of the JS overlap and doesn't work.
26
+ js = generate_init_code(dependent_js)
27
+ IRuby.display(IRuby.javascript(js))
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ module LazyHighCharts
2
+ module LayoutHelper
3
+ def high_chart_iruby(placeholder, object, &block)
4
+ object.html_options[:id] = placeholder
5
+ object.options[:chart][:renderTo] = placeholder
6
+ high_graph_iruby(placeholder, object, &block).concat(content_tag('div', '', object.html_options))
7
+ end
8
+
9
+ def high_graph_iruby(placeholder, object, &block)
10
+ build_html_output_iruby('Chart', placeholder, object, &block)
11
+ end
12
+
13
+ private
14
+
15
+ def build_html_output_iruby(type, placeholder, object, &block)
16
+ core_js =<<-EOJS
17
+ var options = #{options_collection_as_string(object)};
18
+ #{capture(&block) if block_given?}
19
+ window.chart_#{placeholder.underscore} = new Highcharts.#{type}(options);
20
+ EOJS
21
+
22
+ encapsulate_js_iruby core_js
23
+ end
24
+
25
+ # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength
26
+ def encapsulate_js_iruby(core_js)
27
+ js_output =
28
+ if request_is_xhr?
29
+ "#{js_start_iruby} #{core_js} #{js_end_iruby}"
30
+ # Turbolinks.version < 5
31
+ elsif defined?(Turbolinks)
32
+ if request_is_referrer?
33
+ <<-EOJS
34
+ #{js_start_iruby}
35
+ var f = function(){
36
+ document.removeEventListener('page:load', f, true);
37
+ #{core_js}
38
+ };
39
+ document.addEventListener('page:load', f, true);
40
+ #{js_end_iruby}
41
+ EOJS
42
+ elsif request_turbolinks_5_tureferrer?
43
+ <<-EOJS
44
+ #{js_start_iruby}
45
+ document.addEventListener("turbolinks:load", function() {
46
+ #{core_js}
47
+ });
48
+ #{js_end_iruby}
49
+ EOJS
50
+ end
51
+ else
52
+ <<-EOJS
53
+ #{js_start_iruby}
54
+ #{core_js}
55
+ #{js_end_iruby}
56
+ EOJS
57
+ end
58
+
59
+ defined?(raw) ? raw(js_output) : js_output
60
+ end
61
+
62
+ def js_start_iruby
63
+ <<-EOJS
64
+ <script type="text/javascript">
65
+ $(function() {
66
+ EOJS
67
+ end
68
+
69
+ def js_end_iruby
70
+ <<-EOJS
71
+ });
72
+ </script>
73
+ EOJS
74
+ end
75
+ end
76
+ end