Class: Gvis::DataTable

Inherits:
Object
  • Object
show all
Defined in:
lib/gvis/data_table.rb

Constant Summary collapse

COLUMN_TYPES =
["string", "number", "date", "datetime", "timeofday"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, columns = [], options = {}) ⇒ DataTable

Returns a new instance of DataTable.

Parameters:

  • data (Array) (defaults to: nil)

    optional param that may contain a 2D Array for specifying the data upon initialization

  • columns (Array) (defaults to: [])

    optional param for specifying the column structure upon initialization

  • options (Hash) (defaults to: {})

    optional param of configuration options for the google.visualization.DataTable javascript object



18
19
20
21
22
23
24
25
26
# File 'lib/gvis/data_table.rb', line 18

def initialize(data = nil, columns = [], options = {})
  @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_typesObject

Returns the value of attribute column_types.



13
14
15
# File 'lib/gvis/data_table.rb', line 13

def column_types
  @column_types
end

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/gvis/data_table.rb', line 13

def data
  @data
end

#table_columnsObject

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

Parameters:

  • row (Array)

    An array with a single row of data for the table. Should have the same number of entries as there are columns

Raises:

  • (ArgumentError)


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

Parameters:

  • rows (Array)

    A 2d Array containing multiple rows of data. Each Array should have the same number of entries as the table has columns

Raises:

  • (ArgumentError)


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

#columnsArray

Returns The columns stored in this table.

Returns:

  • (Array)

    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” }

Parameters:

  • cols (Hash)

    a hash where keys are column names, and values are a string of the column type



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_dataString 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

Returns:

  • (String)

    a javascript array with the first row defining the table, and subsequent rows holding the table’s data



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