require 'daru/view'
Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions. Install the mechanize gem version ~>2.7.5 for using mechanize functions.
true
# used for creating table and charts
Daru::View.plotting_library = :googlecharts
:googlecharts
df = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
c: [11,22,33,44,55]},
order: [:a, :b, :c],
index: [:one, :two, :three, :four, :five])
Daru::DataFrame(5x3) | |||
---|---|---|---|
a | b | c | |
one | 1 | 11 | 11 |
two | 2 | 12 | 22 |
three | 3 | 13 | 33 |
four | 4 | 14 | 44 |
five | 5 | 15 | 55 |
t1 = Daru::View::Table.new(df)
#<Daru::View::Table:0x007f9c34034eb8 @data=#<Daru::DataFrame(5x3)> a b c one 1 11 11 two 2 12 22 three 3 13 33 four 4 14 44 five 5 15 55, @options={}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c34034da0 @cols=[{:type=>"number", :label=>:a}, {:type=>"number", :label=>:b}, {:type=>"number", :label=>:c}], @rows=[[#<GoogleVisualr::DataTable::Cell:0x007f9c34b0dab0 @v=1, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0da10 @v=11, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d970 @v=11, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b0d830 @v=2, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d7b8 @v=12, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d740 @v=22, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b0d650 @v=3, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d5d8 @v=13, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d560 @v=33, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b0d470 @v=4, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d3f8 @v=14, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d380 @v=44, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b0d290 @v=5, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d218 @v=15, @type="number">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b0d1a0 @v=55, @type="number">]], @options={}>>
t1.table.class
GoogleVisualr::DataTable
t1.table.cols
[{:type=>"number", :label=>:a}, {:type=>"number", :label=>:b}, {:type=>"number", :label=>:c}]
t1.class
Daru::View::Table
t1.show_in_iruby
t1.div
"<div id='e469227b-5cda-4f1f-90ca-61c18877ba4a'></div>\n<script>\n\n google.load('visualization', 1.0, \n {packages: ['table'], callback:\n draw_e469227b_5cda_4f1f_90ca_61c18877ba4a});\n function draw_e469227b_5cda_4f1f_90ca_61c18877ba4a() {\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"number\",\"label\":\"a\"});data_table.addColumn({\"type\":\"number\",\"label\":\"b\"});data_table.addColumn({\"type\":\"number\",\"label\":\"c\"});data_table.addRow([{v: 1}, {v: 11}, {v: 11}]);data_table.addRow([{v: 2}, {v: 12}, {v: 22}]);data_table.addRow([{v: 3}, {v: 13}, {v: 33}]);data_table.addRow([{v: 4}, {v: 14}, {v: 44}]);data_table.addRow([{v: 5}, {v: 15}, {v: 55}]);\n var table = new google.visualization.Table(\n document.getElementById('e469227b-5cda-4f1f-90ca-61c18877ba4a'));\n table.draw(data_table, {}); \n };\n</script>\n"
IRuby.html t1.div
rows = [
['Mike', {v: 10000, f: '$10,000'}, true],
['Jim', {v:8000, f: '$8,000'}, false],
['Alice', {v: 12500, f: '$12,500'}, true],
['Bob', {v: 7000, f: '$7,000'}, true]
]
df2 = Daru::DataFrame.rows(rows)
Daru::DataFrame(4x3) | |||
---|---|---|---|
0 | 1 | 2 | |
0 | Mike | {:v=>10000, :f=>"$10,000"} | true |
1 | Jim | {:v=>8000, :f=>"$8,000"} | false |
2 | Alice | {:v=>12500, :f=>"$12,500"} | true |
3 | Bob | {:v=>7000, :f=>"$7,000"} | true |
t2 = Daru::View::Table.new(df2)
#<Daru::View::Table:0x007f9c3431ea98 @data=#<Daru::DataFrame(4x3)> 0 1 2 0 Mike {:v=>10000 true 1 Jim {:v=>8000, false 2 Alice {:v=>12500 true 3 Bob {:v=>7000, true, @options={}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c3431ea48 @cols=[{:type=>"string", :label=>"0"}, {:type=>"number", :label=>"1"}, {:type=>"boolean", :label=>"2"}], @rows=[[#<GoogleVisualr::DataTable::Cell:0x007f9c34b36938 @v="Mike", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b368c0 @v=10000, @type="number", @f="$10,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34b367f8 @v=true, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b36708 @v="Jim", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b36690 @v=8000, @type="number", @f="$8,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34b36500 @v=false, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b36410 @v="Alice", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b36370 @v=12500, @type="number", @f="$12,500", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34b362d0 @v=true, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c34b361e0 @v="Bob", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c34b36168 @v=7000, @type="number", @f="$7,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34b360c8 @v=true, @type="boolean">]], @options={}>>
t2.show_in_iruby
# adding pagination
opts_pagination = {
showRowNumber: true, width: '100%', height: '100%' ,
page: 'enable',
pageSize: 2,
pagingSymbols: {
prev: 'prev',
next: 'next'
},
pagingButtonsConfiguration: 'auto'}
{:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}
t3 = Daru::View::Table.new(df2,opts)
#<Daru::View::Table:0x007f9c3596e410 @data=#<Daru::DataFrame(4x3)> 0 1 2 0 Mike {:v=>10000 true 1 Jim {:v=>8000, false 2 Alice {:v=>12500 true 3 Bob {:v=>7000, true, @options={:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c3596e3e8 @cols=[{:type=>"string", :label=>"0"}, {:type=>"number", :label=>"1"}, {:type=>"boolean", :label=>"2"}], @rows=[[#<GoogleVisualr::DataTable::Cell:0x007f9c35352b08 @v="Mike", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c35352a18 @v=10000, @type="number", @f="$10,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c35352978 @v=true, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c35352860 @v="Jim", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c35352770 @v=8000, @type="number", @f="$8,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c353526d0 @v=false, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c35352608 @v="Alice", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c35352540 @v=12500, @type="number", @f="$12,500", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c353524a0 @v=true, @type="boolean">], [#<GoogleVisualr::DataTable::Cell:0x007f9c353523d8 @v="Bob", @type="string">, #<GoogleVisualr::DataTable::Cell:0x007f9c35352360 @v=7000, @type="number", @f="$7,000", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c35352248 @v=true, @type="boolean">]], @options={:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}>>
t3.show_in_iruby
t3.class
Daru::View::Table
t4 = Daru::View::Table.new([],opts)
#<Daru::View::Table:0x007f9c34ab4348 @data=[], @options={:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c34ab4320 @cols=[], @rows=[], @options={:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}>>
# users are free to use the Google charts features, by accessing the google chart table using `#table`
# Add Column Headers
t4.table.new_column('string', 'Year' )
t4.table.new_column('number', 'Sales')
t4.table.new_column('number', 'Expenses')
t4.table.new_column('number', 'Expenses2')
# Add Rows and Values
t4.table.add_rows([
['2004', 1000, 400, 1111],
['2005', 1170, 460, 2111],
['2006', 660, 1120, 3111],
['2007', 1030, 540, 1112]
])
[["2004", 1000, 400, 1111], ["2005", 1170, 460, 2111], ["2006", 660, 1120, 3111], ["2007", 1030, 540, 1112]]
t4.show_in_iruby
opts = {
cols: [
{id: 'A', label: 'NEW A', type: 'string'},
{id: 'B', label: 'B-label', type: 'number'},
{id: 'C', label: 'C-label', type: 'date'}
],
rows: [
{c:[
{v: 'a'},
{v: 1.0, f: 'One'},
{:v => Date.parse('2008-1-28 00:31:26'), f: '2/28/08 12:31 AM'}
]},
{c:[
{v: 'b'},
{v: 2.0, f: 'Two'},
{:v =>Date.parse('2008-2-12 00:31:27'), f: '3/30/08 12:31 AM'}
]},
{c:[
{v: 'c'},
{v: 3.0, f: 'Three'},
{v: Date.parse('2008-3-30 00:31:26'), f: '4/30/08 12:31 AM'}
]}
]
}
{:cols=>[{:id=>"A", :label=>"NEW A", :type=>"string"}, {:id=>"B", :label=>"B-label", :type=>"number"}, {:id=>"C", :label=>"C-label", :type=>"date"}], :rows=>[{:c=>[{:v=>"a"}, {:v=>1.0, :f=>"One"}, {:v=>#<Date: 2008-01-28 ((2454494j,0s,0n),+0s,2299161j)>, :f=>"2/28/08 12:31 AM"}]}, {:c=>[{:v=>"b"}, {:v=>2.0, :f=>"Two"}, {:v=>#<Date: 2008-02-12 ((2454509j,0s,0n),+0s,2299161j)>, :f=>"3/30/08 12:31 AM"}]}, {:c=>[{:v=>"c"}, {:v=>3.0, :f=>"Three"}, {:v=>#<Date: 2008-03-30 ((2454556j,0s,0n),+0s,2299161j)>, :f=>"4/30/08 12:31 AM"}]}]}
t_ops = Daru::View::Table.new([],opts)
#<Daru::View::Table:0x007f9c34a668a0 @data=[], @options={:cols=>[{:id=>"A", :label=>"NEW A", :type=>"string"}, {:id=>"B", :label=>"B-label", :type=>"number"}, {:id=>"C", :label=>"C-label", :type=>"date"}], :rows=>[{:c=>[{:v=>"a"}, {:v=>1.0, :f=>"One"}, {:v=>#<Date: 2008-01-28 ((2454494j,0s,0n),+0s,2299161j)>, :f=>"2/28/08 12:31 AM"}]}, {:c=>[{:v=>"b"}, {:v=>2.0, :f=>"Two"}, {:v=>#<Date: 2008-02-12 ((2454509j,0s,0n),+0s,2299161j)>, :f=>"3/30/08 12:31 AM"}]}, {:c=>[{:v=>"c"}, {:v=>3.0, :f=>"Three"}, {:v=>#<Date: 2008-03-30 ((2454556j,0s,0n),+0s,2299161j)>, :f=>"4/30/08 12:31 AM"}]}]}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c34a66878 @cols=[{:type=>"string", :label=>"NEW A", :id=>"A"}, {:type=>"number", :label=>"B-label", :id=>"B"}, {:type=>"date", :label=>"C-label", :id=>"C"}], @rows=[[#<GoogleVisualr::DataTable::Cell:0x007f9c34a66648 @v="a", @type="string", @f=nil, @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a665d0 @v=1.0, @type="number", @f="One", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a664b8 @v=#<Date: 2008-01-28 ((2454494j,0s,0n),+0s,2299161j)>, @type="date", @f="2/28/08 12:31 AM", @p=nil>], [#<GoogleVisualr::DataTable::Cell:0x007f9c34a663f0 @v="b", @type="string", @f=nil, @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a66378 @v=2.0, @type="number", @f="Two", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a66260 @v=#<Date: 2008-02-12 ((2454509j,0s,0n),+0s,2299161j)>, @type="date", @f="3/30/08 12:31 AM", @p=nil>], [#<GoogleVisualr::DataTable::Cell:0x007f9c34a66198 @v="c", @type="string", @f=nil, @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a660d0 @v=3.0, @type="number", @f="Three", @p=nil>, #<GoogleVisualr::DataTable::Cell:0x007f9c34a65ef0 @v=#<Date: 2008-03-30 ((2454556j,0s,0n),+0s,2299161j)>, @type="date", @f="4/30/08 12:31 AM", @p=nil>]], @options={:cols=>[{:id=>"A", :label=>"NEW A", :type=>"string"}, {:id=>"B", :label=>"B-label", :type=>"number"}, {:id=>"C", :label=>"C-label", :type=>"date"}], :rows=>[{:c=>[{:v=>"a"}, {:v=>1.0, :f=>"One"}, {:v=>#<Date: 2008-01-28 ((2454494j,0s,0n),+0s,2299161j)>, :f=>"2/28/08 12:31 AM"}]}, {:c=>[{:v=>"b"}, {:v=>2.0, :f=>"Two"}, {:v=>#<Date: 2008-02-12 ((2454509j,0s,0n),+0s,2299161j)>, :f=>"3/30/08 12:31 AM"}]}, {:c=>[{:v=>"c"}, {:v=>3.0, :f=>"Three"}, {:v=>#<Date: 2008-03-30 ((2454556j,0s,0n),+0s,2299161j)>, :f=>"4/30/08 12:31 AM"}]}]}>>
t_ops.show_in_iruby # or t_ops.table
t_update = Daru::View::Table.new
#<Daru::View::Table:0x007f9c358eeb98 @data=[], @options={}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c358eeaf8 @cols=[], @rows=[], @options={}>>
t_update.table.new_column(type= 'string', 'Employee Name');
t_update.table.new_column(type= 'date', 'Start Date');
[{:type=>"string", :label=>"Employee Name"}, {:type=>"date", :label=>"Start Date"}]
t_update.table
t_update.table.add_rows(6)
6
t_update.table.set_cell(0, 0, 'Mike');
#<GoogleVisualr::DataTable::Cell:0x007f9c349a4b10 @v="Mike", @type="string">
t_update.table.set_cell(0, 1, Date.parse('2008-1-28'));
t_update.table.set_cell(1, 0, 'Bob');
t_update.table.set_cell(1, 1, Date.parse('2007-5-1'));
t_update.table.set_cell(2, 0, 'Alice');
t_update.table.set_cell(2, 1, Date.parse('2006-7-16'));
t_update.table.set_cell(3, 0, 'Frank');
t_update.table.set_cell(3, 1, Date.parse('2007-11-28'));
t_update.table.set_cell(4, 0, 'Floyd');
t_update.table.set_cell(4, 1, Date.parse('2005-3-13'));
t_update.table.set_cell(5, 0, 'Fritz');
t_update.table.set_cell(5, 1, Date.parse('2007-9-2'));
#<GoogleVisualr::DataTable::Cell:0x007f9c3424d628 @v=#<Date: 2007-09-02 ((2454346j,0s,0n),+0s,2299161j)>, @type="date">
t_update.table
t_update.table.get_column(0)
["Mike", "Bob", "Alice", "Frank", "Floyd", "Fritz"]
t_update.table.get_row(0)
["Mike", #<Date: 2008-01-28 ((2454494j,0s,0n),+0s,2299161j)>]
t_update.table.get_cell(3,0)
"Frank"
dv = Daru::Vector.new [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
t_vec = Daru::View::Table.new(dv)
#<Daru::View::Table:0x007f9c351e5040 @data=#<Daru::Vector(8)> 0 43934 1 52503 2 57177 3 69658 4 97031 5 119931 6 137133 7 154175, @options={}, @adapter=Daru::View::Adapter::GooglechartsAdapter, @table=#<GoogleVisualr::DataTable:0x007f9c351e4ff0 @cols=[{:type=>"number", :label=>"Series"}], @rows=[[#<GoogleVisualr::DataTable::Cell:0x007f9c351e4bb8 @v=43934, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e4ac8 @v=52503, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e49d8 @v=57177, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e48c0 @v=69658, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e47d0 @v=97031, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e46b8 @v=119931, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e4578 @v=137133, @type="number">], [#<GoogleVisualr::DataTable::Cell:0x007f9c351e4488 @v=154175, @type="number">]], @options={}>>
t_vec.table
t_vec.table.options
{}
# set new options in the table. This feature is not present in the basic google_visualr
t_vec.table.options = opts_pagination
{:showRowNumber=>true, :width=>"100%", :height=>"100%", :page=>"enable", :pageSize=>2, :pagingSymbols=>{:prev=>"prev", :next=>"next"}, :pagingButtonsConfiguration=>"auto"}
t_vec.table
# css styling
cssClassNames = {
'headerRow': 'italic-darkblue-font large-font bold-font',
'tableRow': '',
'oddTableRow': 'beige-background',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': '',
'headerCell': 'gold-border',
'tableCell': '',
'rowNumberCell': 'underline-blue-font'
};
opts_css = {'showRowNumber': true, pageSize: 5, 'allowHtml': true, 'cssClassNames': cssClassNames};
{:showRowNumber=>true, :pageSize=>5, :allowHtml=>true, :cssClassNames=>{:headerRow=>"italic-darkblue-font large-font bold-font", :tableRow=>"", :oddTableRow=>"beige-background", :selectedTableRow=>"orange-background large-font", :hoverTableRow=>"", :headerCell=>"gold-border", :tableCell=>"", :rowNumberCell=>"underline-blue-font"}}
t_vec.table.options = opts_css
{:showRowNumber=>true, :pageSize=>5, :allowHtml=>true, :cssClassNames=>{:headerRow=>"italic-darkblue-font large-font bold-font", :tableRow=>"", :oddTableRow=>"beige-background", :selectedTableRow=>"orange-background large-font", :hoverTableRow=>"", :headerCell=>"gold-border", :tableCell=>"", :rowNumberCell=>"underline-blue-font"}}
# FixMe: css is not working. May be there is other way to pass cssClassName
t_vec.table