Class: Gvis::DataTable
- Inherits:
-
Object
- Object
- Gvis::DataTable
- Defined in:
- lib/gvis/data_table.rb
Constant Summary collapse
- COLUMN_TYPES =
["string", "number", "date", "datetime", "timeofday"]
Instance Attribute Summary collapse
-
#column_types ⇒ Object
Returns the value of attribute column_types.
-
#data ⇒ Object
Returns the value of attribute data.
-
#table_columns ⇒ Object
Returns the value of attribute table_columns.
Instance Method Summary collapse
-
#add_row(row) ⇒ Object
Adds a single row to the table.
-
#add_rows(rows) ⇒ Object
Adds multiple rows to the table.
-
#columns ⇒ Array
The columns stored in this table.
-
#columns=(cols) ⇒ Object
A one liner to set all the columns at once eg: chart.columns = { :signups => “number”, :day => “date” }.
-
#format_data ⇒ String
(also: #js_format_data, #to_s)
Outputs the data within this table as a javascript array ready for use by google.visualization.DataTable This is where conversions of ruby date objects to javascript Date objects and escaping strings, and formatting options is done.
-
#initialize(data = nil, columns = [], options = {}) ⇒ DataTable
constructor
A new instance of DataTable.
Constructor Details
#initialize(data = nil, columns = [], options = {}) ⇒ DataTable
Returns a new instance of DataTable.
18 19 20 21 22 23 24 25 26 |
# File 'lib/gvis/data_table.rb', line 18 def initialize(data = nil, columns = [], = {}) @table_columns, @column_types = [], {} if columns && columns.any? columns.each do |name, type| register_column(type, name) end end @data = data || [] end |
Instance Attribute Details
#column_types ⇒ Object
Returns the value of attribute column_types.
13 14 15 |
# File 'lib/gvis/data_table.rb', line 13 def column_types @column_types end |
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/gvis/data_table.rb', line 13 def data @data end |
#table_columns ⇒ Object
Returns the value of attribute table_columns.
13 14 15 |
# File 'lib/gvis/data_table.rb', line 13 def table_columns @table_columns end |
Instance Method Details
#add_row(row) ⇒ Object
Adds a single row to the table
44 45 46 47 48 |
# File 'lib/gvis/data_table.rb', line 44 def add_row(row) size = row.size raise ArgumentError.new("Given a row of data with #{size} entries, but there are only #{@table_columns.size} columns in the table") unless size == @table_columns.size @data << row end |
#add_rows(rows) ⇒ Object
Adds multiple rows to the table
52 53 54 55 56 57 58 |
# File 'lib/gvis/data_table.rb', line 52 def add_rows(rows) sizes = rows.collect {|r| r.size }.uniq expected_size = @table_columns.size errors = sizes.select {|s| s != expected_size } raise ArgumentError.new("Given a row of data with #{errors.to_sentence} entries, but there are only #{expected_size} columns in the table") if errors.any? @data += rows end |
#columns ⇒ Array
Returns The columns stored in this table.
38 39 40 |
# File 'lib/gvis/data_table.rb', line 38 def columns @table_columns end |
#columns=(cols) ⇒ Object
A one liner to set all the columns at once eg: chart.columns = { :signups => “number”, :day => “date” }
31 32 33 34 35 |
# File 'lib/gvis/data_table.rb', line 31 def columns=(cols) cols.each do |name, coltype| register_column(coltype, name) end end |
#format_data ⇒ String Also known as: js_format_data, to_s
Outputs the data within this table as a javascript array ready for use by google.visualization.DataTable This is where conversions of ruby date objects to javascript Date objects and escaping strings, and formatting options is done
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gvis/data_table.rb', line 72 def format_data formatted_rows = [] @data.each do |row| values = [] row.each_with_index do |entry,index| values << Gvis::DataCell.new(entry, @column_types.to_a[index][1]).to_js end rowstring = "[#{values.join(", ")}]" formatted_rows << rowstring end "[#{formatted_rows.join(', ')}]" end |