Open Flash Chart 2 + CodeIgniter

Many of you maybe familiar with the CodeIgniter (CI) PHP framework. I was asked by a client recently about creating a graph for some data in a report. CodeIgniter LogoLooking through several charting libraries at the CI website, I found a few options listed in the wiki. Open Flash Chart 2 (OFC) seemed like a nice option and was also open source. There was a couple libraries written for CI using Open Flash Chart, but the recent code for OFC has recently changed.

The new code for Open Flash Chart 2 has separated the PHP classes making it very difficult using in CodeIgniter. Thankfully Mesozoic had merged all of the classes back into a single file.

Here are the steps getting CodeIgniter and Open Flash Chart 2 to play nicely together. Download the Open Flash Chart 2 and extract the files. In this zip file you will need to extract open-flash-chart.swf to the root folder of your server. Then place the other files in the application folder into your CI application folder.

I’ve also coded  a CI helper for inserting the chart into your page that plays nicely with CI’s URL helper.  Here’s example of how to use Open Flash Chart 2:

In the controller of your application add the following code below.

[php]

$this->load->helper(‘open-flash-chart’); // load helper function
$this->load->library(‘ofc’); //load Open Flash Chart
[/php]

Now you can use the code that is shown on the Open Flash Chart website, execpt make a like change when building the chart. You will now use:

[php]

$this->ofc->open_flash_chart();
$this->ofc->set_title( $title );
$this->ofc->add_element( $line );
$this->ofc->set_x_axis( $x_axis );
$this->ofc->set_y_axis( $y_axis);
return $this->ofc->toPrettyString();

[/php]

You can find more information on this on the CodeIgniter forums.