Rails Flash Charting Plugin

Flash charts just look a lot better than what we could do with plan images. Flash is a lot more interactive and since they are vector base you can zoom in and out without image deterioration. The PHP folks have a lot of great libraries for working with Flash such as Ming, PHP/SWF Charts, and Amfphp. If you want flashy graphs, pun intended, you can do so thanks to ZiYa, an XML/SWF Charts based Ruby on Rails plugin. ZiYa can be installed from the following SVN repository.

svn://rubyforge.org/var/svn/liquidrail/plugins/ziya/trunk

You typically install a Rails plugin by executing ’script/plugin install URL’ from the application directory where you replace URL with the one above.

Once ZiYa has been installed you need to include one line into the controller to load the required graphing capabilities. Add the following line in the controller near the top.

include Ziya

To follow along with the code examples, create a bargraph action in the controller so that we can create a bar graph. In the bargraph action we will create a bar chart object with code similar to the following.

def bargraph
chart = Ziya::Charts::Bar.new
render :text => chart
end

The new bar chart will contain default data so that at this point we can quickly move onto the view and see ZiYa and XML/SWF in action. You do not have to create a rhtml view for bargraph since the action will generate the necessary XML required for the chart. You can render the chart in any rhtml view by adding the following view helper code.

<%= gen_chart(”chart_id”,
url_for(:controller => ‘mycontroller’, :action => “bargraph”),
“#ffffff”,
400,
300) %>

The above action and view code will generate a chart with default data that looks like the following.

ZiYa Bar Chart Example

if you want to create a similar graph with code replace the action with the following code.

def bargraph
chart = Ziya::Charts::Bar.new
chart.add(:axis_category_text, [ "2003", "2004", "2005"])
chart.add(:series, “Region A”, [100, 25, 40], ['Large', 'Low', 'Soso'])
chart.add(:series, “Region A”, [80, 70, 20])
render :text => chart
end

For this graph, the :axis_category_text symbol indicate that the following values are the y-axis labels. To produce x-axis labels you can add data for :axis_value_text. The series symbol indicates your data points. You will also notice that when setting the series data point there is an optional string array. That string array is used as legend or label for the bars in this series. The chart is highly customizable, you can create yml files that describe the theme such as color, border, and even animation effect for each charts.

The other graph types are just as easy to generate. Just as an example below is the action code required to generate a pie chart.

def piegraph
chart = Ziya::Charts::Pie.new
chart.add :axis_category_text, ["2003", "2004", "2005"]
chart.add :series, “Region I”, [200, 100, 50], ['Super', 'Large', 'Medium']
render :text => chart
end

ZiYa is such a large and rich plugin that I obviously can’t do it justice. Please take a look at the Google Groups and official documentation.

Technorati Tags: , , , , , , , , , , , , ,

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

  1. Graphs Rails Plugin
  2. Rails PDF Plugin
  3. Rails Google Analytics Plugin
  4. Rails Layout
  5. Calendar Helper Plugin

This entry was posted in PHP, Ruby, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Ravi Kumar
    Posted February 21, 2007 at 1:56 am | Permalink

    Seems you forgot FusionCharts – http://www.fusioncharts.com – they can be used with PHP too and they’ve a free version too. In fact, they’ve some code samples and demo apps in RoR too.

  2. Posted May 21, 2007 at 9:44 am | Permalink

    Have you seen http://teethgrinder.co.uk/open-flash-chart/ ?? It is flash charts and line graphs. All open source. Needs a good Ruby wrapper though. Should be easy to write as there is a PHP wrapper already.

    monk.e.boy

  3. Posted August 24, 2007 at 9:46 am | Permalink

    Hey, just to let you know, a bunch of Ruby on Rails programmers have written (what do you call Rails plugins?) some code to do Open Flash Charts in Ruby! All the code is in the download. We also have a sourceforge project so you can get the Ruby and fiddle and post patches: Open Flash Chart

    cheers,

    monk.e.boy

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*