=begin This script creates a google chart image as output given acceptable input data. Usage: script [width [height]] Input data record format (tab delimited): Input data record example: 2008-09-10 4 1 The chicken count is ignored at the moment and a graph with date on the X, and egg count on the y is produced. The only limit is the encoded URL string limit, which on the google end is probably very large. =end require 'date' require 'net/http' if ARGV.length < 2 puts "usage: [width [heigth]]" exit end width = 640 if ARGV.length >= 3 width = ARGV[2] end height = 128 if ARGV.length >= 4 height = ARGV[3] end in_filename = ARGV[0] out_filename = ARGV[1] line_count = 0 line_count_valid = 0 date_today = DateTime.now #pipe_char = '|' pipe_char = '%7c' gchart_label = '' gchart_data = [] gchart_xlabel = '' data_extent = { 'min' => 0, 'max' => 0 } File.open( in_filename, "r" ) do |infile| while( line = infile.gets ) line_count = line_count + 1 #puts "#{line_count}: #{line}" #puts line.split( "\t" ).join( '|' ) line_a = line.split( "\t" ) # Example # 2008-09-10 4 1 # if line_a.length < 3 puts '[' + line_count.to_s() + ']: Warning: item_count=' + line_a.length.to_s + ' is not >= 3.' next end date_in = line_a[0] if ( date_in =~ /([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})/) == nil puts '[' + line_count.to_s() + ']: Warning: date="' + date_in + '".' next end date_in_h = { 'year' => $1, 'month' => $2, 'day' => $3 } if date_in_h['day'].to_i == 1 gchart_xlabel << pipe_char << date_in_h['year'] << '-' << date_in_h['month'] else gchart_xlabel << pipe_char end #puts "y=#{date_in_h['year']}, m=#{date_in_h['month']}, d=#{date_in_h['day']}" #puts "line valid: " + line_a.join( pipe_char ) chicken_count_in = line_a[1].to_i egg_count_in = line_a[2].to_i if egg_count_in > data_extent['max'] data_extent['max'] = egg_count_in end gchart_data << egg_count_in line_count_valid = line_count_valid + 1 end end gchart_ylabel = ( data_extent['min'] .. data_extent['max'] ).to_a.join( pipe_char ) # HTTP encoding # ':' -> %3a # ',' -> %2c # '|' -> %7c (is not "UNRESERVED" in ruby net/http library) gchart_url_s = '/chart' \ << '?cht=bvg' \ << '&chtt=Egg%20count' \ << '&chs=' << width.to_s << 'x' << height.to_s \ << '&chxt=x%2cy' \ << '&chbh=a%2c0%2c0' \ << '&chds=0%2c' << data_extent['max'].to_s \ << '&chxl=' << '0%3a' << pipe_char << gchart_xlabel << pipe_char << '1%3a' << pipe_char << gchart_ylabel \ << '&chd=t%3a' << gchart_data.join( '%2c' ) #puts 'gchart_url_s=' + gchart_url_s # Fetch google charts image via HTTP. url = URI.parse( 'http://chart.apis.google.com/' ) response = Net::HTTP.start( url.host, url.port ) { |http| http.get( gchart_url_s ) } # Save image to file. File.open( out_filename, 'wb') { |f| f.write( response.body ) }