From 2441a21e3f8431d1a3b6db5a3d83b5fbabcecef9 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Thu, 5 Jul 2018 13:46:17 +0530 Subject: [PATCH 1/8] Use javascript Array rather than html table in DataTables --- lib/daru/view/adapters/datatables.rb | 54 ++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index 42bab15..b1e9667 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -1,6 +1,7 @@ require 'daru/data_tables' require 'daru' require 'securerandom' +require 'erb' module Daru module View @@ -16,16 +17,21 @@ def init_table(data=[], options={}) # TODO : create data array from the df and vector data. So that # we can directly use the array.(No need to create df or vector and # generate the html table using to_html) - if data.is_a?(Array) - data_name = 'series_data'+ SecureRandom.uuid - data = - if data.all? { |e| e.class==Array } - Daru::DataFrame.rows(data, name: data_name) - else - Daru::Vector.new(data, name: data_name) - end + # if data.is_a?(Array) + # data_name = 'series_data'+ SecureRandom.uuid + # data = + # if data.all? { |e| e.class==Array } + # Daru::DataFrame.rows(data, name: data_name) + # else + # Daru::Vector.new(data, name: data_name) + # end + # end + options[:data] = to_data_array(data) + row_number = 0 + options[:data].each do |array| + array.unshift(row_number) + row_number += 1 end - # options[:data] = data_in_array unless data_in_array.empty? @table = Daru::DataTables::DataTable.new(options) @data = data @table @@ -40,12 +46,12 @@ def generate_body(table) class: 'display', cellspacing: '0', width: '100%', - table_html: @data.to_html_thead + @data.to_html_tbody + table_html: extract_table } - html_options ={ + html_options = { table_options: table_opts } - table.to_html(@data.name, html_options) + table.to_html('series_data'+ SecureRandom.uuid, html_options) end def export_html_file(table, path='./table.html') @@ -73,17 +79,37 @@ def to_data_array(data_set) case when data_set.is_a?(Daru::DataFrame) return ArgumentError unless data_set.index.is_a?(Daru::Index) - data_set.access_row_tuples_by_indexs(*data_set.index.to_a) + rows = data_set.access_row_tuples_by_indexs(*data_set.index.to_a) + convert_to_array_of_array(rows) when data_set.is_a?(Daru::Vector) rows = [] data_set.to_a.each { |a| rows << [a] } rows when data_set.is_a?(Array) - data_set + convert_to_array_of_array(data_set) else raise ArgumentError # TODO: error msg end end + + def convert_to_array_of_array(rows) + if rows.all? { |row| row.class==Array } + rows + else + tuples = [] + rows.each { |row| tuples << [row] } + tuples + end + end + + def extract_table + return @data.to_html_thead unless @data.is_a?(Array) + path = File.expand_path( + '../templates/datatables/thead.erb', __dir__ + ) + template = File.read(path) + ERB.new(template).result(binding) + end end end end From b02a480346ea3a097a90cb94fe572afc7999ff37 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Fri, 6 Jul 2018 02:29:29 +0530 Subject: [PATCH 2/8] Fixed the bug with simultaneous multiple datatables --- lib/daru/view/adapters/datatables.rb | 20 ++++---------------- lib/daru/view/templates/datatables/thead.erb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 lib/daru/view/templates/datatables/thead.erb diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index b1e9667..2383971 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -14,18 +14,6 @@ module DatatablesAdapter # # TODO : this docs must be improved def init_table(data=[], options={}) - # TODO : create data array from the df and vector data. So that - # we can directly use the array.(No need to create df or vector and - # generate the html table using to_html) - # if data.is_a?(Array) - # data_name = 'series_data'+ SecureRandom.uuid - # data = - # if data.all? { |e| e.class==Array } - # Daru::DataFrame.rows(data, name: data_name) - # else - # Daru::Vector.new(data, name: data_name) - # end - # end options[:data] = to_data_array(data) row_number = 0 options[:data].each do |array| @@ -33,7 +21,7 @@ def init_table(data=[], options={}) row_number += 1 end @table = Daru::DataTables::DataTable.new(options) - @data = data + @table.data = data @table end @@ -46,7 +34,7 @@ def generate_body(table) class: 'display', cellspacing: '0', width: '100%', - table_html: extract_table + table_html: extract_table(table) } html_options = { table_options: table_opts @@ -102,8 +90,8 @@ def convert_to_array_of_array(rows) end end - def extract_table - return @data.to_html_thead unless @data.is_a?(Array) + def extract_table(table) + return table.data.to_html_thead unless table.data.is_a?(Array) path = File.expand_path( '../templates/datatables/thead.erb', __dir__ ) diff --git a/lib/daru/view/templates/datatables/thead.erb b/lib/daru/view/templates/datatables/thead.erb new file mode 100644 index 0000000..5d55018 --- /dev/null +++ b/lib/daru/view/templates/datatables/thead.erb @@ -0,0 +1,15 @@ + + + + <% column_number = 0 %> + <% if table.data.sample.is_a?(Array)%> + <% columns = table.data.sample.length %> + <% else %> + <% columns = 2 %> + <% end %> + <% (columns - 1).times do %> + Column: <%=column_number%> + <% column_number += 1 %> + <% end %> + + From d462a3ae194656f1528a3339b22127d3d05066a1 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Sat, 7 Jul 2018 03:24:45 +0530 Subject: [PATCH 3/8] Updated columns in thead.rb --- lib/daru/view/templates/datatables/thead.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/daru/view/templates/datatables/thead.erb b/lib/daru/view/templates/datatables/thead.erb index 5d55018..57b8f44 100644 --- a/lib/daru/view/templates/datatables/thead.erb +++ b/lib/daru/view/templates/datatables/thead.erb @@ -3,11 +3,11 @@ <% column_number = 0 %> <% if table.data.sample.is_a?(Array)%> - <% columns = table.data.sample.length %> + <% columns = table.data.sample.length - 1 %> <% else %> - <% columns = 2 %> + <% columns = 1 %> <% end %> - <% (columns - 1).times do %> + <% columns.times do %> Column: <%=column_number%> <% column_number += 1 %> <% end %> From 8f32a160095e82915cceae350cffc44dbb851641 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Mon, 9 Jul 2018 23:05:30 +0530 Subject: [PATCH 4/8] Added export_html_file method --- lib/daru/view/adapters/datatables.rb | 16 +++++++++--- .../view/templates/datatables/static_html.erb | 9 +++++++ spec/adapters/datatables_spec.rb | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 lib/daru/view/templates/datatables/static_html.erb diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index 2383971..e1952e5 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -39,11 +39,14 @@ def generate_body(table) html_options = { table_options: table_opts } - table.to_html('series_data'+ SecureRandom.uuid, html_options) + table.element_id ||= 'series_data'+ SecureRandom.uuid + table.to_html(table.element_id, html_options) end def export_html_file(table, path='./table.html') - # TODO + path = File.expand_path(path, Dir.pwd) + str = generate_html(table) + File.write(path, str) end def show_in_iruby(table) @@ -51,7 +54,14 @@ def show_in_iruby(table) end def generate_html(table) - # TODO + path = File.expand_path( + '../templates/datatables/static_html.erb', __dir__ + ) + template = File.read(path) + table_script = generate_body(table) + initial_script = init_script + id = table.element_id + ERB.new(template).result(binding) end def init_iruby diff --git a/lib/daru/view/templates/datatables/static_html.erb b/lib/daru/view/templates/datatables/static_html.erb new file mode 100644 index 0000000..f57b900 --- /dev/null +++ b/lib/daru/view/templates/datatables/static_html.erb @@ -0,0 +1,9 @@ + + + DataTable_<%= id %> + <%= initial_script %> + + + <%= table_script %> + + \ No newline at end of file diff --git a/spec/adapters/datatables_spec.rb b/spec/adapters/datatables_spec.rb index 4099ae1..d3d782a 100644 --- a/spec/adapters/datatables_spec.rb +++ b/spec/adapters/datatables_spec.rb @@ -35,4 +35,29 @@ expect(table_array.options).to eq options end end + + describe "#export_html_file" do + it "writes valid html code of the DataTable to the file" do + @plot.export_html_file('./plot.html') + path = File.expand_path('../../plot.html', __dir__) + content = File.read(path) + expect(content).to match(/jquery-latest.min.js/) + expect(content).to match(/jquery.dataTables.js/) + expect(content).to match(/jquery.dataTables.css/) + expect(content).to match(/html/i) + expect(content).to match(/script/i) + expect(content).to match( + /data_array = \[\[0, 1, 15\], \[1, 2, 30\], \[2, 4, 40\]\]/ + ) + expect(content).to match( + /width: 800, height: 720, serverSide: true, ajax:/ + ) + expect(content).to match( + /function \( data, callback, settings \) {/) + expect(content).to match(/out.push\( data_array\[i\] \);/i) + expect(content).to match(/callback\( \{/i) + expect(content).to match(/arr1<\/th>/i) + expect(content).to match(/arr2<\/th>/i) + end + end end From 1dfc43ee71bcaf45e2d1998bc5a34b550051424b Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Thu, 12 Jul 2018 04:20:21 +0530 Subject: [PATCH 5/8] Improved API daru-data_tables --- lib/daru/view/adapters/datatables.rb | 62 +------------------- lib/daru/view/templates/datatables/thead.erb | 15 ----- 2 files changed, 2 insertions(+), 75 deletions(-) delete mode 100644 lib/daru/view/templates/datatables/thead.erb diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index e1952e5..12a1ee0 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -14,14 +14,7 @@ module DatatablesAdapter # # TODO : this docs must be improved def init_table(data=[], options={}) - options[:data] = to_data_array(data) - row_number = 0 - options[:data].each do |array| - array.unshift(row_number) - row_number += 1 - end - @table = Daru::DataTables::DataTable.new(options) - @table.data = data + @table = Daru::DataTables::DataTable.new(data, options) @table end @@ -30,17 +23,7 @@ def init_script end def generate_body(table) - table_opts = { - class: 'display', - cellspacing: '0', - width: '100%', - table_html: extract_table(table) - } - html_options = { - table_options: table_opts - } - table.element_id ||= 'series_data'+ SecureRandom.uuid - table.to_html(table.element_id, html_options) + table.to_html end def export_html_file(table, path='./table.html') @@ -67,47 +50,6 @@ def generate_html(table) def init_iruby Daru::DataTables.init_iruby end - - private - - # DataTables accept the data as Array of array. - # - # TODO : I didn't find use case for multi index. - def to_data_array(data_set) - case - when data_set.is_a?(Daru::DataFrame) - return ArgumentError unless data_set.index.is_a?(Daru::Index) - rows = data_set.access_row_tuples_by_indexs(*data_set.index.to_a) - convert_to_array_of_array(rows) - when data_set.is_a?(Daru::Vector) - rows = [] - data_set.to_a.each { |a| rows << [a] } - rows - when data_set.is_a?(Array) - convert_to_array_of_array(data_set) - else - raise ArgumentError # TODO: error msg - end - end - - def convert_to_array_of_array(rows) - if rows.all? { |row| row.class==Array } - rows - else - tuples = [] - rows.each { |row| tuples << [row] } - tuples - end - end - - def extract_table(table) - return table.data.to_html_thead unless table.data.is_a?(Array) - path = File.expand_path( - '../templates/datatables/thead.erb', __dir__ - ) - template = File.read(path) - ERB.new(template).result(binding) - end end end end diff --git a/lib/daru/view/templates/datatables/thead.erb b/lib/daru/view/templates/datatables/thead.erb deleted file mode 100644 index 57b8f44..0000000 --- a/lib/daru/view/templates/datatables/thead.erb +++ /dev/null @@ -1,15 +0,0 @@ - - - - <% column_number = 0 %> - <% if table.data.sample.is_a?(Array)%> - <% columns = table.data.sample.length - 1 %> - <% else %> - <% columns = 1 %> - <% end %> - <% columns.times do %> - Column: <%=column_number%> - <% column_number += 1 %> - <% end %> - - From 9528c02012e5c5afc5232eabc77bfb51c9df4b26 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Thu, 12 Jul 2018 08:52:16 +0530 Subject: [PATCH 6/8] Added tests for datatables.rb --- .rubocop.yml | 2 +- spec/adapters/datatables_spec.rb | 153 ++++++++++++++++++++++++++----- 2 files changed, 132 insertions(+), 23 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d171474..b96c652 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -16,7 +16,7 @@ AllCops: - 'daru-view.gemspec' - '**/*.rake' DisplayCopNames: true - TargetRubyVersion: 2.1 + TargetRubyVersion: 2.2 # Preferred codebase style --------------------------------------------- Layout/ExtraSpacing: diff --git a/spec/adapters/datatables_spec.rb b/spec/adapters/datatables_spec.rb index d3d782a..47b1904 100644 --- a/spec/adapters/datatables_spec.rb +++ b/spec/adapters/datatables_spec.rb @@ -3,7 +3,7 @@ describe Daru::View::Table, 'table using daru-data_tables' do before { Daru::View.table_library = :datatables } before(:each) do - @data_vec1 = Daru::Vector.new([1 ,2, 4]) + @data_vec1 = Daru::Vector.new([1 ,2, 4], name: :a) @data_vec2 = Daru::Vector.new([15 ,30, 40]) @data_df = Daru::DataFrame.new(arr1: @data_vec1, arr2: @data_vec2) @options = {width: 800, height: 720} @@ -15,6 +15,7 @@ let(:data_array) {[[1, 15], [2, 30], [4, 40]]} let(:table_string_array) { Daru::View::Table.new(string_array, options) } let(:table_array) { Daru::View::Table.new(data_array, options) } + let(:table_dv) { Daru::View::Table.new(@data_vec1, @options) } describe "initialization Tables" do it "Table class must be Daru::DataTables::DataTable" do @@ -36,28 +37,136 @@ end end + describe "#init_script" do + subject(:js) { table_array.init_script } + it "loads correct dependent js and css files" do + expect(js).to match(/jquery-latest.min.js/) + expect(js).to match(/jquery.dataTables.js/) + expect(js).to match(/jquery.dataTables.css/) + end + end + + describe "#generate_body" do + context 'when data is set as Daru::DataFrame' do + subject(:js) { @plot.adapter.generate_body(@plot.table) } + it 'generates valid script and table' do + expect(js).to match(/DataTable/) + expect(js).to match( + /width: 800, height: 720, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ + ) + expect(js).to match(/<\/th>/i) + expect(js).to match(/arr1<\/th>/i) + expect(js).to match(/arr2<\/th>/i) + end + end + + # In Daru, thead is generated only when the name of the vector is provided + context 'when data is set as Daru::Vector' do + subject(:js) { table_dv.adapter.generate_body(table_dv.table) } + it 'generates valid script and table' do + expect(js).to match(/DataTable/) + expect(js).to match( + /width: 800, height: 720, data: \[\[0,1\],\[1,2\],\[2,4\]\]/ + ) + expect(js).to match(/ <\/th>/i) + expect(js).to match(/a<\/th>/i) + end + end + + context 'when data is set as Array' do + subject(:js) { + table_string_array.adapter.generate_body(table_string_array.table) + } + it 'generates valid script and table' do + expect(js).to match(/DataTable/) + expect(js).to match( + /width: 800, height: 720, data: \[\[0,"daru"\],\[1,"view"\]\]/ + ) + expect(js).to match(/<\/th>/i) + expect(js).to match(/Column: 0<\/th>/i) + end + end + + context 'when data is set as Array of Arrays' do + subject(:js) { + table_string_array.adapter.generate_body(table_string_array.table) + } + it 'generates valid script and table' do + expect(js).to match(/DataTable/) + expect(js).to match( + /width: 800, height: 720, data: \[\[0,"daru"\],\[1,"view"\]\]/ + ) + expect(js).to match(/<\/th>/i) + expect(js).to match(/Column: 0<\/th>/i) + end + end + end + describe "#export_html_file" do - it "writes valid html code of the DataTable to the file" do - @plot.export_html_file('./plot.html') - path = File.expand_path('../../plot.html', __dir__) - content = File.read(path) - expect(content).to match(/jquery-latest.min.js/) - expect(content).to match(/jquery.dataTables.js/) - expect(content).to match(/jquery.dataTables.css/) - expect(content).to match(/html/i) - expect(content).to match(/script/i) - expect(content).to match( - /data_array = \[\[0, 1, 15\], \[1, 2, 30\], \[2, 4, 40\]\]/ - ) - expect(content).to match( - /width: 800, height: 720, serverSide: true, ajax:/ - ) - expect(content).to match( - /function \( data, callback, settings \) {/) - expect(content).to match(/out.push\( data_array\[i\] \);/i) - expect(content).to match(/callback\( \{/i) - expect(content).to match(/arr1<\/th>/i) - expect(content).to match(/arr2<\/th>/i) + context 'when large dataset is used' do + before do + array_large = [] + for i in 0..50000 + array_large << i + end + table_array_large = Daru::View::Table.new(array_large, options) + table_array_large.export_html_file('./plot.html') + @path = File.expand_path('../../plot.html', __dir__) + end + subject(:content) { File.read(@path) } + it "writes correct dependent js and css files" do + expect(content).to match(/jquery-latest.min.js/) + expect(content).to match(/jquery.dataTables.js/) + expect(content).to match(/jquery.dataTables.css/) + end + it "writes a script" do + expect(content).to match(/html/i) + expect(content).to match(/script/i) + end + it "writes server side html code of the DataTable to the file" do + expect(content).to match( + /data_array = \[\[0, 0\], \[1, 1\], \[2, 2\]/ + ) + expect(content).to match( + /width: 800, height: 720, serverSide: true, ajax:/ + ) + expect(content).to match( + /function \( data, callback, settings \) {/) + expect(content).to match(/out.push\( data_array\[i\] \);/i) + expect(content).to match(/callback\( \{/i) + end + it "generates a table" do + expect(content).to match(/<\/th>/i) + expect(content).to match(/Column: 0<\/th>/i) + end + end + + context 'when small dataset is used' do + before do + @plot.export_html_file('./plot.html') + @path = File.expand_path('../../plot.html', __dir__) + end + subject(:content) { File.read(@path) } + it "writes correct dependent js and css files" do + expect(content).to match(/jquery-latest.min.js/) + expect(content).to match(/jquery.dataTables.js/) + expect(content).to match(/jquery.dataTables.css/) + end + it "writes a script" do + expect(content).to match(/html/i) + expect(content).to match(/script/i) + end + it "writes client side html code of the DataTable to the file" do + expect(content).to match(/DataTable/) + expect(content).to match( + /width: 800, height: 720, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ + ) + end + it "generates a table" do + expect(content).to match(/<\/th>/i) + expect(content).to match(/arr1<\/th>/i) + expect(content).to match(/arr2<\/th>/i) + end end end end From 035f9fd4d51e219f1f8981eb00c42dea9a0ded97 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Thu, 12 Jul 2018 10:27:54 +0530 Subject: [PATCH 7/8] Updated options in specs --- spec/adapters/datatables_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/adapters/datatables_spec.rb b/spec/adapters/datatables_spec.rb index 47b1904..3aff3a5 100644 --- a/spec/adapters/datatables_spec.rb +++ b/spec/adapters/datatables_spec.rb @@ -6,11 +6,11 @@ @data_vec1 = Daru::Vector.new([1 ,2, 4], name: :a) @data_vec2 = Daru::Vector.new([15 ,30, 40]) @data_df = Daru::DataFrame.new(arr1: @data_vec1, arr2: @data_vec2) - @options = {width: 800, height: 720} + @options = {scrollX: true} @plot = Daru::View::Table.new(@data_df, @options) end - let(:options) {{width: 800, height: 720}} + let(:options) {{scrollX: true}} let(:string_array) {["daru", "view"]} let(:data_array) {[[1, 15], [2, 30], [4, 40]]} let(:table_string_array) { Daru::View::Table.new(string_array, options) } @@ -52,7 +52,7 @@ it 'generates valid script and table' do expect(js).to match(/DataTable/) expect(js).to match( - /width: 800, height: 720, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ + /scrollX: true, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ ) expect(js).to match(/<\/th>/i) expect(js).to match(/arr1<\/th>/i) @@ -66,7 +66,7 @@ it 'generates valid script and table' do expect(js).to match(/DataTable/) expect(js).to match( - /width: 800, height: 720, data: \[\[0,1\],\[1,2\],\[2,4\]\]/ + /scrollX: true, data: \[\[0,1\],\[1,2\],\[2,4\]\]/ ) expect(js).to match(/ <\/th>/i) expect(js).to match(/a<\/th>/i) @@ -80,7 +80,7 @@ it 'generates valid script and table' do expect(js).to match(/DataTable/) expect(js).to match( - /width: 800, height: 720, data: \[\[0,"daru"\],\[1,"view"\]\]/ + /scrollX: true, data: \[\[0,"daru"\],\[1,"view"\]\]/ ) expect(js).to match(/<\/th>/i) expect(js).to match(/Column: 0<\/th>/i) @@ -94,7 +94,7 @@ it 'generates valid script and table' do expect(js).to match(/DataTable/) expect(js).to match( - /width: 800, height: 720, data: \[\[0,"daru"\],\[1,"view"\]\]/ + /scrollX: true, data: \[\[0,"daru"\],\[1,"view"\]\]/ ) expect(js).to match(/<\/th>/i) expect(js).to match(/Column: 0<\/th>/i) @@ -128,7 +128,7 @@ /data_array = \[\[0, 0\], \[1, 1\], \[2, 2\]/ ) expect(content).to match( - /width: 800, height: 720, serverSide: true, ajax:/ + /scrollX: true, serverSide: true, ajax:/ ) expect(content).to match( /function \( data, callback, settings \) {/) @@ -159,7 +159,7 @@ it "writes client side html code of the DataTable to the file" do expect(content).to match(/DataTable/) expect(content).to match( - /width: 800, height: 720, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ + /scrollX: true, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/ ) end it "generates a table" do From d523244366a8e1c395c2cf82e851dc362cec53a0 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Sat, 14 Jul 2018 07:01:38 +0530 Subject: [PATCH 8/8] Added docs --- lib/daru/view/adapters/datatables.rb | 71 ++++++++++++++++++- .../view/templates/datatables/static_html.erb | 2 +- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/daru/view/adapters/datatables.rb b/lib/daru/view/adapters/datatables.rb index 12a1ee0..eb7fde6 100644 --- a/lib/daru/view/adapters/datatables.rb +++ b/lib/daru/view/adapters/datatables.rb @@ -11,31 +11,96 @@ module DatatablesAdapter # Read : https://datatables.net/ to understand # the datatables option concept. + # Along with these options, a user can provide an additional option + # html_options[:table_options] to cistomize the generated table + # See the specs of daru-data_tables gem. # - # TODO : this docs must be improved + # @param data [Array, Daru::DataFrame, Daru::Vector] The data provided + # by the user to generate the datatable + # @param options [Hash] Various options provided by the user to + # incorporate in datatable + # @return [Daru::DataTables::DataTable] Returns the datatble object + # + # @example DataTable + # Set Daru::View.table_library = :googlecharts (or set adapter option) + # (Also set Daru::View.dependent_script(:googlecharts) in web + # frameworks in head tag) + # Formulate the data to visualize + # idx = Daru::Index.new ['Year', 'Sales'] + # data_rows = [ + # ['2004', 1000], + # ['2005', 1170], + # ['2006', 660], + # ['2007', 1030] + # ] + # df_sale_exp = Daru::DataFrame.rows(data_rows) + # df_sale_exp.vectors = idx + # + # Set the options required + # options1 = { + # html_options: { + # table_options: { + # table_thead: " + # + # + # C1 + # C2 + # + # ", + # width: '90%' + # } + # }, + # scrollX: true + # } + # options2 = {searching: false} + # + # Draw the Daru::View::Table object. + # table = Daru::View::Table.new(df_sale_exp, options1) + # table2 = Daru::View::Table.new(df_sale_exp, options2) + # table3 = Daru::View::Table.new(df_sale_exp) def init_table(data=[], options={}) @table = Daru::DataTables::DataTable.new(data, options) @table end + # @return [String] returns code of the dependent JS and CSS file(s) def init_script Daru::DataTables.init_script end + # @param table [Daru::DataTables::DataTable] table object to access + # daru-data_table methods + # @return [String] script and table (containg thead only) tags of the + # datatable generated def generate_body(table) table.to_html end + # @param table [Daru::DataTables::DataTable] table object to access + # daru-data_table methods + # @return [void] writes the html code of the datatable to the file + # @example + # table = Daru::View::Table.new(data, options) + # table.export_html_file def export_html_file(table, path='./table.html') path = File.expand_path(path, Dir.pwd) str = generate_html(table) File.write(path, str) end + # @param table [Daru::DataTables::DataTable] table object to access + # daru-data_table methods + # @return [void] shows the datatable in IRuby notebook + # @example + # table = Daru::View::Table.new(data, options) + # table.show_in_iruby def show_in_iruby(table) table.show_in_iruby end + # @param table [Daru::DataTables::DataTable] table object to access + # daru-data_table methods + # @return [String] returns html code of the datatable generated def generate_html(table) path = File.expand_path( '../templates/datatables/static_html.erb', __dir__ @@ -47,6 +112,10 @@ def generate_html(table) ERB.new(template).result(binding) end + # @return [void] loads the dependent JS and CSS files in IRuby notebook + # @example + # table = Daru::View::Table.new(data, options) + # table.init_iruby def init_iruby Daru::DataTables.init_iruby end diff --git a/lib/daru/view/templates/datatables/static_html.erb b/lib/daru/view/templates/datatables/static_html.erb index f57b900..45b84ac 100644 --- a/lib/daru/view/templates/datatables/static_html.erb +++ b/lib/daru/view/templates/datatables/static_html.erb @@ -6,4 +6,4 @@ <%= table_script %> - \ No newline at end of file +