XML/SWF Charts in Ruby on Rails

By August 8, 2009Ruby on Rails

Since RMagick seems to be broken on Win32 when using with Ruby 1.8.4 and Gruff is using RMagick to draw its pretty graphs, I had to go out and find an alternative chart library for use with Rails.


XML/SWF Charts
Fortunately I have run into this chart library a while back when doing php evelopment. It’s not really a php driven because the data input andconfiguration is in form of a XML file. To make it work with rails, all Ihad to do is translate the php function that generates the XML file to Ruby.

Well, after a few hours of playing around and experimenting with differentapproaches to the issue I think I have come up with a fairly easy way to get he charts working with rails.

Chart Sample

Chart Sample

The above sample displays a sample of a working graph. After a few failed / ugly attempts, I settled for a class/controller hybrid that hopefully is easy to use. So with out further rambling, here is a step by step guide to get it to work:

1.Download the SWF Chart library and place it in the /public directory

2.Download the swfchart.rb class and place in the /lib directory

3.Download chart_controller and place it in the /controllers directory

With all the pieces in place , the last two components will be tied to your application directly. The data and options for the chart have to be set in a controller. For instance something like this should work:

@swf = SWFChart.new

session[:swfchart] = @swf
#set data headings
@swf.data_array = Array.new
@swf.data_array[0] = [nil]
@swf.data_array[1] = [”]

#get space count per category
for c in @current_show.ShowCategories
@swf.data_array[0] << c.name
@swf.data_array[1] << c.BaseSpaces.count
end
@swf.chart_type = "3d pie"
@swf.chart_rect = {:x => ‘150’,:width => ‘200’,:height => ‘125’}
@swf.legend_rect = {:x => 10, :y => 10, :width => 50, :height => 200}

And finally, the chart will be displayed from the view with code like this:

<%= @swf.insert_chart(:data_source => url_for(:controller => "/chart"), :width => "400", :height => "300") %>

Observant readers will have noticed that the whole chart data-setting and generation is encapsulated in the swfchart.rb class from the /lib directory. Unfortunately I don’t have much time to encapsulate this as a plugin, but perhaps somebody could take what I have and run with it…

06.12.06 – Edit
There have been a few issues reported by a few users while trying to get this up and running. In order to solve some of the more common mistakes, I have decided to create a sample project. Just download testapp.zip, unzip, run webrick and browse to http://localhost:3000/sample

08.02.06 – Edit
Version 0.1.14 of the SWFChart wrapper is done. Changes include a "plugin" like structure (still have to solve copying of the charts to the public directory before it becomes a true plugin) and minor bugfixes to xml generator method.

Installation:
Just download the swfchart.0.1.14.zip and unzip it to the root of your rails application. If you have a previous version installed , remove swfchart.rb from the lib directory and any require statements from your controllers.