Class: QueryReport::Chart::PieChart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PieChart.



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

def initialize(title, query, options={})
  @title = title
  @options = options
  @rows = []
  @query = query
end

Instance Attribute Details

#data_tableObject (readonly)

Returns the value of attribute data_table.



6
7
8
# File 'lib/query_report/chart/pie_chart.rb', line 6

def data_table
  @data_table
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/query_report/chart/pie_chart.rb', line 6

def options
  @options
end

#queryObject (readonly)

Returns the value of attribute query.



6
7
8
# File 'lib/query_report/chart/pie_chart.rb', line 6

def query
  @query
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/query_report/chart/pie_chart.rb', line 6

def rows
  @rows
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/query_report/chart/pie_chart.rb', line 6

def title
  @title
end

Instance Method Details

#add(column_title, &block) ⇒ Object



15
16
17
18
# File 'lib/query_report/chart/pie_chart.rb', line 15

def add(column_title, &block)
  val = block.call(@query)
  @rows << [column_title, val]
end

#prepareObject



20
21
22
23
24
25
26
27
# File 'lib/query_report/chart/pie_chart.rb', line 20

def prepare
  @data_table = GoogleVisualr::DataTable.new
  @data_table.new_column('string', 'Item')
  @data_table.new_column('number', 'Value')
  @data_table.add_rows(@rows)
  opts = {:width => 500, :height => 240, :title => @title, :is3D => true, backgroundColor: 'transparent'}.merge(options)
  GoogleVisualr::Interactive::PieChart.new(@data_table, opts)
end

#prepare_gruffObject



29
30
31
32
33
34
35
36
# File 'lib/query_report/chart/pie_chart.rb', line 29

def prepare_gruff
  @gruff = Gruff::Pie.new(options[:width] || 600)
  @gruff.title = title
  @gruff.theme = Gruff::Themes::GOOGLE_CHART
  @rows.each do |row|
    @gruff.data(row[0], row[1])
  end
end

#to_blobObject



38
39
40
41
# File 'lib/query_report/chart/pie_chart.rb', line 38

def to_blob
  prepare_gruff
  @gruff.to_blob
end