CodeIgniter Forums
Semi-complete FusionCharts helper for Code Igniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Semi-complete FusionCharts helper for Code Igniter (/showthread.php?tid=46285)



Semi-complete FusionCharts helper for Code Igniter - El Forum - 10-26-2011

[eluser]Kobus M[/eluser]
Hi all,

I have written a FusionCharts (www.fusioncharts.com) helper for use in CodeIgniter to make graphs easier to generate (at least for me). If someone can use this, please don't hesitate to copy the helper (and code in the controller and view) to generate graphs. We are using a purchased FusionCharts, not a free or evaluation, so some of the features may not work for you, though. It should be a good start for you to get it implemented.

The reason I am posting this is two-fold:

1. To give back to the community who has helped me so many times before
2. To get comments or feedback on how to make this better, because it is far from optimized.

With that said, please see below the implementation. If you need specific details about something, feel free to ask.

PREPARATION:

1. Copy the fusioncharts graphs into /application/libraries/fusioncharts
2. Copy the FusioCharts.js file into /application/libraries/fusioncharts
3. Copy FusionCharts.php file into /application/libraries
4. Copy the charts_helper.php into /application/helpers

If you want to use the exporting feature (exporting graphs as images so you can use them in PDFs) then you need to do the following as well:

5. Copy FCExporter.php into /application/libraries/FusionChartsExportHandlers
6. Copy FCExporter_IMG and FCExporter_PDF into /application/libraries/FusionChartsExportHandlers/Resource
7. Edit your paths in FCExporter.php to point to your server path and html path.
8. Make a folder "temp" with 0777 rights (might be able to reduce it, I am no Linux expert)

With these tasks, your preparation is complete.

Now you will find a snippet of code needed in a controller and a view to implement the graphs.

CONTROLLER:

First you need to get your data, via your model, and depending on your type of graph, in key => value pairs (see the $cData variable below). Some other types of graphs have different ways of presenting data, though, but this helper and code examples work for normal Column and Pie charts, as well as simple multi-series charts.

Once you have your data, you need to initialize your graphs:

Code:
$data['chartID1'] = "SampleRMSChart1";
$data['exportFormat'] = 'JPG';
$chartsettings1 = array(
    'caption' => 'Contract type',
    'numberSuffix' => '',
    'chartID' => $data['chartID1'],
    'exportFileName' => 'RMSContractsGraph_status1'
);
$chartbase1 = array(
    'type' => 'Column3D',
    'width' => 300,
    'height' => 320,
    'div' => 'RMSchartDiv1',
    'bgColor' => 'ffffff'
);
$this->load->library('FusionCharts');
$this->load->helper('charts');
$cData1 = array(
    'Item 1' => $item1,
    'Item 2' => $item2,
    'Item 3' => $item3
);
$data['chart1'] = BuildChart($cData1, $chartsettings1, $chartbase1);

You may ask why I am using two variables $chartbase1 and $chartsettings1. This will be clear when you view the helper file. $chartbase1 is for settings related to the chart itself, and $chartsettings1 is for the different elements inside the graph (items in the < set > groups).

VIEW:

In your view, you simply need to echo out the chart variable:

Code:
echo $chart1

There are more things to do in the view if you would want to export the graphs as images, and the code is as follows, but can most certainly be improved

Code:
&lt; script type = " text / javascript " (edited to prevent removal) &gt;
    function FC_Rendered ()
    {
        if (FusionCharts("&lt;?php echo $chartID1; ?&gt;").exportChart)
        {
            FusionCharts("&lt;?php echo $chartID1; ?&gt;").exportChart( { "exportFormat" : '&lt;?php echo $exportFormat; ?&gt;' } );
        }
    }

    function FC_Exported (statusObj)
    {
        $('#filename1').text(statusObj.fileName);
        // Additional things you want to do after export such as enabling PDF buttons or something.
        // The filename is stored in a hidden HTML element for later use with an Ajax request, so that the name
        // of the file is available for me to use in DOMPDF where I need the export to be placed inside the PDF.
    }
&lt; / script (edited to prevent removal) &gt;

Hope this helps someone, as it took me quite a while to figure this out myself.

Comments welcome.

EDITED: Fixed some errors in my code: 12:13 (GMT+2, 26-10-2011)


Semi-complete FusionCharts helper for Code Igniter - El Forum - 03-10-2012

[eluser]Unknown[/eluser]
I can't seem to download the helper. Can you repost it? Are other people having the same issue downloading forum attachments?


