Class: Google::Visualization::DataTable
- Inherits:
-
DataElement
- Object
- DataElement
- Google::Visualization::DataTable
- Defined in:
- lib/google_visualization/data_table.rb
Overview
DataTable
Description
Represents a two-dimensional, mutable table of values.
Instance Method Summary collapse
-
#add_column(obj) ⇒ Object
Adds a single column to the end of the table and returns self.
-
#add_columns(list) ⇒ Object
Adds multiple columns to the end of the table and returns self.
-
#add_row(obj) ⇒ Object
Adds a single row to the end of the table and returns self.
-
#add_rows(list) ⇒ Object
Adds multiple rows to the end of the table and returns self.
-
#cell(row_index, column_index) ⇒ Object
Returns the cell at index.
-
#column(index) ⇒ Object
Returns the column at index.
-
#columns ⇒ Object
Returns a enumerable of all columns.
-
#columns_count ⇒ Object
Returns the number of columns.
-
#initialize(columns = [], rows = []) ⇒ DataTable
constructor
Creates a new table.
-
#row(index) ⇒ Object
Returns the row at index.
-
#rows ⇒ Object
Returns a enumerable of all rows.
-
#rows_count ⇒ Object
Returns the number of rows.
-
#sort_rows!(order, *columns) ⇒ Object
Sorts the table rows according to the specified order and columns.
-
#to_csv ⇒ Object
Returns a CSV representation of the table, according to the Google Visualization API.
-
#to_json ⇒ Object
Returns a JSON representation of the table, according to the Google Visualization API.
Methods inherited from DataElement
#custom_properties, #custom_properties_count, #custom_property, #store_custom_properties, #store_custom_property
Constructor Details
#initialize(columns = [], rows = []) ⇒ DataTable
Creates a new table.
16 17 18 19 20 21 22 |
# File 'lib/google_visualization/data_table.rb', line 16 def initialize(columns=[], rows=[]) super() @columns = [] @rows = [] add_columns(columns) unless (columns.nil? or columns.empty?) add_rows(rows) unless (rows.nil? or rows.empty?) end |
Instance Method Details
#add_column(obj) ⇒ Object
Adds a single column to the end of the table and returns self.
87 88 89 90 91 92 93 |
# File 'lib/google_visualization/data_table.rb', line 87 def add_column(obj) raise(StandardError, "can't add columns after adding rows") if rows_count > 0 obj = DataColumn.new(obj) unless obj.is_a?(DataColumn) obj.close @columns << obj self end |
#add_columns(list) ⇒ Object
Adds multiple columns to the end of the table and returns self.
106 107 108 109 |
# File 'lib/google_visualization/data_table.rb', line 106 def add_columns(list) list.each { |obj| add_column(obj) } self end |
#add_row(obj) ⇒ Object
Adds a single row to the end of the table and returns self.
76 77 78 79 80 81 82 |
# File 'lib/google_visualization/data_table.rb', line 76 def add_row(obj) obj = DataRow.new(obj) unless obj.is_a?(DataRow) raise(StandardError, "wrong row size #{obj.cells_count}, should be #{columns_count})") if obj.cells_count != columns_count obj.close @rows << obj self end |
#add_rows(list) ⇒ Object
Adds multiple rows to the end of the table and returns self.
98 99 100 101 |
# File 'lib/google_visualization/data_table.rb', line 98 def add_rows(list) list.each { |obj| add_row(obj) } self end |
#cell(row_index, column_index) ⇒ Object
Returns the cell at index.
55 56 57 |
# File 'lib/google_visualization/data_table.rb', line 55 def cell(row_index, column_index) row(row_index).cell(column_index) end |
#column(index) ⇒ Object
Returns the column at index.
48 49 50 |
# File 'lib/google_visualization/data_table.rb', line 48 def column(index) @columns[index] end |
#columns ⇒ Object
Returns a enumerable of all columns.
69 70 71 |
# File 'lib/google_visualization/data_table.rb', line 69 def columns @columns.to_enum end |
#columns_count ⇒ Object
Returns the number of columns.
34 35 36 |
# File 'lib/google_visualization/data_table.rb', line 34 def columns_count @columns.size end |
#row(index) ⇒ Object
Returns the row at index.
41 42 43 |
# File 'lib/google_visualization/data_table.rb', line 41 def row(index) @rows[index] end |
#rows ⇒ Object
Returns a enumerable of all rows.
62 63 64 |
# File 'lib/google_visualization/data_table.rb', line 62 def rows @rows.to_enum end |
#rows_count ⇒ Object
Returns the number of rows.
27 28 29 |
# File 'lib/google_visualization/data_table.rb', line 27 def rows_count @rows.size end |
#sort_rows!(order, *columns) ⇒ Object
Sorts the table rows according to the specified order and columns.
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/google_visualization/data_table.rb', line 114 def sort_rows!(order, *columns) correlation = (order == :ascending ? 1 : -1) @rows.sort! { |a,b| pivot = 0 columns.each { |column| pivot = column break if a.cell(column).value != b.cell(column).value } (a.cell(pivot).value <=> b.cell(pivot).value) * correlation } end |