I am working on a few projects that require some simple charts. Historically I have been a big supporter of JasperReports and JFreeCharts (in fact I still am). However both those tools run server side to generate the reports and charts. Sometimes you just don’t need that level of complication. If your looking for something quick to just display a basic (or compound) chart over a table, then using a flash charting tool might be the answer. I use FusionCharts. They even have some rails examples (though I don’t like them it looks like a java programmer wrote them).

Pros

  • Fast Development
  • Client side chart generation (less load on the server)
  • Animation and other pretty effects

Cons

  • Client needs flash installed
  • Only supports web pages (no exporting)
  • More data transfer then just one image

Quick Notes
As stated above, you can go along way with the flash charts if your just looking for some really basic presentation. Get to complicated though, and your going to have to use a Reporting Engine. Converting a large number of reports from a web version to a reporting engine can be expensive, and in many cases, reporting engines can’t support all the features that your used to because of the exporting they need to do. If you choose to implement flash based charts make sure that it’s not a stop gap measure, when what you really need is a full suite.

Good News
Flash charts take xml. Rails (specially restful) can send the xml to the chart with little effort. All you really have to do is format the data for the chart. There are several solutions. You can override the models .to_xml method, you can use render :text=>prepaired_xml_string, or you can use an xml builder to do what you want. I chose the 2nd option (render :text). It’s easy to implement and lets you keep your view stuff at a controller level (I know, I know, but you have to put it somewhere). Here’s what I did

Implementation

  1. Define a new format
    Mime::Type.register_alias “text/xml”, :chart
  2. In your controller add in a responds_to block for the chart type
          format.chart{
            chart = "“
            for m in @cmodels
              chart << ”< set name=‘#{m.name}’ value=‘#{m.value}’ / >“
            end
            chart << ”"
            render :text=>chart.to_s
          }
  3. Then in the view, do your normal layout. But add in the chart. (I used a helper for this part)

    &nbsp;

Conclusion

That’s it. You now have a basic chart. In this example i put it on the index method, but there’s no reason you can’t place it on custom methods. I even recomend taking that controller cruft that generates the “xml” and placeing it in a helper.

Coteyr.net Programming LLC. is about one thing. Getting your project done the way you like it. Using Agile development and management techniques, we are able to get even the most complex projects done in a short time frame and on a modest budget.

Feel free to contact me via any of the methods below. My normal hours are 10am to 10pm Eastern Standard Time. In case of emergency I am available 24/7.

Email: coteyr@coteyr.net
Phone: (813) 421-4338
GTalk: coteyr@coteyr.net
Skype: coteyr
Guru: Profile