Sha256: f6c33f1f6a0ad6faa75a4ef002a9dddc2c7ec076f3467c9a6ab10ad5c9331040

Contents?: true

Size: 1.46 KB

Versions: 66

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

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

66 entries across 66 versions & 2 rubygems

Version Path
newrelic_rpm-3.16.2.321 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.16.1.320 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.16.0.318 ui/helpers/google_pie_chart.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/newrelic_rpm-3.15.2.317/ui/helpers/google_pie_chart.rb
newrelic_rpm-3.15.2.317 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.15.1.316 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.15.0.314 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.14.3.313 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.14.2.312 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.14.1.311 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.14.0.305 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.13.2.302 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.13.1.300 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.13.0.299 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.12.1.298 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.12.0.288 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.11.2.286 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.11.1.284 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.11.0.283 ui/helpers/google_pie_chart.rb
newrelic_rpm-3.10.0.279 ui/helpers/google_pie_chart.rb