bnn_nnb

Members
  • Content count

    1
  • Joined

  • Last visited

Posts posted by bnn_nnb


  1. If you're cool with PNGs, here's some Ruby code to take the FusionCharts data and return it as a PNG. Please let me know if you find any bugs in it, but it's been working well for me so far. I have this saved in my controllers as graph_controller.rb

     

     

     

    
    # * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    
    # * FusionChartsSave for Ruby on Rails (Jan 21 2008)
    
    # * Requires PNG gem from http://seattlerb.rubyforge.org/
    
    # * PNG is an almost-pure-ruby PNG library. 
    
    # * 
    
    # * sudo gem install -y png
    
    # * 
    
    # * The default location for the imageSaveURL would be:
    
    # * imageSaveURL='/graph/imagesave'
    
    # * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    
    require 'png'
    
    
    
    class GraphController < ApplicationController
    
    
    
    def imagesave
    
     #### Prevent Memory abuse
    
     maxwidth  = 1024
    
     maxheight = 1024
    
    
    
     width   = params[:width].to_i
    
     height  = params[:height].to_i
    
     bgcolor = params[:bgcolor]
    
     cdata   = params[:data]
    
    
    
     if width > maxwidth
    
    return nil
    
     end
    
     if height > maxheight
    
    return nil
    
     end
    
    
    
     if bgcolor.nil?
    
    bgcolor = PNG::Color::Background
    
     else
    
    bgcolor = PNG::Color.from "0x#{bgcolor.rjust(6,'0')}FF"
    
     end
    
    
    
     canvas = PNG::Canvas.new width, height, bgcolor
    
    
    
     # Keep count of which row we are on in our loop. Start on the top and draw down.
    
     ycount = height - 1
    
    
    
     rows = cdata.split(";") 
    
    
    
     rows.each do |row|
    
    ### Reset the count for each new row
    
    ri = 0
    
    
    
    pixels = row.split(",") 
    
    pixels.each do |pixel|
    
    	c,r = pixel.split("_")
    
    
    
    	if c.length > 0 
    
    		x = 0
    
    		mycolor = PNG::Color.from "0x#{c.rjust(6,'0')}FF"
    
    		while x < r.to_i
    
    			#point function will prevent drawing outside of canvas
    
    			canvas.point(ri + x, ycount, mycolor)
    
    			x += 1
    
    		end
    
    	end
    
    
    
    	ri += r.to_i
    
    end
    
    
    
    #Move down to the next row
    
    ycount -= 1
    
     end
    
    
    
     png = PNG.new canvas
    
     send_data(png.to_blob, {:type => "content-type:image/png", :stream => true, :filename => "FusionCharts.png"})
    
    
    
    end
    
    
    
    end