The endpoint of the Google Chart API API is stored in a class variable @@urlinside the Gchart class. So initially I was thinking about monkeypatching a class variable to set the url to https
Gchart.send :class_variable_set, :@@url, "https://chart.apis.google.com/chart?"
Alas, Google Charts does not work through https. Therefore, we cannot use this method. Since the methods of the Gchart class simply return the URL, we can wrap the calls in the proxy controller method that the API call server performs and proxy it to the client using the send_data ActionController method using your selection protocol. This way you don't have to reinvent the wheel of the Gchart library.
class ChartsController < ApplicationController
require 'net/http'
require 'gchart'
def show
options = params.except :controller, :action
options[:data].map! { |x| x.to_i } if options[:data]
begin
chart = URI.parse(Gchart.send options.delete(:type), options)
send_data Net::HTTP.get(chart), :content_type => 'image/png', :disposition => 'inline'
rescue
raise ActiveRecord::RecordNotFound
end
end
end
A helper that you can use in your views:
module ApplicationHelper
def chart_tag(options ={})
image_tag chart_path(options)
end
end
and route
map.resource :chart, :only => :show
Using:
<%= chart_tag :type => "line", :size => '200x300', :title => "example title", :bg => 'efefef', :legend => ['first data set label', 'second data set label'], :data => [10, 30, 120, 45, 72] %>
, .