Semi-complete FusionCharts helper for Code Igniter - El Forum - 03-11-2012

[eluser]Kobus M[/eluser]
[quote author="Dustin Lunt" date="1331416661"]I can't seem to download the helper. Can you repost it? Are other people having the same issue downloading forum attachments?[/quote]

I seem unable to attach it, and the message gets truncated with the size of the post, so here is part of the code in this post, the rest following in a reply to this. Hope it goes through:

Code:
function ChartBase($data)
    {
        if (!isset($data['type']))
        {
            $data['type'] = 'Column3D';
        }
        if (!isset($data['width']))
        {
            $data['width'] = 800;
        }
        if (!isset($data['height']))
        {
            $data['height'] = 600;
        }
        if (!isset($data['div']))
        {
            $data['div'] = 'RMSchart';
        }
        return $data;
    }


    function BuildChart($chartdata, $chartsettings, $chartbase)
    {
        $FC = new FusionCharts($chartbase['type'], $chartbase['width'], $chartbase['height']);
        $FC->chartID = $chartsettings['chartID'];
        $FC->setSWFPath(base_url(). "application/libraries/fusioncharts/");
        $FC->setChartParams(ChartSettings($chartsettings));
        foreach ($chartdata as $key => $value)
        {
            $FC->addChartData($value, "Label=" . $key);
        }
        return $FC->renderChart(false, false);
    }

Regards,

Kobus




Semi-complete FusionCharts helper for Code Igniter - El Forum - 03-11-2012

[eluser]Kobus M[/eluser]
Some more code of the helper I am unable to attach:

