Class: QueryReport::Chart::BasicChart

Inherits:
Object
  • Object
show all
Defined in:
lib/query_report/chart/basic_chart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, columns, data, options = {}) ⇒ BasicChart

Returns a new instance of BasicChart.



6
7
8
9
10
11
12
13
14
15
# File 'lib/query_report/chart/basic_chart.rb', line 6

def initialize(type, name, columns, data, options={})
  @type = type
  @name = name
  @columns = []
  columns.each_with_index do |column, i|
    @columns << QueryReport::Column.new(column, {type: (i == 0 ? 'string' : 'number')})
  end
  @data = data
  @options = options
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/query_report/chart/basic_chart.rb', line 4

def columns
  @columns
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/query_report/chart/basic_chart.rb', line 4

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/query_report/chart/basic_chart.rb', line 4

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/query_report/chart/basic_chart.rb', line 4

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/query_report/chart/basic_chart.rb', line 4

def type
  @type
end

Instance Method Details

#prepareObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/query_report/chart/basic_chart.rb', line 17

def prepare
  data_table = GoogleVisualr::DataTable.new
  columns.each do |column|
    data_table.new_column(column.type, column.name)
  end

  rows = []
  @data.each do |record|
    row = []
    columns.each do |column|
      row << record[column.name]
    end
    rows << row
  end
  data_table.add_rows(rows)

  opts = {:width => 400, :height => 240, :title => title, :hAxis => {:title => columns[0].name}}.merge(options)

  chart_type = "#{type}_chart".classify
  chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
  chart_type.new(data_table, opts)
end