Class: QueryReport::Chart::ChartBase

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

Direct Known Subclasses

ColumnChart, PieChart

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, query, options = {}) ⇒ ChartBase

Returns a new instance of ChartBase.



9
10
11
12
13
# File 'lib/query_report/chart/chart_base.rb', line 9

def initialize(title, query, options={})
  @title = title
  @query = query
  @options = {:width => 500, :height => 240}.merge(options)
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'lib/query_report/chart/chart_base.rb', line 7

def columns
  @columns
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/query_report/chart/chart_base.rb', line 7

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/query_report/chart/chart_base.rb', line 7

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/query_report/chart/chart_base.rb', line 7

def title
  @title
end

Instance Method Details

#add(column_title, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/query_report/chart/chart_base.rb', line 15

def add(column_title, &block)
  val = block.call(@query)
  @columns ||= []
  @columns << QueryReport::Chart::Column.new(column_title, val.kind_of?(String) ? :string : :number)
  @data ||= []
  @data << val
end

#prepare_visualr(type) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/query_report/chart/chart_base.rb', line 23

def prepare_visualr(type)
  @data_table = GoogleVisualr::DataTable.new

  ##### Adding column header #####
  @data_table.new_column('string', '') if type == :column
  @columns.each do |col|
    @data_table.new_column(col.type.to_s, col.title)
  end
  ##### Adding column header #####

  ##### Adding value #####
  chart_row_data = type == :column ? [''] + @data : @data
  @data_table.add_row(chart_row_data)
  ##### Adding value #####

  options = {:title => title, backgroundColor: 'transparent'}.merge(@options)
  chart_type = "#{type}_chart".classify
  chart_type = "GoogleVisualr::Interactive::#{chart_type}".constantize
  ap @data_table.as_json
  chart_type.new(@data_table, options)
end

#to_blob(type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/query_report/chart/chart_base.rb', line 45

def to_blob(type)
  chart_type = type.to_s.classify
  chart_type = "Gruff::#{chart_type}".constantize

  @gruff = chart_type.new(@options[:width])
  @gruff.title = title
  @gruff.theme = QueryReport::Chart::Themes::GOOGLE_CHART
  @data.each_with_index do |value, i|
    @gruff.data(@columns[i].title, value)
  end

  @gruff.to_blob
end