Class: Google::Visualization::DataTable

Inherits:
DataElement show all
Defined in:
lib/google_visualization/data_table.rb

Overview

DataTable

Description

Represents a two-dimensional, mutable table of values.

Instance Method Summary collapse

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.

Raises:

  • (StandardError)


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.

Raises:

  • (StandardError)


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

#columnsObject

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_countObject

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

#rowsObject

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_countObject

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

#to_csvObject

Returns a CSV representation of the table, according to the Google Visualization API.



136
137
138
# File 'lib/google_visualization/data_table.rb', line 136

def to_csv
  Formatter::CSV.render(self)
end

#to_jsonObject

Returns a JSON representation of the table, according to the Google Visualization API.



129
130
131
# File 'lib/google_visualization/data_table.rb', line 129

def to_json
  Formatter::JSON.render(self)
end