Class: Formatter::Png

Inherits:
Formatter
  • Object
show all
Defined in:
app/formatters/png_formatter.rb

Constant Summary collapse

CHART_TYPES =
%w{line line_xy scatter bar venn pie pie_3d sparkline meter}

Instance Method Summary collapse

Instance Method Details

#finalize_tableObject



50
51
52
53
# File 'app/formatters/png_formatter.rb', line 50

def finalize_table
  require 'open-uri'
  output << open(google_url).read
end

#google_hashObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/formatters/png_formatter.rb', line 9

def google_hash
  out = options.to_hash.symbolize_keys
  columns = out.delete(:columns) || data.column_names

  if out[:legend] == false or skip_legend?
    out.delete(:legend)
  else
    out[:legend] ||= columns
  end
  
  unless out[:data]
    out[:data] = []
    columns.each_with_index do |col, i|
      out[:data][i] ||= []
    end
    data.each_with_index do |row, i|
      columns.each_with_index do |col, i|
        out[:data][i] << row[col]
      end
    end
  end
  
  out
end

#google_typeObject



38
39
40
# File 'app/formatters/png_formatter.rb', line 38

def google_type
  options.chart_type.try(:to_s).try(:downcase) || CHART_TYPES.first
end

#google_urlObject



42
43
44
45
46
47
48
# File 'app/formatters/png_formatter.rb', line 42

def google_url
  url = Gchart.send(google_type, google_hash)
  unwise = %w({ } | \ ^ [ ] `)
  unwise.each { |c| url.gsub!(c, "%#{c[0].to_s(16).upcase}") }
  url.gsub!(/\s/, "%20")
  url
end

#skip_legend?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/formatters/png_formatter.rb', line 34

def skip_legend?
  google_type == "sparkline"
end