Sha256: 638d4b71dd5fa0222df6a39203817a1d9db097e9ebff4b2825629de4833f41a0

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

require 'cgi'

# A wrapper around the google charts service.
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 > 0 ? value * 100 / @max : value).round.to_s
    end
    params = {:cht => 'p', :chs => "#{width}x#{height}", :chd => "t:#{values.join(',')}", :chl => labels.join('|') }
    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

12 entries across 12 versions & 3 rubygems

Version Path
wd_newrelic_rpm-3.5.8 ui/helpers/google_pie_chart.rb
sundawg_newrelic_rpm-3.5.8.2 ui/helpers/google_pie_chart.rb
sundawg_newrelic_rpm-3.5.8.1 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.8.72 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.8.70 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.8.64.beta ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.7.59 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.7.59.beta ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.7.58.beta ui/helpers/google_pie_chart.rb
wd_newrelic_rpm-3.5.6 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.7.57.beta ui/helpers/google_pie_chart.rb
newrelic_rpm-3.5.6.55 ui/helpers/google_pie_chart.rb