I'm using the Google charts API to generate a pie chart in my Rails application. However, I'm having a problem passing local variables to the def in the helper. The def takes a 2D array of [label, value] pairs. It doesn't like it when I try to pass a local variable in as the value. These are calculated ahead of time and are in currency format. Putting the variable in quotes or in #{}
doesn't work either.
application_helper.rb
def pie_chart(data, options = {})
options[:width] ||= 250
options[:height] ||= 100
options[:colors] = %w(F5DD7E 0DB2AC FC8D4D FC694D FABA32 704948 968144 C08FBC ADD97E)
dt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-."
options[:divisor] ||= 1
options[:title] ||= "Energy Costs"
while (data.map { |k,v| v }.max / options[:divisor] >= 4096) do
options[:divisor] *= 20
end
opts = {
:cht => "p",
:chd => "e:#{data.map{|k,v|v=v/options[:divisor];dt[v/64..v/64]+dt[v%64..v%64]}}",
:chl => "#{data.map { |k,v| CGI::escape(k)}.join('|')}",
:chs => "#{options[:width]}x#{options[:height]}",
:chco => options[:colors].slice(0, data.length).join(','),
:chf => "bg,s,FFFFFF00",
:chtt => "#{options[:title]}"
}
The def call in the view:
<%= pie_chart( [["Light Cost", #{light_cost}], ["Small Appliance Cost", #{sm_appl_cost}]], :width => 550, :height => 200) %>
How do I pass the local variables?