Skip to content

Instantly share code, notes, and snippets.

@sr
Created November 3, 2008 20:22
Show Gist options
  • Save sr/21969 to your computer and use it in GitHub Desktop.
Save sr/21969 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'open-uri'
require File.dirname(__FILE__) + '/gchartrb/lib/gchartrb'
def get_chart(color, data)
chart = GoogleChart::PieChart.new
chart.width = 600
chart.height = 300
total = data.inject(0) { |sum, (label, value)| sum = sum + value }
data_with_percentage = data.inject({}) do |data, (label, value)|
percent = (value/total)*100
data[label] = percent
data
end
data_with_percentage.each { |label, value| chart.data(label, value) }
url =
case color
when :red then chart.to_url + '&chco=FF0000'
when :blue then chart.to_url + '&chco=0000FF'
end
open(url).read
end
File.open('onss.png', 'w') do |file|
file << get_chart(:red, {
'Cotisations' => 37_020.6,
"Subvention de l'Etat" => 5_523.9,
"Financement alternatif" => 10_338.3,
"Autres recettes" => 3_399.3
})
end
File.open('inasti.png', 'w') do |file|
file << get_chart(:blue, {
'Cotisations' => 2_773.8,
"Subvention de l'Etat" => 1_123.834,
"Financement alternatif" => 662.7,
'Autres recettes' => 146.2
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment