Sha256: 5c9efd235118cbaae67cb049ea1220289955626f7da459bdadac45b0d792b3d4

Contents?: true

Size: 1.45 KB

Versions: 20

Compression:

Stored size: 1.45 KB

Contents

# A wrapper around the google charts service.
# TODO consider making generic and open sourcing
class GooglePieChart
  attr_accessor :width, :height, :color

  def initialize
    # an array of [label, value]
    @data = []

    self.width = 300
    self.height = 200
  end

  def add_data_point(label, value)
    @data << [label, value]
    @max = (@max.nil? || @max < value ? value : @max)
  end

  # render the chart to html by creating an image object and
  # placing the correct URL to the google charts api
  def render
    labels = ''
    values = ''
    @data.each do |label, value|
      labels << CGI::escape(label) + '|'
      values << (value * 100 / @max).round.to_s + ","
    end

    # strip of the last separator char for labels and values
    labels = labels[0..-2]
    values = values[0..-2]
      
    params = {:cht => 'p', :chs => "#{width}x#{height}", :chd => "t:#{values}", :chl => labels }
    params['chco'] = color if color
    
    url = "http://chart.apis.google.com/chart?#{to_query(params)}"

    alt_msg = "This pie chart is generated by Google Charts. You must be connected to the Internet to view this chart."
    html = "<img id=\"pie_chart_image\" src=\"#{url}\" alt=\"#{alt_msg}\"/>"
    return html       
  end
  
private
  # Hash#to_query is not present in all supported rails platforms, so implement
  # its equivalent here.
  def to_query(params)
    p = []
    params.each do |k,v|
      p << "#{k}=#{v}"
    end
    
    p.join "&"
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
newrelic_rpm-2.9.9 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.8 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.6 ui/helpers/google_pie_chart.rb
genki-newrelic_rpm-2.10.1 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.5 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.2 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.3 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.9.4 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.5 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.6 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.7 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.11 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.2 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.3 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.4 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.1 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.0 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.10 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.9 ui/helpers/google_pie_chart.rb
newrelic_rpm-2.8.8 ui/helpers/google_pie_chart.rb