Fusion Chart plugin for CodeIgniter

This is my own version in creating a fusion chart plugin for CodeIgniter. I tried to make it as simple as possible. Enjoy…

Step 1: Get your copy of free fusion chart here. http://www.fusioncharts.com/free/Gallery.asp?gMenuItemId=3
and extract all files.

Step 2: Copy the Charts folder to anywhere you like. I’ve set mine to /charts/ on the CI root directory.

Step 3: Copy the FusionCharts.js under the JSClass folder to anywhere you like. I’ve set mine to /javascripts/ on the CI root still.

Step 4: Copy everything from Code/PHPClass/Includes and put it inside system/plugins/fusion/.

Step 5: Build your CI Plugin. I named my fusion_pi.php and save it on system/plugins/:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function FusionCharts( $chart_type='', $width = "300", $height = "250" ){
	require_once( 'fusion/FusionCharts_Gen.php' );
	$FC = new FusionCharts( $chart_type, $width, $height );
	$FC->setSWFPath("images/fusioncharts/");
	return $FC;
}
?>

Using the plugin:

Note: You need to include FusionCharts.js on your HTML File preferably on the header part.

function mychart(){
    $this->load->plugin( 'fusion' );
    $FC =  FusionCharts("Column3D","520","200");
    $arrData = array( 500, 269, 254, 895, 633);
    foreach( $arrData as $i=>$data ){
        $FC->addChartData( $data );
    }
    $strParam="numberSuffix=%; formatNumberScale=0; decimalPrecision=0; xAxisName=Integrated Process Average Score; animation=1";
    $FC->setChartParams($strParam);
    $FC->setChartMessage("ChartNoDataText=Chart Data not provided; PBarLoadingText=Please Wait.The chart is loading...");
    return $FC->renderChart(false,false);
}

For more information in using Fusion Chart please see documentation included.

11 comments

  1. Great job – very useful indeed!

    /Moefelt

  2. Hi, would you mind to post the view ? o even a complete example
    i cant figure out how to use it in the view

    • The second part of the code is a portion of the controller already, use it with your view and your golden.

  3. jbeasley

    Ok, so i have messed around with the script to get it to work properly, but i have a question.

    if i do this

    $FC->addChartData(‘500′);

    it will show the data correctly, one bar with the value of 500, how do i name it though? I have looked through the docs quite a bit and cant find an answer.

    Your code above copied and pasted does not work, it’s show 5 plots but none with correct values, all with the word “ARRAY” as the value.

    Thanks in advance.

  4. jbeasley

    typo in your code above

    yours
    foreach( $arrData as $i=>$data ){

    fixed
    foreach( $arrData as $i=>$arrData ){

    • or you can change the:
      $FC->addChartData( $arrData );
      to
      $FC->addChartData( $data );

      my bad ^_^

  5. jbeasley

    I have also taken the time to simply echo it out in a view as the first person asked, and to also put everything in the CI file structure so that people can copy and paste the system over.

    (i always move my application folder out of my system folder so if you have a stock install of CI please make sure you put the folders in the correct spots.)

    http://www.o5media.com/share/graphs.zip

  6. jbeasley

    Np, if you want to post the updated code publicly, thats more than ok, the one spot that i spent a lot of questioning was how to attach a name to a value. I couldn’t figure it out with

    This method :
    $FC->addChartData( $arrData );

    But this works, and is the the zip i attached above:

    $arrData[0][0] = ‘Jan’;
    $arrData[1][0] = ‘Feb’;
    $arrData[2][0] = ‘Mar’;
    $arrData[3][0] = ‘Apr’;

    $arrData[0][1] = 200;
    $arrData[1][1] = 400;
    $arrData[2][1] = 200;
    $arrData[3][1] = 300;

    $FC->addChartDataFromArray( $arrData);

    I am going to hook it up to a database this week to plot some basic stats for a client lead system i’m building for a client. I’ll post the code and sqldump with some working examples when it is done.

    • Me neither, so I decided to output xml and javascript instead.

  7. how u do that ?

Leave a Reply