Code:
function ChartSettings($data)
    {
        if (!isset($data['ChartNoDataText']))
        {
            $data['ChartNoDataText'] = 'Chart data not provided.';
        }
        if (!isset($data['PBarLoadingText']))
        {
            $data['PBarLoadingText'] = 'Please wait while the chart is loading...';
        }
        if (!isset($data['numberSuffix']))
        {
            $data['numberSuffix'] = '';
        }
        if (!isset($data['numberPrefix']))
        {
            $data['numberPrefix'] = '';
        }
        if (!isset($data['formatNumberScale']))
        {
            $data['formatNumberScale'] = 0;
        }
        if (!isset($data['caption']))
        {
            $data['caption'] = '';
        }
        if (!isset($data['subcaption']))
        {
            $data['subcaption'] = '';
        }
        if (!isset($data['baseFontSize']))
        {
            $data['baseFontSize'] = 13;
        }
        if (!isset($data['decimalPrecision']))
        {
            $data['decimalPrecision'] = 0;
        }
        if (!isset($data['xAxisName']))
        {
            $data['xAxisName'] = '';
        }
        if (!isset($data['yAxisName']))
        {
            $data['yAxisName'] = '';
        }
        if (!isset($data['animation']))
        {
            $data['animation'] = 1;
        }
        if (!isset($data['exportEnabled']))
        {
            $data['exportEnabled'] = 1;
        }
        if (!isset($data['exportAtClient']))
        {
            $data['exportAtClient'] = 0;
        }
        if (!isset($data['exportShowMenuItem']))
        {
            $data['exportShowMenuItem'] = 1;
        }
        if (!isset($data['showExportDialog']))
        {
            $data['showExportDialog'] = 0;
        }
        if (!isset($data['exportDialogMessage']))
        {
            $data['exportDialogMessage'] = 'Exporting data...';
        }
        if (!isset($data['exportFileName']))
        {
            $data['exportFileName'] = 'RMSgraph001';
        }
        if (!isset($data['exportAction']))
        {
            $data['exportAction'] = 'save';
        }
        if (!isset($data['exportFormat']))
        {
            $data['exportFormat'] = 'png';
        }
        if (!isset($data['saveMode']))
        {
            $data['saveMode'] = 'both';
        }
        if (!isset($data['exportCallback']))
        {
            $data['exportCallback'] = 'FC_Exported';
        }
        if (!isset($data['showLabels']))
        {
            $data['showLabels'] = 1;
        }
        if (!isset($data['showLegend']))
        {
            $data['showLegend'] = 1;
        }
        if (!isset($data['labelDisplay']))
        {
            $data['labelDisplay'] = 'rotate';
        }
        if (!isset($data['slantLabels']))
        {
            $data['slantLabels'] = 1;
        }
        if (!isset($data['logoPosition']))
        {
            $data['logoPosition'] = 'TR';
        }
        if (!isset($data['bgColor']))
        {
            $data['bgColor'] = 'F2F3FF, FFFFFF';
        }
        if (!isset($data['bgAlpha']))
        {
            $data['bgAlpha'] = '80, 100';
        }
        if (!isset($data['bgRatio']))
        {
            $data['bgRatio'] = '20, 80';
        }
        if (!isset($data['bgAngle']))
        {
            $data['bgAngle'] = '270';
        }
        if (!isset($data['borderColor']))
        {
            $data['borderColor'] = 'EBEBEB';
        }
        if (!isset($data['borderThickness']))
        {
            $data['borderThickness'] = '1';
        }
        if (!isset($data['showBorder']))
        {
            $data['showBorder'] = '1';
        }
        if (!isset($data['decimals']))
        {
            $data['decimals'] = 2;
        }
        if (!isset($data['formatNumberScale']))
        {
            $data['formatNumberScale'] = '0';
        }

        $paramStr = '
            ChartNoDataText=' . $data['ChartNoDataText'] . ';
            PBarLoadingText=' . $data['PBarLoadingText'] . ';
            showExportDialog=' . $data['showExportDialog'] . ';
            labelDisplay=' . $data['labelDisplay'] . ';
            slantLabels=' . $data['slantLabels'] . ';
            decimals=' . $data['decimals'] . ';
            formatNumberScale=' . $data['formatNumberScale'] . ';
            showLabels=' . $data['showLabels'] . ';
            showLegend=' . $data['showLegend'] . ';
            saveMode=' .$data['saveMode'] . ';
            numberSuffix=' . $data['numberSuffix'] . ';
            formatNumberScale=' . $data['formatNumberScale'] . ';
            exportEnabled=' . $data['exportEnabled'] . ';
            exportAtClient=' . $data['exportAtClient'] . ';
            exportAction=' . $data['exportAction'] . ';
            bgColor=' . $data['bgColor'] . ';
            bgAlpha=' . $data['bgAlpha'] . ';
            bgRatio=' . $data['bgRatio'] . ';
            bgAngle=' . $data['bgAngle'] . ';
            borderColor=' . $data['borderColor'] . ';
            showBorder=' . $data['showBorder'] . ';
            borderThickness=' . $data['borderThickness'] . ';
            logoPosition=' . $data['logoPosition'] . ';
            exportHandler=' . base_url() . 'application/libraries/FusionChartsExportHandlers/FCExporter.php;
            caption=' . $data['caption'] . ';
            subcaption=' . $data['subcaption'] . ';
            baseFontSize=' . $data['baseFontSize'] . ';
            decimalPrecision=' . $data['decimalPrecision'] . ';
            exportFormat=' . $data['exportFormat'] . ';
            animation=' . $data['animation'] . ';
            numberPrefix=' . $data['numberPrefix'] . ';
            exportFileName=' . $data['exportFileName'] . ';
            exportCallback=' . $data['exportCallback'] . ';
            xAxisName=' . $data['xAxisName'] . ';
            yAxisName=' . $data['yAxisName'] . '
        ';
        return $paramStr;
    }



Semi-complete FusionCharts helper for Code Igniter - El Forum - 05-30-2012

[eluser]thatguy142[/eluser]
Is there any way you can try to upload the zip file again, even if it's hosted elsewhere like mediafire? Thanks!


Semi-complete FusionCharts helper for Code Igniter - El Forum - 06-02-2012

[eluser]Kobus M[/eluser]
[quote author="thatguy142" date="1338404323"]Is there any way you can try to upload the zip file again, even if it's hosted elsewhere like mediafire? Thanks![/quote]

Hi,

Unfortunately I do not have the helper in its original state as it was when I wrote it. Posting the updated version will invalidate the rest of the post, but I have uploaded it here: http://www.impero.co.za/demo/chart.zip.

The post above's text and the file contents do not match 100% anymore, but is close enough - you should be able to manage.

Kobus



Semi-complete FusionCharts helper for Code Igniter - El Forum - 03-09-2013

[eluser]Unknown[/eluser]
Thank you ! This is the first explanation I've followed that actually worked, tried 4 others before this one.

Just to update the information for the next person:

Where it says "3. Copy FusionCharts.php file into /application/libraries", should with more recent versions be the class FusionCharts_Gen.php and I put it in system/libraries, instead of application/libraries.

Works.




Semi-complete FusionCharts helper for Code Igniter - El Forum - 07-18-2013

[eluser]quickshiftin[/eluser]
Nice work!